sha256.js 5.9 KB

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