sha256.js 5.0 KB

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