PageSourceSearch

http://e-file.isenlin.cn/register/js/md5.js

js isenlin.cn collected 2026-10-02 07:38:55 UTC 12,121 bytes, 356 lines download raw bytes

1/*
2 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
3 * Digest Algorithm, as defined in RFC 1321.
4 * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
5 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
6 * Distributed under the BSD License
7 * See http://pajhome.org.uk/crypt/md5 for more info.
8 */
9
10/*
11 * Configurable variables. You may need to tweak these to be compatible with
12 * the server-side, but the defaults work in most cases.
13 */
14var hexcase = 0;   /* hex output format. 0 - lowercase; 1 - uppercase        */
15var b64pad = "";  /* base-64 pad character. "=" for strict RFC compliance   */
16
17/*
18 * These are the functions you'll usually want to call
19 * They take string arguments and return either hex or base-64 encoded strings
20 */
21function hex_md5(s) { return rstr2hex(rstr_md5(str2rstr_utf8(s))); }
22function b64_md5(s) { return rstr2b64(rstr_md5(str2rstr_utf8(s))); }
23function any_md5(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); }
24function hex_hmac_md5(k, d)
25{ return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
26function b64_hmac_md5(k, d)
27{ return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
28function any_hmac_md5(k, d, e)
29{ return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
30
31/*
32 * Perform a simple self-test to see if the VM is working
33 */
34function md5_vm_test() {
35  return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
36}
37
38/*
39 * Calculate the MD5 of a raw string
40 */
41function rstr_md5(s) {
42  return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
43}
44
45/*
46 * Calculate the HMAC-MD5, of a key and some data (raw strings)
47 */
48function rstr_hmac_md5(key, data) {
49  var bkey = rstr2binl(key);
50  if (bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);
51
52  var ipad = Array(16), opad = Array(16);
53  for (var i = 0; i < 16; i++) {
54    ipad[i] = bkey[i] ^ 0x36363636;
55    opad[i] = bkey[i] ^ 0x5C5C5C5C;
56  }
57
58  var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
59  return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
60}
61
62/*
63 * Convert a raw string to a hex string
64 */
65function rstr2hex(input) {
66  try { hexcase } catch (e) { hexcase = 0; }
67  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
68  var output = "";
69  var x;
70  for (var i = 0; i < input.length; i++) {
71    x = input.charCodeAt(i);
72    output += hex_tab.charAt((x >>> 4) & 0x0F)
73      + hex_tab.charAt(x & 0x0F);
74  }
75  return output;
76}
77
78/*
79 * Convert a raw string to a base-64 string
80 */
81function rstr2b64(input) {
82  try { b64pad } catch (e) { b64pad = ''; }
83  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
84  var output = "";
85  var len = input.length;
86  for (var i = 0; i < len; i += 3) {
87    var triplet = (input.charCodeAt(i) << 16)
88      | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0)
89      | (i + 2 < len ? input.charCodeAt(i + 2) : 0);
90    for (var j = 0; j < 4; j++) {
91      if (i * 8 + j * 6 > input.length * 8) output += b64pad;
92      else output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
93    }
94  }
95  return output;
96}
97
98/*
99 * Convert a raw string to an arbitrary string encoding
100 */
101function rstr2any(input, encoding) {
102  var divisor = encoding.length;
103  var i, j, q, x, quotient;
104
105  /* Convert to an array of 16-bit big-endian values, forming the dividend */
106  var dividend = Array(Math.ceil(input.length / 2));
107  for (i = 0; i < dividend.length; i++) {
108    dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
109  }
110
111  /*
112   * Repeatedly perform a long division. The binary array forms the dividend,
113   * the length of the encoding is the divisor. Once computed, the quotient
114   * forms the dividend for the next step. All remainders are stored for later
115   * use.
116   */
117  var full_length = Math.ceil(input.length * 8 /
118    (Math.log(encoding.length) / Math.log(2)));
119  var remainders = Array(full_length);
120  for (j = 0; j < full_length; j++) {
121    quotient = Array();
122    x = 0;
123    for (i = 0; i < dividend.length; i++) {
124      x = (x << 16) + dividend[i];
125      q = Math.floor(x / divisor);
126      x -= q * divisor;
127      if (quotient.length > 0 || q > 0)
128        quotient[quotient.length] = q;
129    }
130    remainders[j] = x;
131    dividend = quotient;
132  }
133
134  /* Convert the remainders to the output string */
135  var output = "";
136  for (i = remainders.length - 1; i >= 0; i--)
137    output += encoding.charAt(remainders[i]);
138
139  return output;
140}
141
142/*
143 * Encode a string as utf-8.
144 * For efficiency, this assumes the input is valid utf-16.
145 */
146function str2rstr_utf8(input) {
147  var output = "";
148  var i = -1;
149  var x, y;
150
151  while (++i < input.length) {
152    /* Decode utf-16 surrogate pairs */
153    x = input.charCodeAt(i);
154    y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
155    if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
156      x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
157      i++;
158    }
159
160    /* Encode output as utf-8 */
161    if (x <= 0x7F)
162      output += String.fromCharCode(x);
163    else if (x <= 0x7FF)
164      output += String.fromCharCode(0xC0 | ((x >>> 6) & 0x1F),
165        0x80 | (x & 0x3F));
166    else if (x <= 0xFFFF)
167      output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
168        0x80 | ((x >>> 6) & 0x3F),
169        0x80 | (x & 0x3F));
170    else if (x <= 0x1FFFFF)
171      output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
172        0x80 | ((x >>> 12) & 0x3F),
173        0x80 | ((x >>> 6) & 0x3F),
174        0x80 | (x & 0x3F));
175  }
176  return output;
177}
178
179/*
180 * Encode a string as utf-16
181 */
182function str2rstr_utf16le(input) {
183  var output = "";
184  for (var i = 0; i < input.length; i++)
185    output += String.fromCharCode(input.charCodeAt(i) & 0xFF,
186      (input.charCodeAt(i) >>> 8) & 0xFF);
187  return output;
188}
189
190function str2rstr_utf16be(input) {
191  var output = "";
192  for (var i = 0; i < input.length; i++)
193    output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
194      input.charCodeAt(i) & 0xFF);
195  return output;
196}
197
198/*
199 * Convert a raw string to an array of little-endian words
200 * Characters >255 have their high-byte silently ignored.
201 */
202function rstr2binl(input) {
203  var output = Array(input.length >> 2);
204  for (var i = 0; i < output.length; i++)
205    output[i] = 0;
206  for (var i = 0; i < input.length * 8; i += 8)
207    output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
208  return output;
209}
210
211/*
212 * Convert an array of little-endian words to a string
213 */
214function binl2rstr(input) {
215  var output = "";
216  for (var i = 0; i < input.length * 32; i += 8)
217    output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
218  return output;
219}
220
221/*
222 * Calculate the MD5 of an array of little-endian words, and a bit length.
223 */
224function binl_md5(x, len) {
225  /* append padding */
226  x[len >> 5] |= 0x80 << ((len) % 32);
227  x[(((len + 64) >>> 9) << 4) + 14] = len;
228
229  var a = 1732584193;
230  var b = -271733879;
231  var c = -1732584194;
232  var d = 271733878;
233
234  for (var i = 0; i < x.length; i += 16) {
235    var olda = a;
236    var oldb = b;
237    var oldc = c;
238    var oldd = d;
239
240    a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
241    d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
242    c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
243    b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
244    a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
245    d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
246    c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
247    b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
248    a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
249    d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
250    c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
251    b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
252    a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
253    d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
254    c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
255    b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
256
257    a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
258    d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
259    c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
260    b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
261    a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
262    d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
263    c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
vendor: 3,482 bytes, lines 264-354
264    b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
265    a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
266    d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
267    c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
268    b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
269    a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
270    d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
271    c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
272    b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
273
274    a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
275    d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
276    c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
277    b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
278    a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
279    d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
280    c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
281    b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
282    a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
283    d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
284    c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
285    b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
286    a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
287    d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
288    c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
289    b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
290
291    a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
292    d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
293    c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
294    b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
295    a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
296    d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
297    c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
298    b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
299    a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
300    d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
301    c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
302    b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
303    a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
304    d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
305    c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
306    b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
307
308    a = safe_add(a, olda);
309    b = safe_add(b, oldb);
310    c = safe_add(c, oldc);
311    d = safe_add(d, oldd);
312  }
313  return Array(a, b, c, d);
314}
315
316/*
317 * These functions implement the four basic operations the algorithm uses.
318 */
319function md5_cmn(q, a, b, x, s, t) {
320  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
321}
322function md5_ff(a, b, c, d, x, s, t) {
323  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
324}
325function md5_gg(a, b, c, d, x, s, t) {
326  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
327}
328function md5_hh(a, b, c, d, x, s, t) {
329  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
330}
331function md5_ii(a, b, c, d, x, s, t) {
332  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
333}
334
335/*
336 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
337 * to work around bugs in some JS interpreters.
338 */
339function safe_add(x, y) {
340  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
341  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
342  return (msw << 16) | (lsw & 0xFFFF);
343}
344/*
345 * Bitwise rotate a 32-bit number to the left.
346 */
347function bit_rol(num, cnt) {
348  return (num << cnt) | (num >>> (32 - cnt));
349}
350
351function hexMD5(str) {
352  return hex_md5(str);
353}
354
355// 将 hexMD5 函数添加到全局作用域(window 对象)
356window.hexMD5 = hexMD5;

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.