1
0

jssha256.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * jssha256 version 0.1 - Copyright 2006 B. Poettering
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. * 02111-1307 USA
  18. */
  19. /*
  20. * http://point-at-infinity.org/jssha256/
  21. *
  22. * This is a JavaScript implementation of the SHA256 secure hash function
  23. * and the HMAC-SHA256 message authentication code (MAC).
  24. *
  25. * The routines' well-functioning has been verified with the test vectors
  26. * given in FIPS-180-2, Appendix B and IETF RFC 4231. The HMAC algorithm
  27. * conforms to IETF RFC 2104.
  28. *
  29. * The following code example computes the hash value of the string "abc".
  30. *
  31. * SHA256_init();
  32. * SHA256_write("abc");
  33. * digest = SHA256_finalize();
  34. * digest_hex = array_to_hex_string(digest);
  35. *
  36. * Get the same result by calling the shortcut function SHA256_hash:
  37. *
  38. * digest_hex = SHA256_hash("abc");
  39. *
  40. * In the following example the calculation of the HMAC of the string "abc"
  41. * using the key "secret key" is shown:
  42. *
  43. * HMAC_SHA256_init("secret key");
  44. * HMAC_SHA256_write("abc");
  45. * mac = HMAC_SHA256_finalize();
  46. * mac_hex = array_to_hex_string(mac);
  47. *
  48. * Again, the same can be done more conveniently:
  49. *
  50. * mac_hex = HMAC_SHA256_MAC("secret key", "abc");
  51. *
  52. * Note that the internal state of the hash function is held in global
  53. * variables. Therefore one hash value calculation has to be completed
  54. * before the next is begun. The same applies the the HMAC routines.
  55. *
  56. * Report bugs to: jssha256 AT point-at-infinity.org
  57. *
  58. */
  59. /******************************************************************************/
  60. /* Two all purpose helper functions follow */
  61. /* string_to_array: convert a string to a character (byte) array */
  62. function string_to_array(str) {
  63. var len = str.length;
  64. var res = new Array(len);
  65. for(var i = 0; i < len; i++)
  66. res[i] = str.charCodeAt(i);
  67. return res;
  68. }
  69. /* array_to_hex_string: convert a byte array to a hexadecimal string */
  70. function array_to_hex_string(ary) {
  71. var res = "";
  72. for(var i = 0; i < ary.length; i++)
  73. res += SHA256_hexchars[ary[i] >> 4] + SHA256_hexchars[ary[i] & 0x0f];
  74. return res;
  75. }
  76. /******************************************************************************/
  77. /* The following are the SHA256 routines */
  78. /*
  79. SHA256_init: initialize the internal state of the hash function. Call this
  80. function before calling the SHA256_write function.
  81. */
  82. function SHA256_init() {
  83. SHA256_H = new Array(0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  84. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19);
  85. SHA256_buf = new Array();
  86. SHA256_len = 0;
  87. }
  88. /*
  89. SHA256_write: add a message fragment to the hash function's internal state.
  90. 'msg' may be given as string or as byte array and may have arbitrary length.
  91. */
  92. function SHA256_write(msg) {
  93. if (typeof(msg) == "string")
  94. SHA256_buf = SHA256_buf.concat(string_to_array(msg));
  95. else
  96. SHA256_buf = SHA256_buf.concat(msg);
  97. for(var i = 0; i + 64 <= SHA256_buf.length; i += 64)
  98. SHA256_Hash_Byte_Block(SHA256_H, SHA256_buf.slice(i, i + 64));
  99. SHA256_buf = SHA256_buf.slice(i);
  100. SHA256_len += msg.length;
  101. }
  102. /*
  103. SHA256_finalize: finalize the hash value calculation. Call this function
  104. after the last call to SHA256_write. An array of 32 bytes (= 256 bits)
  105. is returned.
  106. */
  107. function SHA256_finalize() {
  108. SHA256_buf[SHA256_buf.length] = 0x80;
  109. if (SHA256_buf.length > 64 - 8) {
  110. for(var i = SHA256_buf.length; i < 64; i++)
  111. SHA256_buf[i] = 0;
  112. SHA256_Hash_Byte_Block(SHA256_H, SHA256_buf);
  113. SHA256_buf.length = 0;
  114. }
  115. for(var i = SHA256_buf.length; i < 64 - 5; i++)
  116. SHA256_buf[i] = 0;
  117. SHA256_buf[59] = (SHA256_len >>> 29) & 0xff;
  118. SHA256_buf[60] = (SHA256_len >>> 21) & 0xff;
  119. SHA256_buf[61] = (SHA256_len >>> 13) & 0xff;
  120. SHA256_buf[62] = (SHA256_len >>> 5) & 0xff;
  121. SHA256_buf[63] = (SHA256_len << 3) & 0xff;
  122. SHA256_Hash_Byte_Block(SHA256_H, SHA256_buf);
  123. var res = new Array(32);
  124. for(var i = 0; i < 8; i++) {
  125. res[4 * i + 0] = SHA256_H[i] >>> 24;
  126. res[4 * i + 1] = (SHA256_H[i] >> 16) & 0xff;
  127. res[4 * i + 2] = (SHA256_H[i] >> 8) & 0xff;
  128. res[4 * i + 3] = SHA256_H[i] & 0xff;
  129. }
  130. delete SHA256_H;
  131. delete SHA256_buf;
  132. delete SHA256_len;
  133. return res;
  134. }
  135. /*
  136. SHA256_hash: calculate the hash value of the string or byte array 'msg'
  137. and return it as hexadecimal string. This shortcut function may be more
  138. convenient than calling SHA256_init, SHA256_write, SHA256_finalize
  139. and array_to_hex_string explicitly.
  140. */
  141. function SHA256_hash(msg) {
  142. var res;
  143. SHA256_init();
  144. SHA256_write(msg);
  145. res = SHA256_finalize();
  146. return array_to_hex_string(res);
  147. }
  148. /******************************************************************************/
  149. /* The following are the HMAC-SHA256 routines */
  150. /*
  151. HMAC_SHA256_init: initialize the MAC's internal state. The MAC key 'key'
  152. may be given as string or as byte array and may have arbitrary length.
  153. */
  154. function HMAC_SHA256_init(key) {
  155. if (typeof(key) == "string")
  156. HMAC_SHA256_key = string_to_array(key);
  157. else
  158. HMAC_SHA256_key = new Array().concat(key);
  159. if (HMAC_SHA256_key.length > 64) {
  160. SHA256_init();
  161. SHA256_write(HMAC_SHA256_key);
  162. HMAC_SHA256_key = SHA256_finalize();
  163. }
  164. for(var i = HMAC_SHA256_key.length; i < 64; i++)
  165. HMAC_SHA256_key[i] = 0;
  166. for(var i = 0; i < 64; i++)
  167. HMAC_SHA256_key[i] ^= 0x36;
  168. SHA256_init();
  169. SHA256_write(HMAC_SHA256_key);
  170. }
  171. /*
  172. HMAC_SHA256_write: process a message fragment. 'msg' may be given as
  173. string or as byte array and may have arbitrary length.
  174. */
  175. function HMAC_SHA256_write(msg) {
  176. SHA256_write(msg);
  177. }
  178. /*
  179. HMAC_SHA256_finalize: finalize the HMAC calculation. An array of 32 bytes
  180. (= 256 bits) is returned.
  181. */
  182. function HMAC_SHA256_finalize() {
  183. var md = SHA256_finalize();
  184. for(var i = 0; i < 64; i++)
  185. HMAC_SHA256_key[i] ^= 0x36 ^ 0x5c;
  186. SHA256_init();
  187. SHA256_write(HMAC_SHA256_key);
  188. SHA256_write(md);
  189. for(var i = 0; i < 64; i++)
  190. HMAC_SHA256_key[i] = 0;
  191. delete HMAC_SHA256_key;
  192. return SHA256_finalize();
  193. }
  194. /*
  195. HMAC_SHA256_MAC: calculate the HMAC value of message 'msg' under key 'key'
  196. (both may be of type string or byte array); return the MAC as hexadecimal
  197. string. This shortcut function may be more convenient than calling
  198. HMAC_SHA256_init, HMAC_SHA256_write, HMAC_SHA256_finalize and
  199. array_to_hex_string explicitly.
  200. */
  201. function HMAC_SHA256_MAC(key, msg) {
  202. var res;
  203. HMAC_SHA256_init(key);
  204. HMAC_SHA256_write(msg);
  205. res = HMAC_SHA256_finalize()
  206. return array_to_hex_string(res);
  207. }
  208. /******************************************************************************/
  209. /* The following lookup tables and functions are for internal use only! */
  210. SHA256_hexchars = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  211. 'a', 'b', 'c', 'd', 'e', 'f');
  212. SHA256_K = new Array(
  213. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
  214. 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  215. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
  216. 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  217. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
  218. 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  219. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
  220. 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  221. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
  222. 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  223. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  224. );
  225. function SHA256_sigma0(x) {
  226. return ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3);
  227. }
  228. function SHA256_sigma1(x) {
  229. return ((x >>> 17) | (x << 15)) ^ ((x >>> 19) | (x << 13)) ^ (x >>> 10);
  230. }
  231. function SHA256_Sigma0(x) {
  232. return ((x >>> 2) | (x << 30)) ^ ((x >>> 13) | (x << 19)) ^
  233. ((x >>> 22) | (x << 10));
  234. }
  235. function SHA256_Sigma1(x) {
  236. return ((x >>> 6) | (x << 26)) ^ ((x >>> 11) | (x << 21)) ^
  237. ((x >>> 25) | (x << 7));
  238. }
  239. function SHA256_Ch(x, y, z) {
  240. return z ^ (x & (y ^ z));
  241. }
  242. function SHA256_Maj(x, y, z) {
  243. return (x & y) ^ (z & (x ^ y));
  244. }
  245. function SHA256_Hash_Word_Block(H, W) {
  246. for(var i = 16; i < 64; i++)
  247. W[i] = (SHA256_sigma1(W[i - 2]) + W[i - 7] +
  248. SHA256_sigma0(W[i - 15]) + W[i - 16]) & 0xffffffff;
  249. var state = new Array().concat(H);
  250. for(var i = 0; i < 64; i++) {
  251. var T1 = state[7] + SHA256_Sigma1(state[4]) +
  252. SHA256_Ch(state[4], state[5], state[6]) + SHA256_K[i] + W[i];
  253. var T2 = SHA256_Sigma0(state[0]) + SHA256_Maj(state[0], state[1], state[2]);
  254. state.pop();
  255. state.unshift((T1 + T2) & 0xffffffff);
  256. state[4] = (state[4] + T1) & 0xffffffff;
  257. }
  258. for(var i = 0; i < 8; i++)
  259. H[i] = (H[i] + state[i]) & 0xffffffff;
  260. }
  261. function SHA256_Hash_Byte_Block(H, w) {
  262. var W = new Array(16);
  263. for(var i = 0; i < 16; i++)
  264. W[i] = w[4 * i + 0] << 24 | w[4 * i + 1] << 16 |
  265. w[4 * i + 2] << 8 | w[4 * i + 3];
  266. SHA256_Hash_Word_Block(H, W);
  267. }