md5.js 9.8 KB

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