sha256.js 3.7 KB

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