sha512.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. ;(function (root, factory, undef) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"), require("./x64-core"));
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. // AMD
  8. define(["./core", "./x64-core"], factory);
  9. }
  10. else {
  11. // Global (browser)
  12. factory(root.CryptoJS);
  13. }
  14. }(this, function (CryptoJS) {
  15. (function () {
  16. // Shortcuts
  17. var C = CryptoJS;
  18. var C_lib = C.lib;
  19. var Hasher = C_lib.Hasher;
  20. var C_x64 = C.x64;
  21. var X64Word = C_x64.Word;
  22. var X64WordArray = C_x64.WordArray;
  23. var C_algo = C.algo;
  24. function X64Word_create() {
  25. return X64Word.create.apply(X64Word, arguments);
  26. }
  27. // Constants
  28. var K = [
  29. X64Word_create(0x428a2f98, 0xd728ae22), X64Word_create(0x71374491, 0x23ef65cd),
  30. X64Word_create(0xb5c0fbcf, 0xec4d3b2f), X64Word_create(0xe9b5dba5, 0x8189dbbc),
  31. X64Word_create(0x3956c25b, 0xf348b538), X64Word_create(0x59f111f1, 0xb605d019),
  32. X64Word_create(0x923f82a4, 0xaf194f9b), X64Word_create(0xab1c5ed5, 0xda6d8118),
  33. X64Word_create(0xd807aa98, 0xa3030242), X64Word_create(0x12835b01, 0x45706fbe),
  34. X64Word_create(0x243185be, 0x4ee4b28c), X64Word_create(0x550c7dc3, 0xd5ffb4e2),
  35. X64Word_create(0x72be5d74, 0xf27b896f), X64Word_create(0x80deb1fe, 0x3b1696b1),
  36. X64Word_create(0x9bdc06a7, 0x25c71235), X64Word_create(0xc19bf174, 0xcf692694),
  37. X64Word_create(0xe49b69c1, 0x9ef14ad2), X64Word_create(0xefbe4786, 0x384f25e3),
  38. X64Word_create(0x0fc19dc6, 0x8b8cd5b5), X64Word_create(0x240ca1cc, 0x77ac9c65),
  39. X64Word_create(0x2de92c6f, 0x592b0275), X64Word_create(0x4a7484aa, 0x6ea6e483),
  40. X64Word_create(0x5cb0a9dc, 0xbd41fbd4), X64Word_create(0x76f988da, 0x831153b5),
  41. X64Word_create(0x983e5152, 0xee66dfab), X64Word_create(0xa831c66d, 0x2db43210),
  42. X64Word_create(0xb00327c8, 0x98fb213f), X64Word_create(0xbf597fc7, 0xbeef0ee4),
  43. X64Word_create(0xc6e00bf3, 0x3da88fc2), X64Word_create(0xd5a79147, 0x930aa725),
  44. X64Word_create(0x06ca6351, 0xe003826f), X64Word_create(0x14292967, 0x0a0e6e70),
  45. X64Word_create(0x27b70a85, 0x46d22ffc), X64Word_create(0x2e1b2138, 0x5c26c926),
  46. X64Word_create(0x4d2c6dfc, 0x5ac42aed), X64Word_create(0x53380d13, 0x9d95b3df),
  47. X64Word_create(0x650a7354, 0x8baf63de), X64Word_create(0x766a0abb, 0x3c77b2a8),
  48. X64Word_create(0x81c2c92e, 0x47edaee6), X64Word_create(0x92722c85, 0x1482353b),
  49. X64Word_create(0xa2bfe8a1, 0x4cf10364), X64Word_create(0xa81a664b, 0xbc423001),
  50. X64Word_create(0xc24b8b70, 0xd0f89791), X64Word_create(0xc76c51a3, 0x0654be30),
  51. X64Word_create(0xd192e819, 0xd6ef5218), X64Word_create(0xd6990624, 0x5565a910),
  52. X64Word_create(0xf40e3585, 0x5771202a), X64Word_create(0x106aa070, 0x32bbd1b8),
  53. X64Word_create(0x19a4c116, 0xb8d2d0c8), X64Word_create(0x1e376c08, 0x5141ab53),
  54. X64Word_create(0x2748774c, 0xdf8eeb99), X64Word_create(0x34b0bcb5, 0xe19b48a8),
  55. X64Word_create(0x391c0cb3, 0xc5c95a63), X64Word_create(0x4ed8aa4a, 0xe3418acb),
  56. X64Word_create(0x5b9cca4f, 0x7763e373), X64Word_create(0x682e6ff3, 0xd6b2b8a3),
  57. X64Word_create(0x748f82ee, 0x5defb2fc), X64Word_create(0x78a5636f, 0x43172f60),
  58. X64Word_create(0x84c87814, 0xa1f0ab72), X64Word_create(0x8cc70208, 0x1a6439ec),
  59. X64Word_create(0x90befffa, 0x23631e28), X64Word_create(0xa4506ceb, 0xde82bde9),
  60. X64Word_create(0xbef9a3f7, 0xb2c67915), X64Word_create(0xc67178f2, 0xe372532b),
  61. X64Word_create(0xca273ece, 0xea26619c), X64Word_create(0xd186b8c7, 0x21c0c207),
  62. X64Word_create(0xeada7dd6, 0xcde0eb1e), X64Word_create(0xf57d4f7f, 0xee6ed178),
  63. X64Word_create(0x06f067aa, 0x72176fba), X64Word_create(0x0a637dc5, 0xa2c898a6),
  64. X64Word_create(0x113f9804, 0xbef90dae), X64Word_create(0x1b710b35, 0x131c471b),
  65. X64Word_create(0x28db77f5, 0x23047d84), X64Word_create(0x32caab7b, 0x40c72493),
  66. X64Word_create(0x3c9ebe0a, 0x15c9bebc), X64Word_create(0x431d67c4, 0x9c100d4c),
  67. X64Word_create(0x4cc5d4be, 0xcb3e42b6), X64Word_create(0x597f299c, 0xfc657e2a),
  68. X64Word_create(0x5fcb6fab, 0x3ad6faec), X64Word_create(0x6c44198c, 0x4a475817)
  69. ];
  70. // Reusable objects
  71. var W = [];
  72. (function () {
  73. for (var i = 0; i < 80; i++) {
  74. W[i] = X64Word_create();
  75. }
  76. }());
  77. /**
  78. * SHA-512 hash algorithm.
  79. */
  80. var SHA512 = C_algo.SHA512 = Hasher.extend({
  81. _doReset: function () {
  82. this._hash = new X64WordArray.init([
  83. new X64Word.init(0x6a09e667, 0xf3bcc908), new X64Word.init(0xbb67ae85, 0x84caa73b),
  84. new X64Word.init(0x3c6ef372, 0xfe94f82b), new X64Word.init(0xa54ff53a, 0x5f1d36f1),
  85. new X64Word.init(0x510e527f, 0xade682d1), new X64Word.init(0x9b05688c, 0x2b3e6c1f),
  86. new X64Word.init(0x1f83d9ab, 0xfb41bd6b), new X64Word.init(0x5be0cd19, 0x137e2179)
  87. ]);
  88. },
  89. _doProcessBlock: function (M, offset) {
  90. // Shortcuts
  91. var H = this._hash.words;
  92. var H0 = H[0];
  93. var H1 = H[1];
  94. var H2 = H[2];
  95. var H3 = H[3];
  96. var H4 = H[4];
  97. var H5 = H[5];
  98. var H6 = H[6];
  99. var H7 = H[7];
  100. var H0h = H0.high;
  101. var H0l = H0.low;
  102. var H1h = H1.high;
  103. var H1l = H1.low;
  104. var H2h = H2.high;
  105. var H2l = H2.low;
  106. var H3h = H3.high;
  107. var H3l = H3.low;
  108. var H4h = H4.high;
  109. var H4l = H4.low;
  110. var H5h = H5.high;
  111. var H5l = H5.low;
  112. var H6h = H6.high;
  113. var H6l = H6.low;
  114. var H7h = H7.high;
  115. var H7l = H7.low;
  116. // Working variables
  117. var ah = H0h;
  118. var al = H0l;
  119. var bh = H1h;
  120. var bl = H1l;
  121. var ch = H2h;
  122. var cl = H2l;
  123. var dh = H3h;
  124. var dl = H3l;
  125. var eh = H4h;
  126. var el = H4l;
  127. var fh = H5h;
  128. var fl = H5l;
  129. var gh = H6h;
  130. var gl = H6l;
  131. var hh = H7h;
  132. var hl = H7l;
  133. // Rounds
  134. for (var i = 0; i < 80; i++) {
  135. // Shortcut
  136. var Wi = W[i];
  137. // Extend message
  138. if (i < 16) {
  139. var Wih = Wi.high = M[offset + i * 2] | 0;
  140. var Wil = Wi.low = M[offset + i * 2 + 1] | 0;
  141. } else {
  142. // Gamma0
  143. var gamma0x = W[i - 15];
  144. var gamma0xh = gamma0x.high;
  145. var gamma0xl = gamma0x.low;
  146. var gamma0h = ((gamma0xh >>> 1) | (gamma0xl << 31)) ^ ((gamma0xh >>> 8) | (gamma0xl << 24)) ^ (gamma0xh >>> 7);
  147. var gamma0l = ((gamma0xl >>> 1) | (gamma0xh << 31)) ^ ((gamma0xl >>> 8) | (gamma0xh << 24)) ^ ((gamma0xl >>> 7) | (gamma0xh << 25));
  148. // Gamma1
  149. var gamma1x = W[i - 2];
  150. var gamma1xh = gamma1x.high;
  151. var gamma1xl = gamma1x.low;
  152. var gamma1h = ((gamma1xh >>> 19) | (gamma1xl << 13)) ^ ((gamma1xh << 3) | (gamma1xl >>> 29)) ^ (gamma1xh >>> 6);
  153. var gamma1l = ((gamma1xl >>> 19) | (gamma1xh << 13)) ^ ((gamma1xl << 3) | (gamma1xh >>> 29)) ^ ((gamma1xl >>> 6) | (gamma1xh << 26));
  154. // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
  155. var Wi7 = W[i - 7];
  156. var Wi7h = Wi7.high;
  157. var Wi7l = Wi7.low;
  158. var Wi16 = W[i - 16];
  159. var Wi16h = Wi16.high;
  160. var Wi16l = Wi16.low;
  161. var Wil = gamma0l + Wi7l;
  162. var Wih = gamma0h + Wi7h + ((Wil >>> 0) < (gamma0l >>> 0) ? 1 : 0);
  163. var Wil = Wil + gamma1l;
  164. var Wih = Wih + gamma1h + ((Wil >>> 0) < (gamma1l >>> 0) ? 1 : 0);
  165. var Wil = Wil + Wi16l;
  166. var Wih = Wih + Wi16h + ((Wil >>> 0) < (Wi16l >>> 0) ? 1 : 0);
  167. Wi.high = Wih;
  168. Wi.low = Wil;
  169. }
  170. var chh = (eh & fh) ^ (~eh & gh);
  171. var chl = (el & fl) ^ (~el & gl);
  172. var majh = (ah & bh) ^ (ah & ch) ^ (bh & ch);
  173. var majl = (al & bl) ^ (al & cl) ^ (bl & cl);
  174. var sigma0h = ((ah >>> 28) | (al << 4)) ^ ((ah << 30) | (al >>> 2)) ^ ((ah << 25) | (al >>> 7));
  175. var sigma0l = ((al >>> 28) | (ah << 4)) ^ ((al << 30) | (ah >>> 2)) ^ ((al << 25) | (ah >>> 7));
  176. var sigma1h = ((eh >>> 14) | (el << 18)) ^ ((eh >>> 18) | (el << 14)) ^ ((eh << 23) | (el >>> 9));
  177. var sigma1l = ((el >>> 14) | (eh << 18)) ^ ((el >>> 18) | (eh << 14)) ^ ((el << 23) | (eh >>> 9));
  178. // t1 = h + sigma1 + ch + K[i] + W[i]
  179. var Ki = K[i];
  180. var Kih = Ki.high;
  181. var Kil = Ki.low;
  182. var t1l = hl + sigma1l;
  183. var t1h = hh + sigma1h + ((t1l >>> 0) < (hl >>> 0) ? 1 : 0);
  184. var t1l = t1l + chl;
  185. var t1h = t1h + chh + ((t1l >>> 0) < (chl >>> 0) ? 1 : 0);
  186. var t1l = t1l + Kil;
  187. var t1h = t1h + Kih + ((t1l >>> 0) < (Kil >>> 0) ? 1 : 0);
  188. var t1l = t1l + Wil;
  189. var t1h = t1h + Wih + ((t1l >>> 0) < (Wil >>> 0) ? 1 : 0);
  190. // t2 = sigma0 + maj
  191. var t2l = sigma0l + majl;
  192. var t2h = sigma0h + majh + ((t2l >>> 0) < (sigma0l >>> 0) ? 1 : 0);
  193. // Update working variables
  194. hh = gh;
  195. hl = gl;
  196. gh = fh;
  197. gl = fl;
  198. fh = eh;
  199. fl = el;
  200. el = (dl + t1l) | 0;
  201. eh = (dh + t1h + ((el >>> 0) < (dl >>> 0) ? 1 : 0)) | 0;
  202. dh = ch;
  203. dl = cl;
  204. ch = bh;
  205. cl = bl;
  206. bh = ah;
  207. bl = al;
  208. al = (t1l + t2l) | 0;
  209. ah = (t1h + t2h + ((al >>> 0) < (t1l >>> 0) ? 1 : 0)) | 0;
  210. }
  211. // Intermediate hash value
  212. H0l = H0.low = (H0l + al);
  213. H0.high = (H0h + ah + ((H0l >>> 0) < (al >>> 0) ? 1 : 0));
  214. H1l = H1.low = (H1l + bl);
  215. H1.high = (H1h + bh + ((H1l >>> 0) < (bl >>> 0) ? 1 : 0));
  216. H2l = H2.low = (H2l + cl);
  217. H2.high = (H2h + ch + ((H2l >>> 0) < (cl >>> 0) ? 1 : 0));
  218. H3l = H3.low = (H3l + dl);
  219. H3.high = (H3h + dh + ((H3l >>> 0) < (dl >>> 0) ? 1 : 0));
  220. H4l = H4.low = (H4l + el);
  221. H4.high = (H4h + eh + ((H4l >>> 0) < (el >>> 0) ? 1 : 0));
  222. H5l = H5.low = (H5l + fl);
  223. H5.high = (H5h + fh + ((H5l >>> 0) < (fl >>> 0) ? 1 : 0));
  224. H6l = H6.low = (H6l + gl);
  225. H6.high = (H6h + gh + ((H6l >>> 0) < (gl >>> 0) ? 1 : 0));
  226. H7l = H7.low = (H7l + hl);
  227. H7.high = (H7h + hh + ((H7l >>> 0) < (hl >>> 0) ? 1 : 0));
  228. },
  229. _doFinalize: function () {
  230. // Shortcuts
  231. var data = this._data;
  232. var dataWords = data.words;
  233. var nBitsTotal = this._nDataBytes * 8;
  234. var nBitsLeft = data.sigBytes * 8;
  235. // Add padding
  236. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  237. dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 30] = Math.floor(nBitsTotal / 0x100000000);
  238. dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 31] = nBitsTotal;
  239. data.sigBytes = dataWords.length * 4;
  240. // Hash final blocks
  241. this._process();
  242. // Convert hash to 32-bit word array before returning
  243. var hash = this._hash.toX32();
  244. // Return final computed hash
  245. return hash;
  246. },
  247. clone: function () {
  248. var clone = Hasher.clone.call(this);
  249. clone._hash = this._hash.clone();
  250. return clone;
  251. },
  252. blockSize: 1024/32
  253. });
  254. /**
  255. * Shortcut function to the hasher's object interface.
  256. *
  257. * @param {WordArray|string} message The message to hash.
  258. *
  259. * @return {WordArray} The hash.
  260. *
  261. * @static
  262. *
  263. * @example
  264. *
  265. * var hash = CryptoJS.SHA512('message');
  266. * var hash = CryptoJS.SHA512(wordArray);
  267. */
  268. C.SHA512 = Hasher._createHelper(SHA512);
  269. /**
  270. * Shortcut function to the HMAC's object interface.
  271. *
  272. * @param {WordArray|string} message The message to hash.
  273. * @param {WordArray|string} key The secret key.
  274. *
  275. * @return {WordArray} The HMAC.
  276. *
  277. * @static
  278. *
  279. * @example
  280. *
  281. * var hmac = CryptoJS.HmacSHA512(message, key);
  282. */
  283. C.HmacSHA512 = Hasher._createHmacHelper(SHA512);
  284. }());
  285. return CryptoJS.SHA512;
  286. }));