sha256.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict';
  2. /**
  3. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  4. * in FIPS 180-2
  5. * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
  6. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  7. *
  8. */
  9. var inherits = require('inherits');
  10. var Hash = require('./hash');
  11. var Buffer = require('safe-buffer').Buffer;
  12. var K = [
  13. 0x428A2F98,
  14. 0x71374491,
  15. 0xB5C0FBCF,
  16. 0xE9B5DBA5,
  17. 0x3956C25B,
  18. 0x59F111F1,
  19. 0x923F82A4,
  20. 0xAB1C5ED5,
  21. 0xD807AA98,
  22. 0x12835B01,
  23. 0x243185BE,
  24. 0x550C7DC3,
  25. 0x72BE5D74,
  26. 0x80DEB1FE,
  27. 0x9BDC06A7,
  28. 0xC19BF174,
  29. 0xE49B69C1,
  30. 0xEFBE4786,
  31. 0x0FC19DC6,
  32. 0x240CA1CC,
  33. 0x2DE92C6F,
  34. 0x4A7484AA,
  35. 0x5CB0A9DC,
  36. 0x76F988DA,
  37. 0x983E5152,
  38. 0xA831C66D,
  39. 0xB00327C8,
  40. 0xBF597FC7,
  41. 0xC6E00BF3,
  42. 0xD5A79147,
  43. 0x06CA6351,
  44. 0x14292967,
  45. 0x27B70A85,
  46. 0x2E1B2138,
  47. 0x4D2C6DFC,
  48. 0x53380D13,
  49. 0x650A7354,
  50. 0x766A0ABB,
  51. 0x81C2C92E,
  52. 0x92722C85,
  53. 0xA2BFE8A1,
  54. 0xA81A664B,
  55. 0xC24B8B70,
  56. 0xC76C51A3,
  57. 0xD192E819,
  58. 0xD6990624,
  59. 0xF40E3585,
  60. 0x106AA070,
  61. 0x19A4C116,
  62. 0x1E376C08,
  63. 0x2748774C,
  64. 0x34B0BCB5,
  65. 0x391C0CB3,
  66. 0x4ED8AA4A,
  67. 0x5B9CCA4F,
  68. 0x682E6FF3,
  69. 0x748F82EE,
  70. 0x78A5636F,
  71. 0x84C87814,
  72. 0x8CC70208,
  73. 0x90BEFFFA,
  74. 0xA4506CEB,
  75. 0xBEF9A3F7,
  76. 0xC67178F2
  77. ];
  78. var W = new Array(64);
  79. function Sha256() {
  80. this.init();
  81. this._w = W; // new Array(64)
  82. Hash.call(this, 64, 56);
  83. }
  84. inherits(Sha256, Hash);
  85. Sha256.prototype.init = function () {
  86. this._a = 0x6a09e667;
  87. this._b = 0xbb67ae85;
  88. this._c = 0x3c6ef372;
  89. this._d = 0xa54ff53a;
  90. this._e = 0x510e527f;
  91. this._f = 0x9b05688c;
  92. this._g = 0x1f83d9ab;
  93. this._h = 0x5be0cd19;
  94. return this;
  95. };
  96. function ch(x, y, z) {
  97. return z ^ (x & (y ^ z));
  98. }
  99. function maj(x, y, z) {
  100. return (x & y) | (z & (x | y));
  101. }
  102. function sigma0(x) {
  103. return ((x >>> 2) | (x << 30)) ^ ((x >>> 13) | (x << 19)) ^ ((x >>> 22) | (x << 10));
  104. }
  105. function sigma1(x) {
  106. return ((x >>> 6) | (x << 26)) ^ ((x >>> 11) | (x << 21)) ^ ((x >>> 25) | (x << 7));
  107. }
  108. function gamma0(x) {
  109. return ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3);
  110. }
  111. function gamma1(x) {
  112. return ((x >>> 17) | (x << 15)) ^ ((x >>> 19) | (x << 13)) ^ (x >>> 10);
  113. }
  114. Sha256.prototype._update = function (M) {
  115. var w = this._w;
  116. var a = this._a | 0;
  117. var b = this._b | 0;
  118. var c = this._c | 0;
  119. var d = this._d | 0;
  120. var e = this._e | 0;
  121. var f = this._f | 0;
  122. var g = this._g | 0;
  123. var h = this._h | 0;
  124. for (var i = 0; i < 16; ++i) {
  125. w[i] = M.readInt32BE(i * 4);
  126. }
  127. for (; i < 64; ++i) {
  128. w[i] = (gamma1(w[i - 2]) + w[i - 7] + gamma0(w[i - 15]) + w[i - 16]) | 0;
  129. }
  130. for (var j = 0; j < 64; ++j) {
  131. var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + w[j]) | 0;
  132. var T2 = (sigma0(a) + maj(a, b, c)) | 0;
  133. h = g;
  134. g = f;
  135. f = e;
  136. e = (d + T1) | 0;
  137. d = c;
  138. c = b;
  139. b = a;
  140. a = (T1 + T2) | 0;
  141. }
  142. this._a = (a + this._a) | 0;
  143. this._b = (b + this._b) | 0;
  144. this._c = (c + this._c) | 0;
  145. this._d = (d + this._d) | 0;
  146. this._e = (e + this._e) | 0;
  147. this._f = (f + this._f) | 0;
  148. this._g = (g + this._g) | 0;
  149. this._h = (h + this._h) | 0;
  150. };
  151. Sha256.prototype._hash = function () {
  152. var H = Buffer.allocUnsafe(32);
  153. H.writeInt32BE(this._a, 0);
  154. H.writeInt32BE(this._b, 4);
  155. H.writeInt32BE(this._c, 8);
  156. H.writeInt32BE(this._d, 12);
  157. H.writeInt32BE(this._e, 16);
  158. H.writeInt32BE(this._f, 20);
  159. H.writeInt32BE(this._g, 24);
  160. H.writeInt32BE(this._h, 28);
  161. return H;
  162. };
  163. module.exports = Sha256;