sha256.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. (function (Math) {
  2. // Shortcuts
  3. var C = CryptoJS;
  4. var C_lib = C.lib;
  5. var WordArray = C_lib.WordArray;
  6. var Hasher = C_lib.Hasher;
  7. var C_algo = C.algo;
  8. // Initialization and round constants tables
  9. var H = [];
  10. var K = [];
  11. // Compute constants
  12. (function () {
  13. function isPrime(n) {
  14. var sqrtN = Math.sqrt(n);
  15. for (var factor = 2; factor <= sqrtN; factor++) {
  16. if (!(n % factor)) {
  17. return false;
  18. }
  19. }
  20. return true;
  21. }
  22. function getFractionalBits(n) {
  23. return ((n - (n | 0)) * 0x100000000) | 0;
  24. }
  25. var n = 2;
  26. var nPrime = 0;
  27. while (nPrime < 64) {
  28. if (isPrime(n)) {
  29. if (nPrime < 8) {
  30. H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
  31. }
  32. K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
  33. nPrime++;
  34. }
  35. n++;
  36. }
  37. }());
  38. // Reusable object
  39. var W = [];
  40. /**
  41. * SHA-256 hash algorithm.
  42. */
  43. var SHA256 = C_algo.SHA256 = Hasher.extend({
  44. _doReset: function () {
  45. this._hash = new WordArray.init(H.slice(0));
  46. },
  47. _doProcessBlock: function (M, offset) {
  48. // Shortcut
  49. var H = this._hash.words;
  50. // Working variables
  51. var a = H[0];
  52. var b = H[1];
  53. var c = H[2];
  54. var d = H[3];
  55. var e = H[4];
  56. var f = H[5];
  57. var g = H[6];
  58. var h = H[7];
  59. // Computation
  60. for (var i = 0; i < 64; i++) {
  61. if (i < 16) {
  62. W[i] = M[offset + i] | 0;
  63. } else {
  64. var gamma0x = W[i - 15];
  65. var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
  66. ((gamma0x << 14) | (gamma0x >>> 18)) ^
  67. (gamma0x >>> 3);
  68. var gamma1x = W[i - 2];
  69. var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
  70. ((gamma1x << 13) | (gamma1x >>> 19)) ^
  71. (gamma1x >>> 10);
  72. W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
  73. }
  74. var ch = (e & f) ^ (~e & g);
  75. var maj = (a & b) ^ (a & c) ^ (b & c);
  76. var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
  77. var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
  78. var t1 = h + sigma1 + ch + K[i] + W[i];
  79. var 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. // Intermediate hash value
  90. H[0] = (H[0] + a) | 0;
  91. H[1] = (H[1] + b) | 0;
  92. H[2] = (H[2] + c) | 0;
  93. H[3] = (H[3] + d) | 0;
  94. H[4] = (H[4] + e) | 0;
  95. H[5] = (H[5] + f) | 0;
  96. H[6] = (H[6] + g) | 0;
  97. H[7] = (H[7] + h) | 0;
  98. },
  99. _doFinalize: function () {
  100. // Shortcuts
  101. var data = this._data;
  102. var dataWords = data.words;
  103. var nBitsTotal = this._nDataBytes * 8;
  104. var nBitsLeft = data.sigBytes * 8;
  105. // Add padding
  106. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  107. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
  108. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
  109. data.sigBytes = dataWords.length * 4;
  110. // Hash final blocks
  111. this._process();
  112. // Return final computed hash
  113. return this._hash;
  114. },
  115. clone: function () {
  116. var clone = Hasher.clone.call(this);
  117. clone._hash = this._hash.clone();
  118. return clone;
  119. }
  120. });
  121. /**
  122. * Shortcut function to the hasher's object interface.
  123. *
  124. * @param {WordArray|string} message The message to hash.
  125. *
  126. * @return {WordArray} The hash.
  127. *
  128. * @static
  129. *
  130. * @example
  131. *
  132. * var hash = CryptoJS.SHA256('message');
  133. * var hash = CryptoJS.SHA256(wordArray);
  134. */
  135. C.SHA256 = Hasher._createHelper(SHA256);
  136. /**
  137. * Shortcut function to the HMAC's object interface.
  138. *
  139. * @param {WordArray|string} message The message to hash.
  140. * @param {WordArray|string} key The secret key.
  141. *
  142. * @return {WordArray} The HMAC.
  143. *
  144. * @static
  145. *
  146. * @example
  147. *
  148. * var hmac = CryptoJS.HmacSHA256(message, key);
  149. */
  150. C.HmacSHA256 = Hasher._createHmacHelper(SHA256);
  151. }(Math));