md5.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. function md5(string) {
  2. var rotateLeft = function (lValue, iShiftBits) {
  3. return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
  4. }
  5. var addUnsigned = function (lX, lY) {
  6. var lX4, lY4, lX8, lY8, lResult;
  7. lX8 = (lX & 0x80000000);
  8. lY8 = (lY & 0x80000000);
  9. lX4 = (lX & 0x40000000);
  10. lY4 = (lY & 0x40000000);
  11. lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
  12. if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
  13. if (lX4 | lY4) {
  14. if (lResult & 0x40000000) return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
  15. else return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
  16. } else {
  17. return (lResult ^ lX8 ^ lY8);
  18. }
  19. }
  20. var F = function (x, y, z) {
  21. return (x & y) | ((~x) & z);
  22. }
  23. var G = function (x, y, z) {
  24. return (x & z) | (y & (~z));
  25. }
  26. var H = function (x, y, z) {
  27. return (x ^ y ^ z);
  28. }
  29. var I = function (x, y, z) {
  30. return (y ^ (x | (~z)));
  31. }
  32. var FF = function (a, b, c, d, x, s, ac) {
  33. a = addUnsigned(a, addUnsigned(addUnsigned(F(b, c, d), x), ac));
  34. return addUnsigned(rotateLeft(a, s), b);
  35. };
  36. var GG = function (a, b, c, d, x, s, ac) {
  37. a = addUnsigned(a, addUnsigned(addUnsigned(G(b, c, d), x), ac));
  38. return addUnsigned(rotateLeft(a, s), b);
  39. };
  40. var HH = function (a, b, c, d, x, s, ac) {
  41. a = addUnsigned(a, addUnsigned(addUnsigned(H(b, c, d), x), ac));
  42. return addUnsigned(rotateLeft(a, s), b);
  43. };
  44. var II = function (a, b, c, d, x, s, ac) {
  45. a = addUnsigned(a, addUnsigned(addUnsigned(I(b, c, d), x), ac));
  46. return addUnsigned(rotateLeft(a, s), b);
  47. };
  48. var convertToWordArray = function (string) {
  49. var lWordCount;
  50. var lMessageLength = string.length;
  51. var lNumberOfWordsTempOne = lMessageLength + 8;
  52. var lNumberOfWordsTempTwo = (lNumberOfWordsTempOne - (lNumberOfWordsTempOne % 64)) / 64;
  53. var lNumberOfWords = (lNumberOfWordsTempTwo + 1) * 16;
  54. var lWordArray = Array(lNumberOfWords - 1);
  55. var lBytePosition = 0;
  56. var lByteCount = 0;
  57. while (lByteCount < lMessageLength) {
  58. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  59. lBytePosition = (lByteCount % 4) * 8;
  60. lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition));
  61. lByteCount++;
  62. }
  63. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  64. lBytePosition = (lByteCount % 4) * 8;
  65. lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
  66. lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
  67. lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
  68. return lWordArray;
  69. };
  70. var wordToHex = function (lValue) {
  71. var WordToHexValue = "",
  72. WordToHexValueTemp = "",
  73. lByte, lCount;
  74. for (lCount = 0; lCount <= 3; lCount++) {
  75. lByte = (lValue >>> (lCount * 8)) & 255;
  76. WordToHexValueTemp = "0" + lByte.toString(16);
  77. WordToHexValue = WordToHexValue + WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2);
  78. }
  79. return WordToHexValue;
  80. };
  81. var uTF8Encode = function (string) {
  82. string = string.replace(/\x0d\x0a/g, "\x0a");
  83. var output = "";
  84. for (var n = 0; n < string.length; n++) {
  85. var c = string.charCodeAt(n);
  86. if (c < 128) {
  87. output += String.fromCharCode(c);
  88. } else if ((c > 127) && (c < 2048)) {
  89. output += String.fromCharCode((c >> 6) | 192);
  90. output += String.fromCharCode((c & 63) | 128);
  91. } else {
  92. output += String.fromCharCode((c >> 12) | 224);
  93. output += String.fromCharCode(((c >> 6) & 63) | 128);
  94. output += String.fromCharCode((c & 63) | 128);
  95. }
  96. }
  97. return output;
  98. };
  99. var x = Array();
  100. var k, AA, BB, CC, DD, a, b, c, d;
  101. var S11 = 7,
  102. S12 = 12,
  103. S13 = 17,
  104. S14 = 22;
  105. var S21 = 5,
  106. S22 = 9,
  107. S23 = 14,
  108. S24 = 20;
  109. var S31 = 4,
  110. S32 = 11,
  111. S33 = 16,
  112. S34 = 23;
  113. var S41 = 6,
  114. S42 = 10,
  115. S43 = 15,
  116. S44 = 21;
  117. string = uTF8Encode(string);
  118. x = convertToWordArray(string);
  119. a = 0x67452301;
  120. b = 0xEFCDAB89;
  121. c = 0x98BADCFE;
  122. d = 0x10325476;
  123. for (k = 0; k < x.length; k += 16) {
  124. AA = a;
  125. BB = b;
  126. CC = c;
  127. DD = d;
  128. a = FF(a, b, c, d, x[k + 0], S11, 0xD76AA478);
  129. d = FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756);
  130. c = FF(c, d, a, b, x[k + 2], S13, 0x242070DB);
  131. b = FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
  132. a = FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF);
  133. d = FF(d, a, b, c, x[k + 5], S12, 0x4787C62A);
  134. c = FF(c, d, a, b, x[k + 6], S13, 0xA8304613);
  135. b = FF(b, c, d, a, x[k + 7], S14, 0xFD469501);
  136. a = FF(a, b, c, d, x[k + 8], S11, 0x698098D8);
  137. d = FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
  138. c = FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
  139. b = FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
  140. a = FF(a, b, c, d, x[k + 12], S11, 0x6B901122);
  141. d = FF(d, a, b, c, x[k + 13], S12, 0xFD987193);
  142. c = FF(c, d, a, b, x[k + 14], S13, 0xA679438E);
  143. b = FF(b, c, d, a, x[k + 15], S14, 0x49B40821);
  144. a = GG(a, b, c, d, x[k + 1], S21, 0xF61E2562);
  145. d = GG(d, a, b, c, x[k + 6], S22, 0xC040B340);
  146. c = GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);
  147. b = GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
  148. a = GG(a, b, c, d, x[k + 5], S21, 0xD62F105D);
  149. d = GG(d, a, b, c, x[k + 10], S22, 0x2441453);
  150. c = GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
  151. b = GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
  152. a = GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6);
  153. d = GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);
  154. c = GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87);
  155. b = GG(b, c, d, a, x[k + 8], S24, 0x455A14ED);
  156. a = GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905);
  157. d = GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
  158. c = GG(c, d, a, b, x[k + 7], S23, 0x676F02D9);
  159. b = GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
  160. a = HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942);
  161. d = HH(d, a, b, c, x[k + 8], S32, 0x8771F681);
  162. c = HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
  163. b = HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
  164. a = HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44);
  165. d = HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
  166. c = HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
  167. b = HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
  168. a = HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6);
  169. d = HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA);
  170. c = HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085);
  171. b = HH(b, c, d, a, x[k + 6], S34, 0x4881D05);
  172. a = HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039);
  173. d = HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
  174. c = HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
  175. b = HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665);
  176. a = II(a, b, c, d, x[k + 0], S41, 0xF4292244);
  177. d = II(d, a, b, c, x[k + 7], S42, 0x432AFF97);
  178. c = II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
  179. b = II(b, c, d, a, x[k + 5], S44, 0xFC93A039);
  180. a = II(a, b, c, d, x[k + 12], S41, 0x655B59C3);
  181. d = II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
  182. c = II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
  183. b = II(b, c, d, a, x[k + 1], S44, 0x85845DD1);
  184. a = II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F);
  185. d = II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
  186. c = II(c, d, a, b, x[k + 6], S43, 0xA3014314);
  187. b = II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
  188. a = II(a, b, c, d, x[k + 4], S41, 0xF7537E82);
  189. d = II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
  190. c = II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
  191. b = II(b, c, d, a, x[k + 9], S44, 0xEB86D391);
  192. a = addUnsigned(a, AA);
  193. b = addUnsigned(b, BB);
  194. c = addUnsigned(c, CC);
  195. d = addUnsigned(d, DD);
  196. }
  197. var tempValue = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d);
  198. return tempValue.toLowerCase();
  199. }
  200. module.exports = {
  201. md5: md5
  202. }