md5.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. ;(function (root, factory) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"));
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. // AMD
  8. define(["./core"], factory);
  9. }
  10. else {
  11. // Global (browser)
  12. factory(root.CryptoJS);
  13. }
  14. }(this, function (CryptoJS) {
  15. (function (Math) {
  16. // Shortcuts
  17. var C = CryptoJS;
  18. var C_lib = C.lib;
  19. var WordArray = C_lib.WordArray;
  20. var Hasher = C_lib.Hasher;
  21. var C_algo = C.algo;
  22. // Constants table
  23. var T = [];
  24. // Compute constants
  25. (function () {
  26. for (var i = 0; i < 64; i++) {
  27. T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0;
  28. }
  29. }());
  30. /**
  31. * MD5 hash algorithm.
  32. */
  33. var MD5 = C_algo.MD5 = Hasher.extend({
  34. _doReset: function () {
  35. this._hash = new WordArray.init([
  36. 0x67452301, 0xefcdab89,
  37. 0x98badcfe, 0x10325476
  38. ]);
  39. },
  40. _doProcessBlock: function (M, offset) {
  41. // Swap endian
  42. for (var i = 0; i < 16; i++) {
  43. // Shortcuts
  44. var offset_i = offset + i;
  45. var 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. var H = this._hash.words;
  53. var M_offset_0 = M[offset + 0];
  54. var M_offset_1 = M[offset + 1];
  55. var M_offset_2 = M[offset + 2];
  56. var M_offset_3 = M[offset + 3];
  57. var M_offset_4 = M[offset + 4];
  58. var M_offset_5 = M[offset + 5];
  59. var M_offset_6 = M[offset + 6];
  60. var M_offset_7 = M[offset + 7];
  61. var M_offset_8 = M[offset + 8];
  62. var M_offset_9 = M[offset + 9];
  63. var M_offset_10 = M[offset + 10];
  64. var M_offset_11 = M[offset + 11];
  65. var M_offset_12 = M[offset + 12];
  66. var M_offset_13 = M[offset + 13];
  67. var M_offset_14 = M[offset + 14];
  68. var M_offset_15 = M[offset + 15];
  69. // Working variables
  70. var a = H[0];
  71. var b = H[1];
  72. var c = H[2];
  73. var 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. _doFinalize: function () {
  146. // Shortcuts
  147. var data = this._data;
  148. var dataWords = data.words;
  149. var nBitsTotal = this._nDataBytes * 8;
  150. var nBitsLeft = data.sigBytes * 8;
  151. // Add padding
  152. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  153. var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000);
  154. var nBitsTotalL = nBitsTotal;
  155. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = (
  156. (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) |
  157. (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00)
  158. );
  159. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
  160. (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) |
  161. (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00)
  162. );
  163. data.sigBytes = (dataWords.length + 1) * 4;
  164. // Hash final blocks
  165. this._process();
  166. // Shortcuts
  167. var hash = this._hash;
  168. var H = hash.words;
  169. // Swap endian
  170. for (var i = 0; i < 4; i++) {
  171. // Shortcut
  172. var H_i = H[i];
  173. H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
  174. (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
  175. }
  176. // Return final computed hash
  177. return hash;
  178. },
  179. clone: function () {
  180. var clone = Hasher.clone.call(this);
  181. clone._hash = this._hash.clone();
  182. return clone;
  183. }
  184. });
  185. function FF(a, b, c, d, x, s, t) {
  186. var n = a + ((b & c) | (~b & d)) + x + t;
  187. return ((n << s) | (n >>> (32 - s))) + b;
  188. }
  189. function GG(a, b, c, d, x, s, t) {
  190. var n = a + ((b & d) | (c & ~d)) + x + t;
  191. return ((n << s) | (n >>> (32 - s))) + b;
  192. }
  193. function HH(a, b, c, d, x, s, t) {
  194. var n = a + (b ^ c ^ d) + x + t;
  195. return ((n << s) | (n >>> (32 - s))) + b;
  196. }
  197. function II(a, b, c, d, x, s, t) {
  198. var n = a + (c ^ (b | ~d)) + x + t;
  199. return ((n << s) | (n >>> (32 - s))) + b;
  200. }
  201. /**
  202. * Shortcut function to the hasher's object interface.
  203. *
  204. * @param {WordArray|string} message The message to hash.
  205. *
  206. * @return {WordArray} The hash.
  207. *
  208. * @static
  209. *
  210. * @example
  211. *
  212. * var hash = CryptoJS.MD5('message');
  213. * var hash = CryptoJS.MD5(wordArray);
  214. */
  215. C.MD5 = Hasher._createHelper(MD5);
  216. /**
  217. * Shortcut function to the HMAC's object interface.
  218. *
  219. * @param {WordArray|string} message The message to hash.
  220. * @param {WordArray|string} key The secret key.
  221. *
  222. * @return {WordArray} The HMAC.
  223. *
  224. * @static
  225. *
  226. * @example
  227. *
  228. * var hmac = CryptoJS.HmacMD5(message, key);
  229. */
  230. C.HmacMD5 = Hasher._createHmacHelper(MD5);
  231. }(Math));
  232. return CryptoJS.MD5;
  233. }));