sha256.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /** @fileOverview Javascript SHA-256 implementation.
  2. *
  3. * An older version of this implementation is available in the public
  4. * domain, but this one is (c) Emily Stark, Mike Hamburg, Dan Boneh,
  5. * Stanford University 2008-2010 and BSD-licensed for liability
  6. * reasons.
  7. *
  8. * Special thanks to Aldo Cortesi for pointing out several bugs in
  9. * this code.
  10. *
  11. * @author Emily Stark
  12. * @author Mike Hamburg
  13. * @author Dan Boneh
  14. */
  15. /**
  16. * Context for a SHA-256 operation in progress.
  17. * @constructor
  18. * @class Secure Hash Algorithm, 256 bits.
  19. */
  20. sjcl.hash.sha256 = function (hash) {
  21. if (!this._key[0]) { this._precompute(); }
  22. if (hash) {
  23. this._h = hash._h.slice(0);
  24. this._buffer = hash._buffer.slice(0);
  25. this._length = hash._length;
  26. } else {
  27. this.reset();
  28. }
  29. };
  30. /**
  31. * Hash a string or an array of words.
  32. * @static
  33. * @param {bitArray|String} data the data to hash.
  34. * @return {bitArray} The hash value, an array of 16 big-endian words.
  35. */
  36. sjcl.hash.sha256.hash = function (data) {
  37. return (new sjcl.hash.sha256()).update(data).finalize();
  38. };
  39. sjcl.hash.sha256.prototype = {
  40. /**
  41. * The hash's block size, in bits.
  42. * @constant
  43. */
  44. blockSize: 512,
  45. /**
  46. * Reset the hash state.
  47. * @return this
  48. */
  49. reset:function () {
  50. this._h = this._init.slice(0);
  51. this._buffer = [];
  52. this._length = 0;
  53. return this;
  54. },
  55. /**
  56. * Input several words to the hash.
  57. * @param {bitArray|String} data the data to hash.
  58. * @return this
  59. */
  60. update: function (data) {
  61. if (typeof data === "string") {
  62. data = sjcl.codec.utf8String.toBits(data);
  63. }
  64. var i, b = this._buffer = sjcl.bitArray.concat(this._buffer, data),
  65. ol = this._length,
  66. nl = this._length = ol + sjcl.bitArray.bitLength(data);
  67. for (i = 512+ol & -512; i <= nl; i+= 512) {
  68. this._block(b.splice(0,16));
  69. }
  70. return this;
  71. },
  72. /**
  73. * Complete hashing and output the hash value.
  74. * @return {bitArray} The hash value, an array of 16 big-endian words.
  75. */
  76. finalize:function () {
  77. var i, b = this._buffer, h = this._h;
  78. // Round out and push the buffer
  79. b = sjcl.bitArray.concat(b, [sjcl.bitArray.partial(1,1)]);
  80. // Round out the buffer to a multiple of 16 words, less the 2 length words.
  81. for (i = b.length + 2; i & 15; i++) {
  82. b.push(0);
  83. }
  84. // append the length
  85. b.push(Math.floor(this._length / 0x100000000));
  86. b.push(this._length | 0);
  87. while (b.length) {
  88. this._block(b.splice(0,16));
  89. }
  90. this.reset();
  91. return h;
  92. },
  93. /**
  94. * The SHA-256 initialization vector, to be precomputed.
  95. * @private
  96. */
  97. _init:[],
  98. /*
  99. _init:[0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19],
  100. */
  101. /**
  102. * The SHA-256 hash key, to be precomputed.
  103. * @private
  104. */
  105. _key:[],
  106. /*
  107. _key:
  108. [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  109. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  110. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  111. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  112. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  113. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  114. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  115. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2],
  116. */
  117. /**
  118. * Function to precompute _init and _key.
  119. * @private
  120. */
  121. _precompute: function () {
  122. var i = 0, prime = 2, factor;
  123. function frac(x) { return (x-Math.floor(x)) * 0x100000000 | 0; }
  124. outer: for (; i<64; prime++) {
  125. for (factor=2; factor*factor <= prime; factor++) {
  126. if (prime % factor === 0) {
  127. // not a prime
  128. continue outer;
  129. }
  130. }
  131. if (i<8) {
  132. this._init[i] = frac(Math.pow(prime, 1/2));
  133. }
  134. this._key[i] = frac(Math.pow(prime, 1/3));
  135. i++;
  136. }
  137. },
  138. /**
  139. * Perform one cycle of SHA-256.
  140. * @param {bitArray} words one block of words.
  141. * @private
  142. */
  143. _block:function (words) {
  144. var i, tmp, a, b,
  145. w = words.slice(0),
  146. h = this._h,
  147. k = this._key,
  148. h0 = h[0], h1 = h[1], h2 = h[2], h3 = h[3],
  149. h4 = h[4], h5 = h[5], h6 = h[6], h7 = h[7];
  150. /* Rationale for placement of |0 :
  151. * If a value can overflow is original 32 bits by a factor of more than a few
  152. * million (2^23 ish), there is a possibility that it might overflow the
  153. * 53-bit mantissa and lose precision.
  154. *
  155. * To avoid this, we clamp back to 32 bits by |'ing with 0 on any value that
  156. * propagates around the loop, and on the hash state h[]. I don't believe
  157. * that the clamps on h4 and on h0 are strictly necessary, but it's close
  158. * (for h4 anyway), and better safe than sorry.
  159. *
  160. * The clamps on h[] are necessary for the output to be correct even in the
  161. * common case and for short inputs.
  162. */
  163. for (i=0; i<64; i++) {
  164. // load up the input word for this round
  165. if (i<16) {
  166. tmp = w[i];
  167. } else {
  168. a = w[(i+1 ) & 15];
  169. b = w[(i+14) & 15];
  170. tmp = w[i&15] = ((a>>>7 ^ a>>>18 ^ a>>>3 ^ a<<25 ^ a<<14) +
  171. (b>>>17 ^ b>>>19 ^ b>>>10 ^ b<<15 ^ b<<13) +
  172. w[i&15] + w[(i+9) & 15]) | 0;
  173. }
  174. tmp = (tmp + h7 + (h4>>>6 ^ h4>>>11 ^ h4>>>25 ^ h4<<26 ^ h4<<21 ^ h4<<7) + (h6 ^ h4&(h5^h6)) + k[i]); // | 0;
  175. // shift register
  176. h7 = h6; h6 = h5; h5 = h4;
  177. h4 = h3 + tmp | 0;
  178. h3 = h2; h2 = h1; h1 = h0;
  179. h0 = (tmp + ((h1&h2) ^ (h3&(h1^h2))) + (h1>>>2 ^ h1>>>13 ^ h1>>>22 ^ h1<<30 ^ h1<<19 ^ h1<<10)) | 0;
  180. }
  181. h[0] = h[0]+h0 | 0;
  182. h[1] = h[1]+h1 | 0;
  183. h[2] = h[2]+h2 | 0;
  184. h[3] = h[3]+h3 | 0;
  185. h[4] = h[4]+h4 | 0;
  186. h[5] = h[5]+h5 | 0;
  187. h[6] = h[6]+h6 | 0;
  188. h[7] = h[7]+h7 | 0;
  189. }
  190. };