SHA256.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. (function(){
  2. var C = (typeof window === 'undefined') ? require('./Crypto').Crypto : window.Crypto;
  3. // Shortcuts
  4. var util = C.util,
  5. charenc = C.charenc,
  6. UTF8 = charenc.UTF8,
  7. Binary = charenc.Binary;
  8. // Constants
  9. var K = [ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  10. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  11. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  12. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  13. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  14. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  15. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  16. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  17. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  18. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  19. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  20. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  21. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  22. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  23. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  24. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2 ];
  25. // Public API
  26. var SHA256 = C.SHA256 = function (message, options) {
  27. var digestbytes = util.wordsToBytes(SHA256._sha256(message));
  28. return options && options.asBytes ? digestbytes :
  29. options && options.asString ? Binary.bytesToString(digestbytes) :
  30. util.bytesToHex(digestbytes);
  31. };
  32. // The core
  33. SHA256._sha256 = function (message) {
  34. // Convert to byte array
  35. if (message.constructor == String) message = UTF8.stringToBytes(message);
  36. /* else, assume byte array already */
  37. var m = util.bytesToWords(message),
  38. l = message.length * 8,
  39. H = [ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  40. 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 ],
  41. w = [],
  42. a, b, c, d, e, f, g, h, i, j,
  43. t1, t2;
  44. // Padding
  45. m[l >> 5] |= 0x80 << (24 - l % 32);
  46. m[((l + 64 >> 9) << 4) + 15] = l;
  47. for (var i = 0; i < m.length; i += 16) {
  48. a = H[0];
  49. b = H[1];
  50. c = H[2];
  51. d = H[3];
  52. e = H[4];
  53. f = H[5];
  54. g = H[6];
  55. h = H[7];
  56. for (var j = 0; j < 64; j++) {
  57. if (j < 16) w[j] = m[j + i];
  58. else {
  59. var gamma0x = w[j - 15],
  60. gamma1x = w[j - 2],
  61. gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
  62. ((gamma0x << 14) | (gamma0x >>> 18)) ^
  63. (gamma0x >>> 3),
  64. gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
  65. ((gamma1x << 13) | (gamma1x >>> 19)) ^
  66. (gamma1x >>> 10);
  67. w[j] = gamma0 + (w[j - 7] >>> 0) +
  68. gamma1 + (w[j - 16] >>> 0);
  69. }
  70. var ch = e & f ^ ~e & g,
  71. maj = a & b ^ a & c ^ b & c,
  72. sigma0 = ((a << 30) | (a >>> 2)) ^
  73. ((a << 19) | (a >>> 13)) ^
  74. ((a << 10) | (a >>> 22)),
  75. sigma1 = ((e << 26) | (e >>> 6)) ^
  76. ((e << 21) | (e >>> 11)) ^
  77. ((e << 7) | (e >>> 25));
  78. t1 = (h >>> 0) + sigma1 + ch + (K[j]) + (w[j] >>> 0);
  79. t2 = sigma0 + maj;
  80. h = g;
  81. g = f;
  82. f = e;
  83. e = (d + t1) >>> 0;
  84. d = c;
  85. c = b;
  86. b = a;
  87. a = (t1 + t2) >>> 0;
  88. }
  89. H[0] += a;
  90. H[1] += b;
  91. H[2] += c;
  92. H[3] += d;
  93. H[4] += e;
  94. H[5] += f;
  95. H[6] += g;
  96. H[7] += h;
  97. }
  98. return H;
  99. };
  100. // Package private blocksize
  101. SHA256._blocksize = 16;
  102. SHA256._digestsize = 32;
  103. })();