w_md5.js 9.1 KB

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