1 | "use strict";
|
2 |
|
3 |
|
4 | var Md5FileHasher = (function () {
|
5 | function Md5FileHasher(_callback, // Callback to return the result
|
6 | _async, // Async version is not always available in a web worker
|
7 | _partSize) {
|
8 | if (_async === void 0) { _async = true; }
|
9 | if (_partSize === void 0) { _partSize = 1048576; }
|
10 | this._callback = _callback;
|
11 | this._async = _async;
|
12 | this._partSize = _partSize;
|
13 | this._configureReader();
|
14 | }
|
15 | |
16 |
|
17 |
|
18 |
|
19 | Md5FileHasher.prototype.hash = function (blob) {
|
20 | var self = this;
|
21 | self._blob = blob;
|
22 |
|
23 | self._part = 0;
|
24 | self._md5 = new Md5();
|
25 | self._processPart();
|
26 | };
|
27 | Md5FileHasher.prototype._fail = function () {
|
28 | this._callback({
|
29 | success: false,
|
30 | result: 'data read failed'
|
31 | });
|
32 | };
|
33 | Md5FileHasher.prototype._hashData = function (e) {
|
34 | var self = this;
|
35 | self._md5.appendByteArray(new Uint8Array(e.target.result));
|
36 | if (self._part * self._partSize >= self._blob.size) {
|
37 | self._callback({
|
38 | success: true,
|
39 | result: self._md5.end()
|
40 | });
|
41 | }
|
42 | else {
|
43 | self._processPart();
|
44 | }
|
45 | };
|
46 | Md5FileHasher.prototype._processPart = function () {
|
47 | var self = this;
|
48 | var endbyte = 0;
|
49 | var current_part;
|
50 | self._part += 1;
|
51 | if (self._blob.size > self._partSize) {
|
52 | endbyte = self._part * self._partSize;
|
53 | if (endbyte > self._blob.size) {
|
54 | endbyte = self._blob.size;
|
55 | }
|
56 | current_part = self._blob.slice((self._part - 1) * self._partSize, endbyte);
|
57 | }
|
58 | else {
|
59 | current_part = self._blob;
|
60 | }
|
61 | if (self._async) {
|
62 | self._reader.readAsArrayBuffer(current_part);
|
63 | }
|
64 | else {
|
65 | setTimeout(function () {
|
66 | try {
|
67 | self._hashData({
|
68 | target: {
|
69 | result: self._reader.readAsArrayBuffer(current_part)
|
70 | },
|
71 | });
|
72 | }
|
73 | catch (e) {
|
74 | self._fail();
|
75 | }
|
76 | }, 0);
|
77 | }
|
78 | };
|
79 | Md5FileHasher.prototype._configureReader = function () {
|
80 | var self = this;
|
81 | if (self._async) {
|
82 | self._reader = new FileReader();
|
83 | self._reader.onload = self._hashData.bind(self);
|
84 | self._reader.onerror = self._fail.bind(self);
|
85 | self._reader.onabort = self._fail.bind(self);
|
86 | }
|
87 | else {
|
88 | self._reader = new FileReaderSync();
|
89 | }
|
90 | };
|
91 | return Md5FileHasher;
|
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 | var Md5 = (function () {
|
134 | function Md5() {
|
135 | this._dataLength = 0;
|
136 | this._bufferLength = 0;
|
137 | this._state = new Int32Array(4);
|
138 | this._buffer = new ArrayBuffer(68);
|
139 | this._buffer8 = new Uint8Array(this._buffer, 0, 68);
|
140 | this._buffer32 = new Uint32Array(this._buffer, 0, 17);
|
141 | this.start();
|
142 | }
|
143 | Md5.hashStr = function (str, raw) {
|
144 | if (raw === void 0) { raw = false; }
|
145 | return this.onePassHasher
|
146 | .start()
|
147 | .appendStr(str)
|
148 | .end(raw);
|
149 | };
|
150 | Md5.hashAsciiStr = function (str, raw) {
|
151 | if (raw === void 0) { raw = false; }
|
152 | return this.onePassHasher
|
153 | .start()
|
154 | .appendAsciiStr(str)
|
155 | .end(raw);
|
156 | };
|
157 | Md5._hex = function (x) {
|
158 | var hc = Md5.hexChars;
|
159 | var ho = Md5.hexOut;
|
160 | var n;
|
161 | var offset;
|
162 | var j;
|
163 | var i;
|
164 | for (i = 0; i < 4; i += 1) {
|
165 | offset = i * 8;
|
166 | n = x[i];
|
167 | for (j = 0; j < 8; j += 2) {
|
168 | ho[offset + 1 + j] = hc.charAt(n & 0x0F);
|
169 | n >>>= 4;
|
170 | ho[offset + 0 + j] = hc.charAt(n & 0x0F);
|
171 | n >>>= 4;
|
172 | }
|
173 | }
|
174 | return ho.join('');
|
175 | };
|
176 | Md5._md5cycle = function (x, k) {
|
177 | var a = x[0];
|
178 | var b = x[1];
|
179 | var c = x[2];
|
180 | var d = x[3];
|
181 |
|
182 | a += (b & c | ~b & d) + k[0] - 680876936 | 0;
|
183 | a = (a << 7 | a >>> 25) + b | 0;
|
184 | d += (a & b | ~a & c) + k[1] - 389564586 | 0;
|
185 | d = (d << 12 | d >>> 20) + a | 0;
|
186 | c += (d & a | ~d & b) + k[2] + 606105819 | 0;
|
187 | c = (c << 17 | c >>> 15) + d | 0;
|
188 | b += (c & d | ~c & a) + k[3] - 1044525330 | 0;
|
189 | b = (b << 22 | b >>> 10) + c | 0;
|
190 | a += (b & c | ~b & d) + k[4] - 176418897 | 0;
|
191 | a = (a << 7 | a >>> 25) + b | 0;
|
192 | d += (a & b | ~a & c) + k[5] + 1200080426 | 0;
|
193 | d = (d << 12 | d >>> 20) + a | 0;
|
194 | c += (d & a | ~d & b) + k[6] - 1473231341 | 0;
|
195 | c = (c << 17 | c >>> 15) + d | 0;
|
196 | b += (c & d | ~c & a) + k[7] - 45705983 | 0;
|
197 | b = (b << 22 | b >>> 10) + c | 0;
|
198 | a += (b & c | ~b & d) + k[8] + 1770035416 | 0;
|
199 | a = (a << 7 | a >>> 25) + b | 0;
|
200 | d += (a & b | ~a & c) + k[9] - 1958414417 | 0;
|
201 | d = (d << 12 | d >>> 20) + a | 0;
|
202 | c += (d & a | ~d & b) + k[10] - 42063 | 0;
|
203 | c = (c << 17 | c >>> 15) + d | 0;
|
204 | b += (c & d | ~c & a) + k[11] - 1990404162 | 0;
|
205 | b = (b << 22 | b >>> 10) + c | 0;
|
206 | a += (b & c | ~b & d) + k[12] + 1804603682 | 0;
|
207 | a = (a << 7 | a >>> 25) + b | 0;
|
208 | d += (a & b | ~a & c) + k[13] - 40341101 | 0;
|
209 | d = (d << 12 | d >>> 20) + a | 0;
|
210 | c += (d & a | ~d & b) + k[14] - 1502002290 | 0;
|
211 | c = (c << 17 | c >>> 15) + d | 0;
|
212 | b += (c & d | ~c & a) + k[15] + 1236535329 | 0;
|
213 | b = (b << 22 | b >>> 10) + c | 0;
|
214 |
|
215 | a += (b & d | c & ~d) + k[1] - 165796510 | 0;
|
216 | a = (a << 5 | a >>> 27) + b | 0;
|
217 | d += (a & c | b & ~c) + k[6] - 1069501632 | 0;
|
218 | d = (d << 9 | d >>> 23) + a | 0;
|
219 | c += (d & b | a & ~b) + k[11] + 643717713 | 0;
|
220 | c = (c << 14 | c >>> 18) + d | 0;
|
221 | b += (c & a | d & ~a) + k[0] - 373897302 | 0;
|
222 | b = (b << 20 | b >>> 12) + c | 0;
|
223 | a += (b & d | c & ~d) + k[5] - 701558691 | 0;
|
224 | a = (a << 5 | a >>> 27) + b | 0;
|
225 | d += (a & c | b & ~c) + k[10] + 38016083 | 0;
|
226 | d = (d << 9 | d >>> 23) + a | 0;
|
227 | c += (d & b | a & ~b) + k[15] - 660478335 | 0;
|
228 | c = (c << 14 | c >>> 18) + d | 0;
|
229 | b += (c & a | d & ~a) + k[4] - 405537848 | 0;
|
230 | b = (b << 20 | b >>> 12) + c | 0;
|
231 | a += (b & d | c & ~d) + k[9] + 568446438 | 0;
|
232 | a = (a << 5 | a >>> 27) + b | 0;
|
233 | d += (a & c | b & ~c) + k[14] - 1019803690 | 0;
|
234 | d = (d << 9 | d >>> 23) + a | 0;
|
235 | c += (d & b | a & ~b) + k[3] - 187363961 | 0;
|
236 | c = (c << 14 | c >>> 18) + d | 0;
|
237 | b += (c & a | d & ~a) + k[8] + 1163531501 | 0;
|
238 | b = (b << 20 | b >>> 12) + c | 0;
|
239 | a += (b & d | c & ~d) + k[13] - 1444681467 | 0;
|
240 | a = (a << 5 | a >>> 27) + b | 0;
|
241 | d += (a & c | b & ~c) + k[2] - 51403784 | 0;
|
242 | d = (d << 9 | d >>> 23) + a | 0;
|
243 | c += (d & b | a & ~b) + k[7] + 1735328473 | 0;
|
244 | c = (c << 14 | c >>> 18) + d | 0;
|
245 | b += (c & a | d & ~a) + k[12] - 1926607734 | 0;
|
246 | b = (b << 20 | b >>> 12) + c | 0;
|
247 |
|
248 | a += (b ^ c ^ d) + k[5] - 378558 | 0;
|
249 | a = (a << 4 | a >>> 28) + b | 0;
|
250 | d += (a ^ b ^ c) + k[8] - 2022574463 | 0;
|
251 | d = (d << 11 | d >>> 21) + a | 0;
|
252 | c += (d ^ a ^ b) + k[11] + 1839030562 | 0;
|
253 | c = (c << 16 | c >>> 16) + d | 0;
|
254 | b += (c ^ d ^ a) + k[14] - 35309556 | 0;
|
255 | b = (b << 23 | b >>> 9) + c | 0;
|
256 | a += (b ^ c ^ d) + k[1] - 1530992060 | 0;
|
257 | a = (a << 4 | a >>> 28) + b | 0;
|
258 | d += (a ^ b ^ c) + k[4] + 1272893353 | 0;
|
259 | d = (d << 11 | d >>> 21) + a | 0;
|
260 | c += (d ^ a ^ b) + k[7] - 155497632 | 0;
|
261 | c = (c << 16 | c >>> 16) + d | 0;
|
262 | b += (c ^ d ^ a) + k[10] - 1094730640 | 0;
|
263 | b = (b << 23 | b >>> 9) + c | 0;
|
264 | a += (b ^ c ^ d) + k[13] + 681279174 | 0;
|
265 | a = (a << 4 | a >>> 28) + b | 0;
|
266 | d += (a ^ b ^ c) + k[0] - 358537222 | 0;
|
267 | d = (d << 11 | d >>> 21) + a | 0;
|
268 | c += (d ^ a ^ b) + k[3] - 722521979 | 0;
|
269 | c = (c << 16 | c >>> 16) + d | 0;
|
270 | b += (c ^ d ^ a) + k[6] + 76029189 | 0;
|
271 | b = (b << 23 | b >>> 9) + c | 0;
|
272 | a += (b ^ c ^ d) + k[9] - 640364487 | 0;
|
273 | a = (a << 4 | a >>> 28) + b | 0;
|
274 | d += (a ^ b ^ c) + k[12] - 421815835 | 0;
|
275 | d = (d << 11 | d >>> 21) + a | 0;
|
276 | c += (d ^ a ^ b) + k[15] + 530742520 | 0;
|
277 | c = (c << 16 | c >>> 16) + d | 0;
|
278 | b += (c ^ d ^ a) + k[2] - 995338651 | 0;
|
279 | b = (b << 23 | b >>> 9) + c | 0;
|
280 |
|
281 | a += (c ^ (b | ~d)) + k[0] - 198630844 | 0;
|
282 | a = (a << 6 | a >>> 26) + b | 0;
|
283 | d += (b ^ (a | ~c)) + k[7] + 1126891415 | 0;
|
284 | d = (d << 10 | d >>> 22) + a | 0;
|
285 | c += (a ^ (d | ~b)) + k[14] - 1416354905 | 0;
|
286 | c = (c << 15 | c >>> 17) + d | 0;
|
287 | b += (d ^ (c | ~a)) + k[5] - 57434055 | 0;
|
288 | b = (b << 21 | b >>> 11) + c | 0;
|
289 | a += (c ^ (b | ~d)) + k[12] + 1700485571 | 0;
|
290 | a = (a << 6 | a >>> 26) + b | 0;
|
291 | d += (b ^ (a | ~c)) + k[3] - 1894986606 | 0;
|
292 | d = (d << 10 | d >>> 22) + a | 0;
|
293 | c += (a ^ (d | ~b)) + k[10] - 1051523 | 0;
|
294 | c = (c << 15 | c >>> 17) + d | 0;
|
295 | b += (d ^ (c | ~a)) + k[1] - 2054922799 | 0;
|
296 | b = (b << 21 | b >>> 11) + c | 0;
|
297 | a += (c ^ (b | ~d)) + k[8] + 1873313359 | 0;
|
298 | a = (a << 6 | a >>> 26) + b | 0;
|
299 | d += (b ^ (a | ~c)) + k[15] - 30611744 | 0;
|
300 | d = (d << 10 | d >>> 22) + a | 0;
|
301 | c += (a ^ (d | ~b)) + k[6] - 1560198380 | 0;
|
302 | c = (c << 15 | c >>> 17) + d | 0;
|
303 | b += (d ^ (c | ~a)) + k[13] + 1309151649 | 0;
|
304 | b = (b << 21 | b >>> 11) + c | 0;
|
305 | a += (c ^ (b | ~d)) + k[4] - 145523070 | 0;
|
306 | a = (a << 6 | a >>> 26) + b | 0;
|
307 | d += (b ^ (a | ~c)) + k[11] - 1120210379 | 0;
|
308 | d = (d << 10 | d >>> 22) + a | 0;
|
309 | c += (a ^ (d | ~b)) + k[2] + 718787259 | 0;
|
310 | c = (c << 15 | c >>> 17) + d | 0;
|
311 | b += (d ^ (c | ~a)) + k[9] - 343485551 | 0;
|
312 | b = (b << 21 | b >>> 11) + c | 0;
|
313 | x[0] = a + x[0] | 0;
|
314 | x[1] = b + x[1] | 0;
|
315 | x[2] = c + x[2] | 0;
|
316 | x[3] = d + x[3] | 0;
|
317 | };
|
318 | |
319 |
|
320 |
|
321 | Md5.prototype.start = function () {
|
322 | this._dataLength = 0;
|
323 | this._bufferLength = 0;
|
324 | this._state.set(Md5.stateIdentity);
|
325 | return this;
|
326 | };
|
327 |
|
328 |
|
329 |
|
330 | |
331 |
|
332 |
|
333 |
|
334 | Md5.prototype.appendStr = function (str) {
|
335 | var buf8 = this._buffer8;
|
336 | var buf32 = this._buffer32;
|
337 | var bufLen = this._bufferLength;
|
338 | var code;
|
339 | var i;
|
340 | for (i = 0; i < str.length; i += 1) {
|
341 | code = str.charCodeAt(i);
|
342 | if (code < 128) {
|
343 | buf8[bufLen++] = code;
|
344 | }
|
345 | else if (code < 0x800) {
|
346 | buf8[bufLen++] = (code >>> 6) + 0xC0;
|
347 | buf8[bufLen++] = code & 0x3F | 0x80;
|
348 | }
|
349 | else if (code < 0xD800 || code > 0xDBFF) {
|
350 | buf8[bufLen++] = (code >>> 12) + 0xE0;
|
351 | buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80;
|
352 | buf8[bufLen++] = (code & 0x3F) | 0x80;
|
353 | }
|
354 | else {
|
355 | code = ((code - 0xD800) * 0x400) + (str.charCodeAt(++i) - 0xDC00) + 0x10000;
|
356 | if (code > 0x10FFFF) {
|
357 | throw new Error('Unicode standard supports code points up to U+10FFFF');
|
358 | }
|
359 | buf8[bufLen++] = (code >>> 18) + 0xF0;
|
360 | buf8[bufLen++] = (code >>> 12 & 0x3F) | 0x80;
|
361 | buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80;
|
362 | buf8[bufLen++] = (code & 0x3F) | 0x80;
|
363 | }
|
364 | if (bufLen >= 64) {
|
365 | this._dataLength += 64;
|
366 | Md5._md5cycle(this._state, buf32);
|
367 | bufLen -= 64;
|
368 | buf32[0] = buf32[16];
|
369 | }
|
370 | }
|
371 | this._bufferLength = bufLen;
|
372 | return this;
|
373 | };
|
374 | |
375 |
|
376 |
|
377 |
|
378 | Md5.prototype.appendAsciiStr = function (str) {
|
379 | var buf8 = this._buffer8;
|
380 | var buf32 = this._buffer32;
|
381 | var bufLen = this._bufferLength;
|
382 | var i;
|
383 | var j = 0;
|
384 | for (;;) {
|
385 | i = Math.min(str.length - j, 64 - bufLen);
|
386 | while (i--) {
|
387 | buf8[bufLen++] = str.charCodeAt(j++);
|
388 | }
|
389 | if (bufLen < 64) {
|
390 | break;
|
391 | }
|
392 | this._dataLength += 64;
|
393 | Md5._md5cycle(this._state, buf32);
|
394 | bufLen = 0;
|
395 | }
|
396 | this._bufferLength = bufLen;
|
397 | return this;
|
398 | };
|
399 | |
400 |
|
401 |
|
402 |
|
403 | Md5.prototype.appendByteArray = function (input) {
|
404 | var buf8 = this._buffer8;
|
405 | var buf32 = this._buffer32;
|
406 | var bufLen = this._bufferLength;
|
407 | var i;
|
408 | var j = 0;
|
409 | for (;;) {
|
410 | i = Math.min(input.length - j, 64 - bufLen);
|
411 | while (i--) {
|
412 | buf8[bufLen++] = input[j++];
|
413 | }
|
414 | if (bufLen < 64) {
|
415 | break;
|
416 | }
|
417 | this._dataLength += 64;
|
418 | Md5._md5cycle(this._state, buf32);
|
419 | bufLen = 0;
|
420 | }
|
421 | this._bufferLength = bufLen;
|
422 | return this;
|
423 | };
|
424 | |
425 |
|
426 |
|
427 | Md5.prototype.getState = function () {
|
428 | var s = this._state;
|
429 | return {
|
430 | buffer: String.fromCharCode.apply(null, Array.from(this._buffer8)),
|
431 | buflen: this._bufferLength,
|
432 | length: this._dataLength,
|
433 | state: [s[0], s[1], s[2], s[3]]
|
434 | };
|
435 | };
|
436 | |
437 |
|
438 |
|
439 |
|
440 | Md5.prototype.setState = function (state) {
|
441 | var buf = state.buffer;
|
442 | var x = state.state;
|
443 | var s = this._state;
|
444 | var i;
|
445 | this._dataLength = state.length;
|
446 | this._bufferLength = state.buflen;
|
447 | s[0] = x[0];
|
448 | s[1] = x[1];
|
449 | s[2] = x[2];
|
450 | s[3] = x[3];
|
451 | for (i = 0; i < buf.length; i += 1) {
|
452 | this._buffer8[i] = buf.charCodeAt(i);
|
453 | }
|
454 | };
|
455 | |
456 |
|
457 |
|
458 |
|
459 | Md5.prototype.end = function (raw) {
|
460 | if (raw === void 0) { raw = false; }
|
461 | var bufLen = this._bufferLength;
|
462 | var buf8 = this._buffer8;
|
463 | var buf32 = this._buffer32;
|
464 | var i = (bufLen >> 2) + 1;
|
465 | this._dataLength += bufLen;
|
466 | var dataBitsLen = this._dataLength * 8;
|
467 | buf8[bufLen] = 0x80;
|
468 | buf8[bufLen + 1] = buf8[bufLen + 2] = buf8[bufLen + 3] = 0;
|
469 | buf32.set(Md5.buffer32Identity.subarray(i), i);
|
470 | if (bufLen > 55) {
|
471 | Md5._md5cycle(this._state, buf32);
|
472 | buf32.set(Md5.buffer32Identity);
|
473 | }
|
474 |
|
475 |
|
476 | if (dataBitsLen <= 0xFFFFFFFF) {
|
477 | buf32[14] = dataBitsLen;
|
478 | }
|
479 | else {
|
480 | var matches = dataBitsLen.toString(16).match(/(.*?)(.{0,8})$/);
|
481 | if (matches === null) {
|
482 | return;
|
483 | }
|
484 | var lo = parseInt(matches[2], 16);
|
485 | var hi = parseInt(matches[1], 16) || 0;
|
486 | buf32[14] = lo;
|
487 | buf32[15] = hi;
|
488 | }
|
489 | Md5._md5cycle(this._state, buf32);
|
490 | return raw ? this._state : Md5._hex(this._state);
|
491 | };
|
492 |
|
493 | Md5.stateIdentity = new Int32Array([1732584193, -271733879, -1732584194, 271733878]);
|
494 | Md5.buffer32Identity = new Int32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
495 | Md5.hexChars = '0123456789abcdef';
|
496 | Md5.hexOut = [];
|
497 |
|
498 | Md5.onePassHasher = new Md5();
|
499 | return Md5;
|
500 | }());
|
501 | if (Md5.hashStr('hello') !== '5d41402abc4b2a76b9719d911017c592') {
|
502 | throw new Error('Md5 self test failed.');
|
503 | }
|
504 |
|
505 | (function(global) {
|
506 |
|
507 | var async = !!global.FileReader;
|
508 |
|
509 |
|
510 | if (!Blob.prototype.slice) {
|
511 | Blob.prototype.slice = Blob.prototype.webkitSlice || Blob.prototype.mozSlice;
|
512 | }
|
513 |
|
514 |
|
515 | global.onmessage = function(e) {
|
516 | var hasher = new Md5FileHasher(function (data) {
|
517 |
|
518 | global.postMessage(data);
|
519 | }, async);
|
520 |
|
521 | hasher.hash(e.data);
|
522 | };
|
523 | })(this);
|