md5.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import {
  2. WordArray,
  3. Hasher,
  4. } from './core.js';
  5. // Constants table
  6. const T = [];
  7. // Compute constants
  8. for (let i = 0; i < 64; i += 1) {
  9. T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0;
  10. }
  11. const FF = (a, b, c, d, x, s, t) => {
  12. const n = a + ((b & c) | (~b & d)) + x + t;
  13. return ((n << s) | (n >>> (32 - s))) + b;
  14. };
  15. const GG = (a, b, c, d, x, s, t) => {
  16. const n = a + ((b & d) | (c & ~d)) + x + t;
  17. return ((n << s) | (n >>> (32 - s))) + b;
  18. };
  19. const HH = (a, b, c, d, x, s, t) => {
  20. const n = a + (b ^ c ^ d) + x + t;
  21. return ((n << s) | (n >>> (32 - s))) + b;
  22. };
  23. const II = (a, b, c, d, x, s, t) => {
  24. const n = a + (c ^ (b | ~d)) + x + t;
  25. return ((n << s) | (n >>> (32 - s))) + b;
  26. };
  27. /**
  28. * MD5 hash algorithm.
  29. */
  30. export class MD5Algo extends Hasher {
  31. _doReset() {
  32. this._hash = new WordArray([
  33. 0x67452301,
  34. 0xefcdab89,
  35. 0x98badcfe,
  36. 0x10325476,
  37. ]);
  38. }
  39. _doProcessBlock(M, offset) {
  40. const _M = M;
  41. // Swap endian
  42. for (let i = 0; i < 16; i += 1) {
  43. // Shortcuts
  44. const offset_i = offset + i;
  45. const M_offset_i = M[offset_i];
  46. _M[offset_i] = (
  47. (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff)
  48. | (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
  49. );
  50. }
  51. // Shortcuts
  52. const H = this._hash.words;
  53. const M_offset_0 = _M[offset + 0];
  54. const M_offset_1 = _M[offset + 1];
  55. const M_offset_2 = _M[offset + 2];
  56. const M_offset_3 = _M[offset + 3];
  57. const M_offset_4 = _M[offset + 4];
  58. const M_offset_5 = _M[offset + 5];
  59. const M_offset_6 = _M[offset + 6];
  60. const M_offset_7 = _M[offset + 7];
  61. const M_offset_8 = _M[offset + 8];
  62. const M_offset_9 = _M[offset + 9];
  63. const M_offset_10 = _M[offset + 10];
  64. const M_offset_11 = _M[offset + 11];
  65. const M_offset_12 = _M[offset + 12];
  66. const M_offset_13 = _M[offset + 13];
  67. const M_offset_14 = _M[offset + 14];
  68. const M_offset_15 = _M[offset + 15];
  69. // Working varialbes
  70. let a = H[0];
  71. let b = H[1];
  72. let c = H[2];
  73. let d = H[3];
  74. // Computation
  75. a = FF(a, b, c, d, M_offset_0, 7, T[0]);
  76. d = FF(d, a, b, c, M_offset_1, 12, T[1]);
  77. c = FF(c, d, a, b, M_offset_2, 17, T[2]);
  78. b = FF(b, c, d, a, M_offset_3, 22, T[3]);
  79. a = FF(a, b, c, d, M_offset_4, 7, T[4]);
  80. d = FF(d, a, b, c, M_offset_5, 12, T[5]);
  81. c = FF(c, d, a, b, M_offset_6, 17, T[6]);
  82. b = FF(b, c, d, a, M_offset_7, 22, T[7]);
  83. a = FF(a, b, c, d, M_offset_8, 7, T[8]);
  84. d = FF(d, a, b, c, M_offset_9, 12, T[9]);
  85. c = FF(c, d, a, b, M_offset_10, 17, T[10]);
  86. b = FF(b, c, d, a, M_offset_11, 22, T[11]);
  87. a = FF(a, b, c, d, M_offset_12, 7, T[12]);
  88. d = FF(d, a, b, c, M_offset_13, 12, T[13]);
  89. c = FF(c, d, a, b, M_offset_14, 17, T[14]);
  90. b = FF(b, c, d, a, M_offset_15, 22, T[15]);
  91. a = GG(a, b, c, d, M_offset_1, 5, T[16]);
  92. d = GG(d, a, b, c, M_offset_6, 9, T[17]);
  93. c = GG(c, d, a, b, M_offset_11, 14, T[18]);
  94. b = GG(b, c, d, a, M_offset_0, 20, T[19]);
  95. a = GG(a, b, c, d, M_offset_5, 5, T[20]);
  96. d = GG(d, a, b, c, M_offset_10, 9, T[21]);
  97. c = GG(c, d, a, b, M_offset_15, 14, T[22]);
  98. b = GG(b, c, d, a, M_offset_4, 20, T[23]);
  99. a = GG(a, b, c, d, M_offset_9, 5, T[24]);
  100. d = GG(d, a, b, c, M_offset_14, 9, T[25]);
  101. c = GG(c, d, a, b, M_offset_3, 14, T[26]);
  102. b = GG(b, c, d, a, M_offset_8, 20, T[27]);
  103. a = GG(a, b, c, d, M_offset_13, 5, T[28]);
  104. d = GG(d, a, b, c, M_offset_2, 9, T[29]);
  105. c = GG(c, d, a, b, M_offset_7, 14, T[30]);
  106. b = GG(b, c, d, a, M_offset_12, 20, T[31]);
  107. a = HH(a, b, c, d, M_offset_5, 4, T[32]);
  108. d = HH(d, a, b, c, M_offset_8, 11, T[33]);
  109. c = HH(c, d, a, b, M_offset_11, 16, T[34]);
  110. b = HH(b, c, d, a, M_offset_14, 23, T[35]);
  111. a = HH(a, b, c, d, M_offset_1, 4, T[36]);
  112. d = HH(d, a, b, c, M_offset_4, 11, T[37]);
  113. c = HH(c, d, a, b, M_offset_7, 16, T[38]);
  114. b = HH(b, c, d, a, M_offset_10, 23, T[39]);
  115. a = HH(a, b, c, d, M_offset_13, 4, T[40]);
  116. d = HH(d, a, b, c, M_offset_0, 11, T[41]);
  117. c = HH(c, d, a, b, M_offset_3, 16, T[42]);
  118. b = HH(b, c, d, a, M_offset_6, 23, T[43]);
  119. a = HH(a, b, c, d, M_offset_9, 4, T[44]);
  120. d = HH(d, a, b, c, M_offset_12, 11, T[45]);
  121. c = HH(c, d, a, b, M_offset_15, 16, T[46]);
  122. b = HH(b, c, d, a, M_offset_2, 23, T[47]);
  123. a = II(a, b, c, d, M_offset_0, 6, T[48]);
  124. d = II(d, a, b, c, M_offset_7, 10, T[49]);
  125. c = II(c, d, a, b, M_offset_14, 15, T[50]);
  126. b = II(b, c, d, a, M_offset_5, 21, T[51]);
  127. a = II(a, b, c, d, M_offset_12, 6, T[52]);
  128. d = II(d, a, b, c, M_offset_3, 10, T[53]);
  129. c = II(c, d, a, b, M_offset_10, 15, T[54]);
  130. b = II(b, c, d, a, M_offset_1, 21, T[55]);
  131. a = II(a, b, c, d, M_offset_8, 6, T[56]);
  132. d = II(d, a, b, c, M_offset_15, 10, T[57]);
  133. c = II(c, d, a, b, M_offset_6, 15, T[58]);
  134. b = II(b, c, d, a, M_offset_13, 21, T[59]);
  135. a = II(a, b, c, d, M_offset_4, 6, T[60]);
  136. d = II(d, a, b, c, M_offset_11, 10, T[61]);
  137. c = II(c, d, a, b, M_offset_2, 15, T[62]);
  138. b = II(b, c, d, a, M_offset_9, 21, T[63]);
  139. // Intermediate hash value
  140. H[0] = (H[0] + a) | 0;
  141. H[1] = (H[1] + b) | 0;
  142. H[2] = (H[2] + c) | 0;
  143. H[3] = (H[3] + d) | 0;
  144. }
  145. /* eslint-ensable no-param-reassign */
  146. _doFinalize() {
  147. // Shortcuts
  148. const data = this._data;
  149. const dataWords = data.words;
  150. const nBitsTotal = this._nDataBytes * 8;
  151. const nBitsLeft = data.sigBytes * 8;
  152. // Add padding
  153. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - (nBitsLeft % 32));
  154. const nBitsTotalH = Math.floor(nBitsTotal / 0x100000000);
  155. const nBitsTotalL = nBitsTotal;
  156. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = (
  157. (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff)
  158. | (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00)
  159. );
  160. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
  161. (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff)
  162. | (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00)
  163. );
  164. data.sigBytes = (dataWords.length + 1) * 4;
  165. // Hash final blocks
  166. this._process();
  167. // Shortcuts
  168. const hash = this._hash;
  169. const H = hash.words;
  170. // Swap endian
  171. for (let i = 0; i < 4; i += 1) {
  172. // Shortcut
  173. const H_i = H[i];
  174. H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff)
  175. | (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
  176. }
  177. // Return final computed hash
  178. return hash;
  179. }
  180. clone() {
  181. const clone = super.clone.call(this);
  182. clone._hash = this._hash.clone();
  183. return clone;
  184. }
  185. }
  186. /**
  187. * Shortcut function to the hasher's object interface.
  188. *
  189. * @param {WordArray|string} message The message to hash.
  190. *
  191. * @return {WordArray} The hash.
  192. *
  193. * @static
  194. *
  195. * @example
  196. *
  197. * var hash = CryptoJS.MD5('message');
  198. * var hash = CryptoJS.MD5(wordArray);
  199. */
  200. export const MD5 = Hasher._createHelper(MD5Algo);
  201. /**
  202. * Shortcut function to the HMAC's object interface.
  203. *
  204. * @param {WordArray|string} message The message to hash.
  205. * @param {WordArray|string} key The secret key.
  206. *
  207. * @return {WordArray} The HMAC.
  208. *
  209. * @static
  210. *
  211. * @example
  212. *
  213. * var hmac = CryptoJS.HmacMD5(message, key);
  214. */
  215. export const HmacMD5 = Hasher._createHmacHelper(MD5Algo);