-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmd5c.js.in
306 lines (271 loc) · 8.62 KB
/
md5c.js.in
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# !! THIS CODE IS NOT MEANT TO BE KEPT COMPATIBLE TO ES12 OR BELOW !!
# (You can BABEL and Polyfills of course. See generated md5.js)
#
# This code is parsed through ./unroll.sh to output the real JS.
# It runs on Big endian, too, but is a bit slower due to swap.
#
# vim: ft=javascript :
#
# I need small and fast code, which must not carry any Copyright,
# and which runs in non-HTTPS-context, too.
# Hence this here is free as in free beer, free speech and free baby.
#
# Based on https://github.com/hilbix/tinolib/blob/master/md5.h
# (which was based on the MD5 reference code)
# and then adapted to use names of other sources like:
# - http://www.myersdaily.org/joseph/javascript/md5.js
# - https://github.com/gorhill/yamd5.js/blob/master/yamd5.js
'use strict';
// This is free as in free beer, free speech and free baby.
// IMPORTANT! NEVER COVER THIS CODE WITH A COPYRIGHT
// as this would break German Urheberrecht!
// If you need to state a Copyright, excempt this code from it!
//
// How to use?
// <script src="md5.js"></script>
// then
// const md5 = MD5.md5('string');
// or
// const md5s = new MD5();
// const a = md5s.init().update_str('str1').update_str('str2').end().$hex;
// const b = md5s.init().update8(new UInt8Array(something)) .end().$bin;
// This can be repeated: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Tables
!ctx a 0
!ctx b 1
!ctx c 2
!ctx d 3
# 1 2 3 4 5 6 7
!F1 a b c d 0 d76aa478 7
!F1 d a b c 1 e8c7b756 12
!F1 c d a b 2 242070db 17
!F1 b c d a 3 c1bdceee 22
!F1 a b c d 4 f57c0faf 7
!F1 d a b c 5 4787c62a 12
!F1 c d a b 6 a8304613 17
!F1 b c d a 7 fd469501 22
!F1 a b c d 8 698098d8 7
!F1 d a b c 9 8b44f7af 12
!F1 c d a b 10 ffff5bb1 17
!F1 b c d a 11 895cd7be 22
!F1 a b c d 12 6b901122 7
!F1 d a b c 13 fd987193 12
!F1 c d a b 14 a679438e 17
!F1 b c d a 15 49b40821 22
!F2 a b c d 1 f61e2562 5
!F2 d a b c 6 c040b340 9
!F2 c d a b 11 265e5a51 14
!F2 b c d a 0 e9b6c7aa 20
!F2 a b c d 5 d62f105d 5
!F2 d a b c 10 02441453 9
!F2 c d a b 15 d8a1e681 14
!F2 b c d a 4 e7d3fbc8 20
!F2 a b c d 9 21e1cde6 5
!F2 d a b c 14 c33707d6 9
!F2 c d a b 3 f4d50d87 14
!F2 b c d a 8 455a14ed 20
!F2 a b c d 13 a9e3e905 5
!F2 d a b c 2 fcefa3f8 9
!F2 c d a b 7 676f02d9 14
!F2 b c d a 12 8d2a4c8a 20
!F3 a b c d 5 fffa3942 4
!F3 d a b c 8 8771f681 11
!F3 c d a b 11 6d9d6122 16
!F3 b c d a 14 fde5380c 23
!F3 a b c d 1 a4beea44 4
!F3 d a b c 4 4bdecfa9 11
!F3 c d a b 7 f6bb4b60 16
!F3 b c d a 10 bebfbc70 23
!F3 a b c d 13 289b7ec6 4
!F3 d a b c 0 eaa127fa 11
!F3 c d a b 3 d4ef3085 16
!F3 b c d a 6 04881d05 23
!F3 a b c d 9 d9d4d039 4
!F3 d a b c 12 e6db99e5 11
!F3 c d a b 15 1fa27cf8 16
!F3 b c d a 2 c4ac5665 23
!F4 a b c d 0 f4292244 6
!F4 d a b c 7 432aff97 10
!F4 c d a b 14 ab9423a7 15
!F4 b c d a 5 fc93a039 21
!F4 a b c d 12 655b59c3 6
!F4 d a b c 3 8f0ccc92 10
!F4 c d a b 10 ffeff47d 15
!F4 b c d a 1 85845dd1 21
!F4 a b c d 8 6fa87e4f 6
!F4 d a b c 15 fe2ce6e0 10
!F4 c d a b 6 a3014314 15
!F4 b c d a 13 4e0811a1 21
!F4 a b c d 4 f7537e82 6
!F4 d a b c 11 bd3af235 10
!F4 c d a b 2 2ad7d2bb 15
!F4 b c d a 9 eb86d391 21
# Macros
:ctx let $1 = ctx[$2];
:ctx: ctx[$2] = $1 + ctx[$2] | 0;
#:F1 console.log($1,$2,$3,$4,buf[$5],0x$6);
#:F2 console.log($1,$2,$3,$4,buf[$5],0x$6);
#:F3 console.log($1,$2,$3,$4,buf[$5],0x$6);
#:F4 console.log($1,$2,$3,$4,buf[$5],0x$6);
:F1 $1 += ( ( ($2 & $3) | (~$2 & $4) ) + buf[$5] + 0x$6 )|0;
:F2 $1 += ( ( ($2 & $4) | ($3 & ~$4) ) + buf[$5] + 0x$6 )|0;
:F3 $1 += ( ($2 ^ $3 ^ $4) + buf[$5] + 0x$6 )|0;
:F4 $1 += ( ($3 ^ ($2 | (~$4))) + buf[$5] + 0x$6 )|0;
:F1 $1 = ( ( ($1 << $7) | (($1|0) >>> $[32-$7]) ) + $2 )|0;
:F2 $1 = ( ( ($1 << $7) | (($1|0) >>> $[32-$7]) ) + $2 )|0;
:F3 $1 = ( ( ($1 << $7) | (($1|0) >>> $[32-$7]) ) + $2 )|0;
:F4 $1 = ( ( ($1 << $7) | (($1|0) >>> $[32-$7]) ) + $2 )|0;
# Implementation
const MD5 = (_=>_())(() => {
function D(...a)
{
const p = [];
for (const x of a)
{
if ('string' === typeof x)
p.push(x);
else if (typeof x !== 'object')
p.push(`${Number(x|0).toString(16)}`.padStart(10,' '));
else if (x.BYTES_PER_ELEMENT)
p.push(Array.from(x).map(_ => _.toString(16).padStart(x.BYTES_PER_ELEMENT*2, '0')).join('_'));
}
console.log(p.join(' '));
}
class MD5
{
big_endian()
{
const b = new ArrayBuffer(4);
new Uint32Array(b)[0] = 0x12345678;
return new Uint8Array(b)[0] === 0x12;
}
constructor()
{
// input array
const b = new ArrayBuffer(64); // must be a multiple of 32 bit
this.in8 = new Uint8Array(b);
// https://hacks.mozilla.org/2017/01/typedarray-or-dataview-understanding-byte-order/
this.in32 = new Uint32Array(b);
// encoder
this.enc = new TextEncoder();
this.trafo = this._trafo;
if (this.big_endian())
this.swap = this._swap;
}
_swap(a32)
{
for (let i = a32.length; --i>=0; )
{
let x = a32[i];
x = ((x>> 8) & 0x00ff00ff) | ((x<< 8) & 0xff00ff00);
x = ((x>>16) & 0x0000ffff) | ((x<<16) & 0xffff0000);
a32[i] = x;
}
}
_trafo()
{
const ctx = this.buf;
const buf = this.in32;
// D(buf, ctx);
@ ctx
@ F1
@ F2
@ F3
@ F4
@ ctx:
this.len += 64;
}
init()
{
this.buf = new Uint32Array([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]);
this.fill = 0; // fill state of this.in
this.len = 0; // total bytes transformed into hash so far
return this;
}
end()
{
let pos = this.fill;
const len = (this.len + pos) * 8; // we count bits
const in8 = this.in8;
const in32 = this.in32;
// there is at least 1 byte free in this.in8
in8[pos] = 0x80;
let rest = 64-1-pos;
// The last 8 byte of the block will be overwritten below
for (let i=rest; --i>=0; in8[++pos]=0);
if (this.swap) this.swap(this.in32);
if (rest<8)
{
this.trafo();
for (let i=14; --i>=0; in32[i]=0);
}
in32[15] = len / 0x100000000 | 0; // we cannot use integer math here
in32[14] = len | 0;
this.trafo();
this.fill = void 0;
this.len = void 0;
// this.buf contains the result
if (this.swap) this.swap(this.buf);
return this;
}
update8(b) // Uint8Array
{
const _ = this.in8;
let out = this.fill | 0;
let len = b.length | 0; // we do not support more than 2^32 bytes (AKA 4GiB) per update
let pos = 0;
while (len)
{
let max = 64-out | 0;
if (max > len)
max = len | 0;
// To avoid the swap() on Big endian machines, following copy
// should rather be replaced by some inlined Duff's device.
// (for this we need to add up/down-counting repeats to ./unroll.sh)
// (and we probably need conditional macros as well as recursive ones)
_.set(b.subarray(pos, pos+max), out);
pos = pos + max | 0;
out = out + max | 0;
len = len - max | 0;
if (out<64)
break;
if (this.swap) this.swap(this.in32);
this.trafo();
out = 0;
}
this.fill = out;
return this;
}
update_str(s) // this is not meant for long strings!
{
return this.update8(this.enc.encode(s));
}
get $bin()
{
return new Uint8Array(this.buf.buffer);
}
get $hex()
{
return Array.from(this.$bin, b => b.toString(16).padStart(2, '0')).join('');
}
static md5(s,bin)
{
const out = md5.init().update_str(s).end();
return bin ? out.$bin : out.$hex;
}
static test(d,t)
{
const r = this.md5(t);
if (r !== d)
throw `md5c.js test failed for '${t}':\n'${d}' expected\n'${r}' got`;
// D('test ok', r);
}
};
const md5 = new MD5(); // for the static md5 above
MD5.test('d41d8cd98f00b204e9800998ecf8427e', ''); // 0 bytes
MD5.test('d8ea71eb4d2af27f59a5316c971065e6', '0123456789abcdef0123456789abcdef0123456789abcdef0123456'); // single round
MD5.test('a68f061e81239660f6305195739ba7f0', '0123456789abcdef0123456789abcdef0123456789abcdef01234567'); // double round
MD5.test('fe3a1ff59f3b89b2ad3d33f08984874b', '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'); // 64 byte input
// console.log('selftest ok');
return MD5;
});