sha256.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*!
  2. * Crypto-JS v1.1.0
  3. * http://code.google.com/p/crypto-js/
  4. * Copyright (c) 2009, Jeff Mott. All rights reserved.
  5. * http://code.google.com/p/crypto-js/wiki/License
  6. */
  7. (function(){
  8. // Shortcut
  9. var util = Crypto.util;
  10. // Constants
  11. var K = [ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  12. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  13. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  14. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  15. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  16. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  17. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  18. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  19. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  20. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  21. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  22. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  23. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  24. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  25. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  26. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2 ];
  27. // Public API
  28. var SHA256 = Crypto.SHA256 = function (message, options) {
  29. var digestbytes = util.wordsToBytes(SHA256._sha256(message));
  30. return options && options.asBytes ? digestbytes :
  31. options && options.asString ? util.bytesToString(digestbytes) :
  32. util.bytesToHex(digestbytes);
  33. };
  34. // The core
  35. SHA256._sha256 = function (message) {
  36. var m = util.stringToWords(message),
  37. l = message.length * 8,
  38. H = [ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  39. 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 ],
  40. w = [],
  41. a, b, c, d, e, f, g, h, i, j,
  42. t1, t2;
  43. // Padding
  44. m[l >> 5] |= 0x80 << (24 - l % 32);
  45. m[((l + 64 >> 9) << 4) + 15] = l;
  46. for (var i = 0; i < m.length; i += 16) {
  47. a = H[0];
  48. b = H[1];
  49. c = H[2];
  50. d = H[3];
  51. e = H[4];
  52. f = H[5];
  53. g = H[6];
  54. h = H[7];
  55. for (var j = 0; j < 64; j++) {
  56. if (j < 16) w[j] = m[j + i];
  57. else {
  58. var gamma0x = w[j - 15],
  59. gamma1x = w[j - 2],
  60. gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
  61. ((gamma0x << 14) | (gamma0x >>> 18)) ^
  62. (gamma0x >>> 3),
  63. gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
  64. ((gamma1x << 13) | (gamma1x >>> 19)) ^
  65. (gamma1x >>> 10);
  66. w[j] = gamma0 + (w[j - 7] >>> 0) +
  67. gamma1 + (w[j - 16] >>> 0);
  68. }
  69. var ch = e & f ^ ~e & g,
  70. maj = a & b ^ a & c ^ b & c,
  71. sigma0 = ((a << 30) | (a >>> 2)) ^
  72. ((a << 19) | (a >>> 13)) ^
  73. ((a << 10) | (a >>> 22)),
  74. sigma1 = ((e << 26) | (e >>> 6)) ^
  75. ((e << 21) | (e >>> 11)) ^
  76. ((e << 7) | (e >>> 25));
  77. t1 = (h >>> 0) + sigma1 + ch + (K[j]) + (w[j] >>> 0);
  78. t2 = sigma0 + maj;
  79. h = g;
  80. g = f;
  81. f = e;
  82. e = d + t1;
  83. d = c;
  84. c = b;
  85. b = a;
  86. a = t1 + t2;
  87. }
  88. H[0] += a;
  89. H[1] += b;
  90. H[2] += c;
  91. H[3] += d;
  92. H[4] += e;
  93. H[5] += f;
  94. H[6] += g;
  95. H[7] += h;
  96. }
  97. return H;
  98. };
  99. // Package private blocksize
  100. SHA256._blocksize = 16;
  101. })();