-
Notifications
You must be signed in to change notification settings - Fork 21
/
md5.js
77 lines (65 loc) · 1.54 KB
/
md5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Source: https://raw.githubusercontent.com/jbt/tiny-hashes/master/md5/md5.js
// (MIT License)
var k = [], i = 0;
for (; i < 64;) {
k[i] = 0 | Math.sin(++i % Math.PI) * 4294967296;
// k[i] = 0 | (Math.abs(Math.sin(++i)) * 4294967296);
}
export default function md5(s) {
var b, c, d,
h = [ b = 0x67452301, c = 0xEFCDAB89, ~b, ~c ],
words = [],
j = unescape(encodeURI(s)) + '\x80',
a = j.length;
s = (--a / 4 + 2) | 15;
// See "Length bits" in notes
words[--s] = a * 8;
for (; ~a;) { // a !== -1
words[a >> 2] |= j.charCodeAt(a) << 8 * a--;
}
for (i = j = 0; i < s; i += 16) {
a = h;
for (; j < 64;
a = [
d = a[3],
(
b +
((d =
a[0] +
[
b & c | ~b & d,
d & b | ~d & c,
b ^ c ^ d,
c ^ (b | ~d)
][a = j >> 4] +
k[j] +
~~words[i | [
j,
5 * j + 1,
3 * j + 5,
7 * j
][a] & 15]
) << (a = [
7, 12, 17, 22,
5, 9, 14, 20,
4, 11, 16, 23,
6, 10, 15, 21
][4 * a + j++ % 4]) | d >>> -a)
),
b,
c
]
) {
b = a[1] | 0;
c = a[2];
}
// See "Integer safety" in notes
for (j = 4; j;) h[--j] += a[j];
// j === 0
}
for (s = ''; j < 32;) {
s += ((h[j >> 3] >> ((1 ^ j++) * 4)) & 15).toString(16);
// s += ((h[j >> 3] >> (4 ^ 4 * j++)) & 15).toString(16);
}
return s;
}