js MD5 实现()

  1 function md5(string) {
  2     /* 预备一个数组,用来存储范围为 0-32 bits 的数字 */
  3     var x = Array();
  4 
  5     /*
  6      * k:表示轮次,每 16 个范围为 0-32 bits 的数字为一轮
  7      * AA、BB、CC、DD:用来存储幻数在上一轮变化的最终结果
  8      * a、b、c、d:表示四个幻数,在每一轮的处理中都在不断变化
  9      */
 10     var k, AA, BB, CC, DD, a, b, c, d;
 11 
 12     /* 每个幻数都有四个位移量,依次使用 */
 13 
 14     /* a 的位移量 */
 15     var S11 = 7,
 16         S12 = 12,
 17         S13 = 17,
 18         S14 = 22;
 19     /* b 的位移量 */
 20     var S21 = 5,
 21         S22 = 9,
 22         S23 = 14,
 23         S24 = 20;
 24     /* c 的位移量 */
 25     var S31 = 4,
 26         S32 = 11,
 27         S33 = 16,
 28         S34 = 23;
 29     /* d 的位移量 */
 30     var S41 = 6,
 31         S42 = 10,
 32         S43 = 15,
 33         S44 = 21;
 34 
 35     /* 原始字符转为单字节字符 */
 36     string = md5_Utf8Encode(string);
 37 
 38     /* 范围为 0-32 bits 的数字组成的数组 */
 39     x = md5_ConvertToWordArray(string);
 40 
 41     /* 四个幻数,用大尾序表示 */
 42     a = 0x67452301;
 43     b = 0xefcdab89;
 44     c = 0x98badcfe;
 45     d = 0x10325476;
 46 
 47     /* 每 16 个范围为 0-32 bits 的数字为一轮 */
 48     for (k = 0; k < x.length; k += 16) {
 49         /* 保存幻数在上一轮变化的结果 */
 50         AA = a;
 51         BB = b;
 52         CC = c;
 53         DD = d;
 54 
 55         /* 每个数字都要交给 md5_FF 处理一次 */
 56         a = md5_FF(a, b, c, d, x[k + 0], S11, 0xd76aa478);
 57         d = md5_FF(d, a, b, c, x[k + 1], S12, 0xe8c7b756);
 58         c = md5_FF(c, d, a, b, x[k + 2], S13, 0x242070db);
 59         b = md5_FF(b, c, d, a, x[k + 3], S14, 0xc1bdceee);
 60         a = md5_FF(a, b, c, d, x[k + 4], S11, 0xf57c0faf);
 61         d = md5_FF(d, a, b, c, x[k + 5], S12, 0x4787c62a);
 62         c = md5_FF(c, d, a, b, x[k + 6], S13, 0xa8304613);
 63         b = md5_FF(b, c, d, a, x[k + 7], S14, 0xfd469501);
 64         a = md5_FF(a, b, c, d, x[k + 8], S11, 0x698098d8);
 65         d = md5_FF(d, a, b, c, x[k + 9], S12, 0x8b44f7af);
 66         c = md5_FF(c, d, a, b, x[k + 10], S13, 0xffff5bb1);
 67         b = md5_FF(b, c, d, a, x[k + 11], S14, 0x895cd7be);
 68         a = md5_FF(a, b, c, d, x[k + 12], S11, 0x6b901122);
 69         d = md5_FF(d, a, b, c, x[k + 13], S12, 0xfd987193);
 70         c = md5_FF(c, d, a, b, x[k + 14], S13, 0xa679438e);
 71         b = md5_FF(b, c, d, a, x[k + 15], S14, 0x49b40821);
 72         /* 每个数字再交给 md5_GG 处理一次 */
 73         a = md5_GG(a, b, c, d, x[k + 1], S21, 0xf61e2562);
 74         d = md5_GG(d, a, b, c, x[k + 6], S22, 0xc040b340);
 75         c = md5_GG(c, d, a, b, x[k + 11], S23, 0x265e5a51);
 76         b = md5_GG(b, c, d, a, x[k + 0], S24, 0xe9b6c7aa);
 77         a = md5_GG(a, b, c, d, x[k + 5], S21, 0xd62f105d);
 78         d = md5_GG(d, a, b, c, x[k + 10], S22, 0x2441453);
 79         c = md5_GG(c, d, a, b, x[k + 15], S23, 0xd8a1e681);
 80         b = md5_GG(b, c, d, a, x[k + 4], S24, 0xe7d3fbc8);
 81         a = md5_GG(a, b, c, d, x[k + 9], S21, 0x21e1cde6);
 82         d = md5_GG(d, a, b, c, x[k + 14], S22, 0xc33707d6);
 83         c = md5_GG(c, d, a, b, x[k + 3], S23, 0xf4d50d87);
 84         b = md5_GG(b, c, d, a, x[k + 8], S24, 0x455a14ed);
 85         a = md5_GG(a, b, c, d, x[k + 13], S21, 0xa9e3e905);
 86         d = md5_GG(d, a, b, c, x[k + 2], S22, 0xfcefa3f8);
 87         c = md5_GG(c, d, a, b, x[k + 7], S23, 0x676f02d9);
 88         b = md5_GG(b, c, d, a, x[k + 12], S24, 0x8d2a4c8a);
 89         /* 每个数字再交给 md5_HH 处理一次 */
 90         a = md5_HH(a, b, c, d, x[k + 5], S31, 0xfffa3942);
 91         d = md5_HH(d, a, b, c, x[k + 8], S32, 0x8771f681);
 92         c = md5_HH(c, d, a, b, x[k + 11], S33, 0x6d9d6122);
 93         b = md5_HH(b, c, d, a, x[k + 14], S34, 0xfde5380c);
 94         a = md5_HH(a, b, c, d, x[k + 1], S31, 0xa4beea44);
 95         d = md5_HH(d, a, b, c, x[k + 4], S32, 0x4bdecfa9);
 96         c = md5_HH(c, d, a, b, x[k + 7], S33, 0xf6bb4b60);
 97         b = md5_HH(b, c, d, a, x[k + 10], S34, 0xbebfbc70);
 98         a = md5_HH(a, b, c, d, x[k + 13], S31, 0x289b7ec6);
 99         d = md5_HH(d, a, b, c, x[k + 0], S32, 0xeaa127fa);
100         c = md5_HH(c, d, a, b, x[k + 3], S33, 0xd4ef3085);
101         b = md5_HH(b, c, d, a, x[k + 6], S34, 0x4881d05);
102         a = md5_HH(a, b, c, d, x[k + 9], S31, 0xd9d4d039);
103         d = md5_HH(d, a, b, c, x[k + 12], S32, 0xe6db99e5);
104         c = md5_HH(c, d, a, b, x[k + 15], S33, 0x1fa27cf8);
105         b = md5_HH(b, c, d, a, x[k + 2], S34, 0xc4ac5665);
106         /* 每个数字再交给 md5_II 处理一次 */
107         a = md5_II(a, b, c, d, x[k + 0], S41, 0xf4292244);
108         d = md5_II(d, a, b, c, x[k + 7], S42, 0x432aff97);
109         c = md5_II(c, d, a, b, x[k + 14], S43, 0xab9423a7);
110         b = md5_II(b, c, d, a, x[k + 5], S44, 0xfc93a039);
111         a = md5_II(a, b, c, d, x[k + 12], S41, 0x655b59c3);
112         d = md5_II(d, a, b, c, x[k + 3], S42, 0x8f0ccc92);
113         c = md5_II(c, d, a, b, x[k + 10], S43, 0xffeff47d);
114         b = md5_II(b, c, d, a, x[k + 1], S44, 0x85845dd1);
115         a = md5_II(a, b, c, d, x[k + 8], S41, 0x6fa87e4f);
116         d = md5_II(d, a, b, c, x[k + 15], S42, 0xfe2ce6e0);
117         c = md5_II(c, d, a, b, x[k + 6], S43, 0xa3014314);
118         b = md5_II(b, c, d, a, x[k + 13], S44, 0x4e0811a1);
119         a = md5_II(a, b, c, d, x[k + 4], S41, 0xf7537e82);
120         d = md5_II(d, a, b, c, x[k + 11], S42, 0xbd3af235);
121         c = md5_II(c, d, a, b, x[k + 2], S43, 0x2ad7d2bb);
122         b = md5_II(b, c, d, a, x[k + 9], S44, 0xeb86d391);
123         /* 得到四个新的幻数,交给下一轮处理 */
124         a = md5_AddUnsigned(a, AA);
125         b = md5_AddUnsigned(b, BB);
126         c = md5_AddUnsigned(c, CC);
127         d = md5_AddUnsigned(d, DD);
128     }
129     function md5_AddUnsigned(lX, lY) {
130         var lX4, lY4, lX8, lY8, lResult;
131         lX8 = lX & 0x80000000;
132         lY8 = lY & 0x80000000;
133         lX4 = lX & 0x40000000;
134         lY4 = lY & 0x40000000;
135         lResult = (lX & 0x3fffffff) + (lY & 0x3fffffff);
136         if (lX4 & lY4) {
137             return lResult ^ 0x80000000 ^ lX8 ^ lY8;
138         }
139         if (lX4 | lY4) {
140             if (lResult & 0x40000000) {
141                 return lResult ^ 0xc0000000 ^ lX8 ^ lY8;
142             } else {
143                 return lResult ^ 0x40000000 ^ lX8 ^ lY8;
144             }
145         } else {
146             return lResult ^ lX8 ^ lY8;
147         }
148     }
149     function md5_RotateLeft(lValue, iShiftBits) {
150         return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
151     }
152     /* 非线性函数 */
153     function md5_F(x, y, z) {
154         return (x & y) | (~x & z);
155     }
156     function md5_G(x, y, z) {
157         return (x & z) | (y & ~z);
158     }
159     function md5_H(x, y, z) {
160         return x ^ y ^ z;
161     }
162     function md5_I(x, y, z) {
163         return y ^ (x | ~z);
164     }
165     /* 幻数处理 */
166     function md5_FF(a, b, c, d, x, s, ac) {
167         a = md5_AddUnsigned(
168             a,
169             md5_AddUnsigned(md5_AddUnsigned(md5_F(b, c, d), x), ac),
170         );
171         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
172     }
173     function md5_GG(a, b, c, d, x, s, ac) {
174         a = md5_AddUnsigned(
175             a,
176             md5_AddUnsigned(md5_AddUnsigned(md5_G(b, c, d), x), ac),
177         );
178         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
179     }
180     function md5_HH(a, b, c, d, x, s, ac) {
181         a = md5_AddUnsigned(
182             a,
183             md5_AddUnsigned(md5_AddUnsigned(md5_H(b, c, d), x), ac),
184         );
185         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
186     }
187     function md5_II(a, b, c, d, x, s, ac) {
188         a = md5_AddUnsigned(
189             a,
190             md5_AddUnsigned(md5_AddUnsigned(md5_I(b, c, d), x), ac),
191         );
192         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
193     }
194 
195     /* 返回范围为 0-32 bits 的数字组成的数组 */
196     function md5_ConvertToWordArray(string) {
197         var lWordCount;
198         var lMessageLength = string.length;
199         var lNumberOfWords_temp1 = lMessageLength + 8;
200         var lNumberOfWords_temp2 =
201             (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
202         var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
203         var lWordArray = Array(lNumberOfWords - 1);
204         var lBytePosition = 0;
205         var lByteCount = 0;
206         while (lByteCount < lMessageLength) {
207             lWordCount = (lByteCount - (lByteCount % 4)) / 4;
208             lBytePosition = (lByteCount % 4) * 8;
209             lWordArray[lWordCount] =
210                 lWordArray[lWordCount] |
211                 (string.charCodeAt(lByteCount) << lBytePosition);
212             lByteCount++;
213         }
214         lWordCount = (lByteCount - (lByteCount % 4)) / 4;
215         lBytePosition = (lByteCount % 4) * 8;
216         lWordArray[lWordCount] =
217             lWordArray[lWordCount] | (0x80 << lBytePosition);
218         lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
219         lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
220         return lWordArray;
221     }
222 
223     /* 返回单字节字符拼接的字符串;多字节字符分成单字节字符拼接 */
224     function md5_Utf8Encode(string) {
225         string = string.replace(/\r\n/g, "\n");
226         var utftext = "";
227         for (var n = 0; n < string.length; n++) {
228             var c = string.charCodeAt(n);
229             if (c < 128) {
230                 // ASCII 可以直接返回
231                 utftext += String.fromCharCode(c);
232             } else if (c > 127 && c < 2048) {
233                 utftext += String.fromCharCode((c >> 6) | 192);
234                 utftext += String.fromCharCode((c & 63) | 128);
235             } else {
236                 utftext += String.fromCharCode((c >> 12) | 224);
237                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
238                 utftext += String.fromCharCode((c & 63) | 128);
239             }
240         }
241         return utftext;
242     }
243 
244     /* 返回用 16 进制表示的数值字符串 */
245     function md5_WordToHex(lValue) {
246         var WordToHexValue = "",
247             WordToHexValue_temp = "",
248             lByte,
249             lCount;
250         for (lCount = 0; lCount <= 3; lCount++) {
251             lByte = (lValue >>> (lCount * 8)) & 255;
252             WordToHexValue_temp = "0" + lByte.toString(16);
253             WordToHexValue =
254                 WordToHexValue +
255                 WordToHexValue_temp.substr(
256                     WordToHexValue_temp.length - 2,
257                     2,
258                 );
259         }
260         return WordToHexValue;
261     }
262     return (
263         md5_WordToHex(a) +
264         md5_WordToHex(b) +
265         md5_WordToHex(c) +
266         md5_WordToHex(d)
267     ).toLowerCase();
268 }
————————
  1 function md5(string) {
  2     /* 预备一个数组,用来存储范围为 0-32 bits 的数字 */
  3     var x = Array();
  4 
  5     /*
  6      * k:表示轮次,每 16 个范围为 0-32 bits 的数字为一轮
  7      * AA、BB、CC、DD:用来存储幻数在上一轮变化的最终结果
  8      * a、b、c、d:表示四个幻数,在每一轮的处理中都在不断变化
  9      */
 10     var k, AA, BB, CC, DD, a, b, c, d;
 11 
 12     /* 每个幻数都有四个位移量,依次使用 */
 13 
 14     /* a 的位移量 */
 15     var S11 = 7,
 16         S12 = 12,
 17         S13 = 17,
 18         S14 = 22;
 19     /* b 的位移量 */
 20     var S21 = 5,
 21         S22 = 9,
 22         S23 = 14,
 23         S24 = 20;
 24     /* c 的位移量 */
 25     var S31 = 4,
 26         S32 = 11,
 27         S33 = 16,
 28         S34 = 23;
 29     /* d 的位移量 */
 30     var S41 = 6,
 31         S42 = 10,
 32         S43 = 15,
 33         S44 = 21;
 34 
 35     /* 原始字符转为单字节字符 */
 36     string = md5_Utf8Encode(string);
 37 
 38     /* 范围为 0-32 bits 的数字组成的数组 */
 39     x = md5_ConvertToWordArray(string);
 40 
 41     /* 四个幻数,用大尾序表示 */
 42     a = 0x67452301;
 43     b = 0xefcdab89;
 44     c = 0x98badcfe;
 45     d = 0x10325476;
 46 
 47     /* 每 16 个范围为 0-32 bits 的数字为一轮 */
 48     for (k = 0; k < x.length; k += 16) {
 49         /* 保存幻数在上一轮变化的结果 */
 50         AA = a;
 51         BB = b;
 52         CC = c;
 53         DD = d;
 54 
 55         /* 每个数字都要交给 md5_FF 处理一次 */
 56         a = md5_FF(a, b, c, d, x[k + 0], S11, 0xd76aa478);
 57         d = md5_FF(d, a, b, c, x[k + 1], S12, 0xe8c7b756);
 58         c = md5_FF(c, d, a, b, x[k + 2], S13, 0x242070db);
 59         b = md5_FF(b, c, d, a, x[k + 3], S14, 0xc1bdceee);
 60         a = md5_FF(a, b, c, d, x[k + 4], S11, 0xf57c0faf);
 61         d = md5_FF(d, a, b, c, x[k + 5], S12, 0x4787c62a);
 62         c = md5_FF(c, d, a, b, x[k + 6], S13, 0xa8304613);
 63         b = md5_FF(b, c, d, a, x[k + 7], S14, 0xfd469501);
 64         a = md5_FF(a, b, c, d, x[k + 8], S11, 0x698098d8);
 65         d = md5_FF(d, a, b, c, x[k + 9], S12, 0x8b44f7af);
 66         c = md5_FF(c, d, a, b, x[k + 10], S13, 0xffff5bb1);
 67         b = md5_FF(b, c, d, a, x[k + 11], S14, 0x895cd7be);
 68         a = md5_FF(a, b, c, d, x[k + 12], S11, 0x6b901122);
 69         d = md5_FF(d, a, b, c, x[k + 13], S12, 0xfd987193);
 70         c = md5_FF(c, d, a, b, x[k + 14], S13, 0xa679438e);
 71         b = md5_FF(b, c, d, a, x[k + 15], S14, 0x49b40821);
 72         /* 每个数字再交给 md5_GG 处理一次 */
 73         a = md5_GG(a, b, c, d, x[k + 1], S21, 0xf61e2562);
 74         d = md5_GG(d, a, b, c, x[k + 6], S22, 0xc040b340);
 75         c = md5_GG(c, d, a, b, x[k + 11], S23, 0x265e5a51);
 76         b = md5_GG(b, c, d, a, x[k + 0], S24, 0xe9b6c7aa);
 77         a = md5_GG(a, b, c, d, x[k + 5], S21, 0xd62f105d);
 78         d = md5_GG(d, a, b, c, x[k + 10], S22, 0x2441453);
 79         c = md5_GG(c, d, a, b, x[k + 15], S23, 0xd8a1e681);
 80         b = md5_GG(b, c, d, a, x[k + 4], S24, 0xe7d3fbc8);
 81         a = md5_GG(a, b, c, d, x[k + 9], S21, 0x21e1cde6);
 82         d = md5_GG(d, a, b, c, x[k + 14], S22, 0xc33707d6);
 83         c = md5_GG(c, d, a, b, x[k + 3], S23, 0xf4d50d87);
 84         b = md5_GG(b, c, d, a, x[k + 8], S24, 0x455a14ed);
 85         a = md5_GG(a, b, c, d, x[k + 13], S21, 0xa9e3e905);
 86         d = md5_GG(d, a, b, c, x[k + 2], S22, 0xfcefa3f8);
 87         c = md5_GG(c, d, a, b, x[k + 7], S23, 0x676f02d9);
 88         b = md5_GG(b, c, d, a, x[k + 12], S24, 0x8d2a4c8a);
 89         /* 每个数字再交给 md5_HH 处理一次 */
 90         a = md5_HH(a, b, c, d, x[k + 5], S31, 0xfffa3942);
 91         d = md5_HH(d, a, b, c, x[k + 8], S32, 0x8771f681);
 92         c = md5_HH(c, d, a, b, x[k + 11], S33, 0x6d9d6122);
 93         b = md5_HH(b, c, d, a, x[k + 14], S34, 0xfde5380c);
 94         a = md5_HH(a, b, c, d, x[k + 1], S31, 0xa4beea44);
 95         d = md5_HH(d, a, b, c, x[k + 4], S32, 0x4bdecfa9);
 96         c = md5_HH(c, d, a, b, x[k + 7], S33, 0xf6bb4b60);
 97         b = md5_HH(b, c, d, a, x[k + 10], S34, 0xbebfbc70);
 98         a = md5_HH(a, b, c, d, x[k + 13], S31, 0x289b7ec6);
 99         d = md5_HH(d, a, b, c, x[k + 0], S32, 0xeaa127fa);
100         c = md5_HH(c, d, a, b, x[k + 3], S33, 0xd4ef3085);
101         b = md5_HH(b, c, d, a, x[k + 6], S34, 0x4881d05);
102         a = md5_HH(a, b, c, d, x[k + 9], S31, 0xd9d4d039);
103         d = md5_HH(d, a, b, c, x[k + 12], S32, 0xe6db99e5);
104         c = md5_HH(c, d, a, b, x[k + 15], S33, 0x1fa27cf8);
105         b = md5_HH(b, c, d, a, x[k + 2], S34, 0xc4ac5665);
106         /* 每个数字再交给 md5_II 处理一次 */
107         a = md5_II(a, b, c, d, x[k + 0], S41, 0xf4292244);
108         d = md5_II(d, a, b, c, x[k + 7], S42, 0x432aff97);
109         c = md5_II(c, d, a, b, x[k + 14], S43, 0xab9423a7);
110         b = md5_II(b, c, d, a, x[k + 5], S44, 0xfc93a039);
111         a = md5_II(a, b, c, d, x[k + 12], S41, 0x655b59c3);
112         d = md5_II(d, a, b, c, x[k + 3], S42, 0x8f0ccc92);
113         c = md5_II(c, d, a, b, x[k + 10], S43, 0xffeff47d);
114         b = md5_II(b, c, d, a, x[k + 1], S44, 0x85845dd1);
115         a = md5_II(a, b, c, d, x[k + 8], S41, 0x6fa87e4f);
116         d = md5_II(d, a, b, c, x[k + 15], S42, 0xfe2ce6e0);
117         c = md5_II(c, d, a, b, x[k + 6], S43, 0xa3014314);
118         b = md5_II(b, c, d, a, x[k + 13], S44, 0x4e0811a1);
119         a = md5_II(a, b, c, d, x[k + 4], S41, 0xf7537e82);
120         d = md5_II(d, a, b, c, x[k + 11], S42, 0xbd3af235);
121         c = md5_II(c, d, a, b, x[k + 2], S43, 0x2ad7d2bb);
122         b = md5_II(b, c, d, a, x[k + 9], S44, 0xeb86d391);
123         /* 得到四个新的幻数,交给下一轮处理 */
124         a = md5_AddUnsigned(a, AA);
125         b = md5_AddUnsigned(b, BB);
126         c = md5_AddUnsigned(c, CC);
127         d = md5_AddUnsigned(d, DD);
128     }
129     function md5_AddUnsigned(lX, lY) {
130         var lX4, lY4, lX8, lY8, lResult;
131         lX8 = lX & 0x80000000;
132         lY8 = lY & 0x80000000;
133         lX4 = lX & 0x40000000;
134         lY4 = lY & 0x40000000;
135         lResult = (lX & 0x3fffffff) + (lY & 0x3fffffff);
136         if (lX4 & lY4) {
137             return lResult ^ 0x80000000 ^ lX8 ^ lY8;
138         }
139         if (lX4 | lY4) {
140             if (lResult & 0x40000000) {
141                 return lResult ^ 0xc0000000 ^ lX8 ^ lY8;
142             } else {
143                 return lResult ^ 0x40000000 ^ lX8 ^ lY8;
144             }
145         } else {
146             return lResult ^ lX8 ^ lY8;
147         }
148     }
149     function md5_RotateLeft(lValue, iShiftBits) {
150         return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
151     }
152     /* 非线性函数 */
153     function md5_F(x, y, z) {
154         return (x & y) | (~x & z);
155     }
156     function md5_G(x, y, z) {
157         return (x & z) | (y & ~z);
158     }
159     function md5_H(x, y, z) {
160         return x ^ y ^ z;
161     }
162     function md5_I(x, y, z) {
163         return y ^ (x | ~z);
164     }
165     /* 幻数处理 */
166     function md5_FF(a, b, c, d, x, s, ac) {
167         a = md5_AddUnsigned(
168             a,
169             md5_AddUnsigned(md5_AddUnsigned(md5_F(b, c, d), x), ac),
170         );
171         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
172     }
173     function md5_GG(a, b, c, d, x, s, ac) {
174         a = md5_AddUnsigned(
175             a,
176             md5_AddUnsigned(md5_AddUnsigned(md5_G(b, c, d), x), ac),
177         );
178         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
179     }
180     function md5_HH(a, b, c, d, x, s, ac) {
181         a = md5_AddUnsigned(
182             a,
183             md5_AddUnsigned(md5_AddUnsigned(md5_H(b, c, d), x), ac),
184         );
185         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
186     }
187     function md5_II(a, b, c, d, x, s, ac) {
188         a = md5_AddUnsigned(
189             a,
190             md5_AddUnsigned(md5_AddUnsigned(md5_I(b, c, d), x), ac),
191         );
192         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
193     }
194 
195     /* 返回范围为 0-32 bits 的数字组成的数组 */
196     function md5_ConvertToWordArray(string) {
197         var lWordCount;
198         var lMessageLength = string.length;
199         var lNumberOfWords_temp1 = lMessageLength + 8;
200         var lNumberOfWords_temp2 =
201             (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
202         var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
203         var lWordArray = Array(lNumberOfWords - 1);
204         var lBytePosition = 0;
205         var lByteCount = 0;
206         while (lByteCount < lMessageLength) {
207             lWordCount = (lByteCount - (lByteCount % 4)) / 4;
208             lBytePosition = (lByteCount % 4) * 8;
209             lWordArray[lWordCount] =
210                 lWordArray[lWordCount] |
211                 (string.charCodeAt(lByteCount) << lBytePosition);
212             lByteCount++;
213         }
214         lWordCount = (lByteCount - (lByteCount % 4)) / 4;
215         lBytePosition = (lByteCount % 4) * 8;
216         lWordArray[lWordCount] =
217             lWordArray[lWordCount] | (0x80 << lBytePosition);
218         lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
219         lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
220         return lWordArray;
221     }
222 
223     /* 返回单字节字符拼接的字符串;多字节字符分成单字节字符拼接 */
224     function md5_Utf8Encode(string) {
225         string = string.replace(/\r\n/g, "\n");
226         var utftext = "";
227         for (var n = 0; n < string.length; n++) {
228             var c = string.charCodeAt(n);
229             if (c < 128) {
230                 // ASCII 可以直接返回
231                 utftext += String.fromCharCode(c);
232             } else if (c > 127 && c < 2048) {
233                 utftext += String.fromCharCode((c >> 6) | 192);
234                 utftext += String.fromCharCode((c & 63) | 128);
235             } else {
236                 utftext += String.fromCharCode((c >> 12) | 224);
237                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
238                 utftext += String.fromCharCode((c & 63) | 128);
239             }
240         }
241         return utftext;
242     }
243 
244     /* 返回用 16 进制表示的数值字符串 */
245     function md5_WordToHex(lValue) {
246         var WordToHexValue = "",
247             WordToHexValue_temp = "",
248             lByte,
249             lCount;
250         for (lCount = 0; lCount <= 3; lCount++) {
251             lByte = (lValue >>> (lCount * 8)) & 255;
252             WordToHexValue_temp = "0" + lByte.toString(16);
253             WordToHexValue =
254                 WordToHexValue +
255                 WordToHexValue_temp.substr(
256                     WordToHexValue_temp.length - 2,
257                     2,
258                 );
259         }
260         return WordToHexValue;
261     }
262     return (
263         md5_WordToHex(a) +
264         md5_WordToHex(b) +
265         md5_WordToHex(c) +
266         md5_WordToHex(d)
267     ).toLowerCase();
268 }