AI销管
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

sha256.js 5.4 KiB

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