Skip to content

Instantly share code, notes, and snippets.

@shangdev
Created July 22, 2020 01:00
Show Gist options
  • Save shangdev/af7468cd746106934ba179bf351bd376 to your computer and use it in GitHub Desktop.
Save shangdev/af7468cd746106934ba179bf351bd376 to your computer and use it in GitHub Desktop.
JS版MD5实现
function(e) {
var t = function(e, t) {
return e << t | e >>> 32 - t
}
, i = function(e, t) {
var i, n, a, o, s;
return a = 2147483648 & e,
o = 2147483648 & t,
i = 1073741824 & e,
n = 1073741824 & t,
s = (1073741823 & e) + (1073741823 & t),
i & n ? 2147483648 ^ s ^ a ^ o : i | n ? 1073741824 & s ? 3221225472 ^ s ^ a ^ o : 1073741824 ^ s ^ a ^ o : s ^ a ^ o
}
, n = function(e, t, i) {
return e & t | ~e & i
}
, a = function(e, t, i) {
return e & i | t & ~i
}
, o = function(e, t, i) {
return e ^ t ^ i
}
, s = function(e, t, i) {
return t ^ (e | ~i)
}
, r = function(e, a, o, s, r, l, c) {
return e = i(e, i(i(n(a, o, s), r), c)),
i(t(e, l), a)
}
, l = function(e, n, o, s, r, l, c) {
return e = i(e, i(i(a(n, o, s), r), c)),
i(t(e, l), n)
}
, c = function(e, n, a, s, r, l, c) {
return e = i(e, i(i(o(n, a, s), r), c)),
i(t(e, l), n)
}
, u = function(e, n, a, o, r, l, c) {
return e = i(e, i(i(s(n, a, o), r), c)),
i(t(e, l), n)
}
, d = function(e) {
for (var t, i = e.length, n = i + 8, a = (n - n % 64) / 64, o = 16 * (a + 1), s = Array(o - 1), r = 0, l = 0; l < i; )
t = (l - l % 4) / 4,
r = l % 4 * 8,
s[t] = s[t] | e.charCodeAt(l) << r,
l++;
return t = (l - l % 4) / 4,
r = l % 4 * 8,
s[t] = s[t] | 128 << r,
s[o - 2] = i << 3,
s[o - 1] = i >>> 29,
s
}
, h = function(e) {
var t, i, n = "", a = "";
for (i = 0; i <= 3; i++)
t = e >>> 8 * i & 255,
a = "0" + t.toString(16),
n += a.substr(a.length - 2, 2);
return n
}
, p = function(e) {
e = e.replace(/\x0d\x0a/g, "\n");
for (var t = "", i = 0; i < e.length; i++) {
var n = e.charCodeAt(i);
n < 128 ? t += String.fromCharCode(n) : n > 127 && n < 2048 ? (t += String.fromCharCode(n >> 6 | 192),
t += String.fromCharCode(63 & n | 128)) : (t += String.fromCharCode(n >> 12 | 224),
t += String.fromCharCode(n >> 6 & 63 | 128),
t += String.fromCharCode(63 & n | 128))
}
return t
};
e.extend({
md5: function(e) {
var t, n, a, o, s, f, m, g, v, b = Array(), y = 7, k = 12, $ = 17, w = 22, x = 5, _ = 9, C = 14, S = 20, j = 4, I = 11, T = 16, P = 23, E = 6, L = 10, B = 15, A = 21;
for (e = p(e),
b = d(e),
f = 1732584193,
m = 4023233417,
g = 2562383102,
v = 271733878,
t = 0; t < b.length; t += 16)
n = f,
a = m,
o = g,
s = v,
f = r(f, m, g, v, b[t + 0], y, 3614090360),
v = r(v, f, m, g, b[t + 1], k, 3905402710),
g = r(g, v, f, m, b[t + 2], $, 606105819),
m = r(m, g, v, f, b[t + 3], w, 3250441966),
f = r(f, m, g, v, b[t + 4], y, 4118548399),
v = r(v, f, m, g, b[t + 5], k, 1200080426),
g = r(g, v, f, m, b[t + 6], $, 2821735955),
m = r(m, g, v, f, b[t + 7], w, 4249261313),
f = r(f, m, g, v, b[t + 8], y, 1770035416),
v = r(v, f, m, g, b[t + 9], k, 2336552879),
g = r(g, v, f, m, b[t + 10], $, 4294925233),
m = r(m, g, v, f, b[t + 11], w, 2304563134),
f = r(f, m, g, v, b[t + 12], y, 1804603682),
v = r(v, f, m, g, b[t + 13], k, 4254626195),
g = r(g, v, f, m, b[t + 14], $, 2792965006),
m = r(m, g, v, f, b[t + 15], w, 1236535329),
f = l(f, m, g, v, b[t + 1], x, 4129170786),
v = l(v, f, m, g, b[t + 6], _, 3225465664),
g = l(g, v, f, m, b[t + 11], C, 643717713),
m = l(m, g, v, f, b[t + 0], S, 3921069994),
f = l(f, m, g, v, b[t + 5], x, 3593408605),
v = l(v, f, m, g, b[t + 10], _, 38016083),
g = l(g, v, f, m, b[t + 15], C, 3634488961),
m = l(m, g, v, f, b[t + 4], S, 3889429448),
f = l(f, m, g, v, b[t + 9], x, 568446438),
v = l(v, f, m, g, b[t + 14], _, 3275163606),
g = l(g, v, f, m, b[t + 3], C, 4107603335),
m = l(m, g, v, f, b[t + 8], S, 1163531501),
f = l(f, m, g, v, b[t + 13], x, 2850285829),
v = l(v, f, m, g, b[t + 2], _, 4243563512),
g = l(g, v, f, m, b[t + 7], C, 1735328473),
m = l(m, g, v, f, b[t + 12], S, 2368359562),
f = c(f, m, g, v, b[t + 5], j, 4294588738),
v = c(v, f, m, g, b[t + 8], I, 2272392833),
g = c(g, v, f, m, b[t + 11], T, 1839030562),
m = c(m, g, v, f, b[t + 14], P, 4259657740),
f = c(f, m, g, v, b[t + 1], j, 2763975236),
v = c(v, f, m, g, b[t + 4], I, 1272893353),
g = c(g, v, f, m, b[t + 7], T, 4139469664),
m = c(m, g, v, f, b[t + 10], P, 3200236656),
f = c(f, m, g, v, b[t + 13], j, 681279174),
v = c(v, f, m, g, b[t + 0], I, 3936430074),
g = c(g, v, f, m, b[t + 3], T, 3572445317),
m = c(m, g, v, f, b[t + 6], P, 76029189),
f = c(f, m, g, v, b[t + 9], j, 3654602809),
v = c(v, f, m, g, b[t + 12], I, 3873151461),
g = c(g, v, f, m, b[t + 15], T, 530742520),
m = c(m, g, v, f, b[t + 2], P, 3299628645),
f = u(f, m, g, v, b[t + 0], E, 4096336452),
v = u(v, f, m, g, b[t + 7], L, 1126891415),
g = u(g, v, f, m, b[t + 14], B, 2878612391),
m = u(m, g, v, f, b[t + 5], A, 4237533241),
f = u(f, m, g, v, b[t + 12], E, 1700485571),
v = u(v, f, m, g, b[t + 3], L, 2399980690),
g = u(g, v, f, m, b[t + 10], B, 4293915773),
m = u(m, g, v, f, b[t + 1], A, 2240044497),
f = u(f, m, g, v, b[t + 8], E, 1873313359),
v = u(v, f, m, g, b[t + 15], L, 4264355552),
g = u(g, v, f, m, b[t + 6], B, 2734768916),
m = u(m, g, v, f, b[t + 13], A, 1309151649),
f = u(f, m, g, v, b[t + 4], E, 4149444226),
v = u(v, f, m, g, b[t + 11], L, 3174756917),
g = u(g, v, f, m, b[t + 2], B, 718787259),
m = u(m, g, v, f, b[t + 9], A, 3951481745),
f = i(f, n),
m = i(m, a),
g = i(g, o),
v = i(v, s);
var O = h(f) + h(m) + h(g) + h(v);
return O.toLowerCase()
}
})
}(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment