UNPKG

16.1 kBJavaScriptView Raw
1"use strict";
2/*
3
4TypeScript Md5
5==============
6
7Based on work by
8* Joseph Myers: http://www.myersdaily.org/joseph/javascript/md5-text.html
9* André Cruz: https://github.com/satazor/SparkMD5
10* Raymond Hill: https://github.com/gorhill/yamd5.js
11
12Effectively a TypeScrypt re-write of Raymond Hill JS Library
13
14The MIT License (MIT)
15
16Copyright (C) 2014 Raymond Hill
17
18Permission is hereby granted, free of charge, to any person obtaining a copy
19of this software and associated documentation files (the "Software"), to deal
20in the Software without restriction, including without limitation the rights
21to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22copies of the Software, and to permit persons to whom the Software is
23furnished to do so, subject to the following conditions:
24
25The above copyright notice and this permission notice shall be included in
26all copies or substantial portions of the Software.
27
28THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34THE SOFTWARE.
35
36
37
38 DO WHAT YOU WANT TO PUBLIC LICENSE
39 Version 2, December 2004
40
41 Copyright (C) 2015 André Cruz <amdfcruz@gmail.com>
42
43 Everyone is permitted to copy and distribute verbatim or modified
44 copies of this license document, and changing it is allowed as long
45 as the name is changed.
46
47 DO WHAT YOU WANT TO PUBLIC LICENSE
48 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
49
50 0. You just DO WHAT YOU WANT TO.
51
52
53*/
54Object.defineProperty(exports, "__esModule", { value: true });
55exports.Md5 = void 0;
56;
57var Md5 = /** @class */ (function () {
58 function Md5() {
59 this._dataLength = 0;
60 this._bufferLength = 0;
61 this._state = new Int32Array(4);
62 this._buffer = new ArrayBuffer(68);
63 this._buffer8 = new Uint8Array(this._buffer, 0, 68);
64 this._buffer32 = new Uint32Array(this._buffer, 0, 17);
65 this.start();
66 }
67 Md5.hashStr = function (str, raw) {
68 if (raw === void 0) { raw = false; }
69 return this.onePassHasher
70 .start()
71 .appendStr(str)
72 .end(raw);
73 };
74 Md5.hashAsciiStr = function (str, raw) {
75 if (raw === void 0) { raw = false; }
76 return this.onePassHasher
77 .start()
78 .appendAsciiStr(str)
79 .end(raw);
80 };
81 Md5._hex = function (x) {
82 var hc = Md5.hexChars;
83 var ho = Md5.hexOut;
84 var n;
85 var offset;
86 var j;
87 var i;
88 for (i = 0; i < 4; i += 1) {
89 offset = i * 8;
90 n = x[i];
91 for (j = 0; j < 8; j += 2) {
92 ho[offset + 1 + j] = hc.charAt(n & 0x0F);
93 n >>>= 4;
94 ho[offset + 0 + j] = hc.charAt(n & 0x0F);
95 n >>>= 4;
96 }
97 }
98 return ho.join('');
99 };
100 Md5._md5cycle = function (x, k) {
101 var a = x[0];
102 var b = x[1];
103 var c = x[2];
104 var d = x[3];
105 // ff()
106 a += (b & c | ~b & d) + k[0] - 680876936 | 0;
107 a = (a << 7 | a >>> 25) + b | 0;
108 d += (a & b | ~a & c) + k[1] - 389564586 | 0;
109 d = (d << 12 | d >>> 20) + a | 0;
110 c += (d & a | ~d & b) + k[2] + 606105819 | 0;
111 c = (c << 17 | c >>> 15) + d | 0;
112 b += (c & d | ~c & a) + k[3] - 1044525330 | 0;
113 b = (b << 22 | b >>> 10) + c | 0;
114 a += (b & c | ~b & d) + k[4] - 176418897 | 0;
115 a = (a << 7 | a >>> 25) + b | 0;
116 d += (a & b | ~a & c) + k[5] + 1200080426 | 0;
117 d = (d << 12 | d >>> 20) + a | 0;
118 c += (d & a | ~d & b) + k[6] - 1473231341 | 0;
119 c = (c << 17 | c >>> 15) + d | 0;
120 b += (c & d | ~c & a) + k[7] - 45705983 | 0;
121 b = (b << 22 | b >>> 10) + c | 0;
122 a += (b & c | ~b & d) + k[8] + 1770035416 | 0;
123 a = (a << 7 | a >>> 25) + b | 0;
124 d += (a & b | ~a & c) + k[9] - 1958414417 | 0;
125 d = (d << 12 | d >>> 20) + a | 0;
126 c += (d & a | ~d & b) + k[10] - 42063 | 0;
127 c = (c << 17 | c >>> 15) + d | 0;
128 b += (c & d | ~c & a) + k[11] - 1990404162 | 0;
129 b = (b << 22 | b >>> 10) + c | 0;
130 a += (b & c | ~b & d) + k[12] + 1804603682 | 0;
131 a = (a << 7 | a >>> 25) + b | 0;
132 d += (a & b | ~a & c) + k[13] - 40341101 | 0;
133 d = (d << 12 | d >>> 20) + a | 0;
134 c += (d & a | ~d & b) + k[14] - 1502002290 | 0;
135 c = (c << 17 | c >>> 15) + d | 0;
136 b += (c & d | ~c & a) + k[15] + 1236535329 | 0;
137 b = (b << 22 | b >>> 10) + c | 0;
138 // gg()
139 a += (b & d | c & ~d) + k[1] - 165796510 | 0;
140 a = (a << 5 | a >>> 27) + b | 0;
141 d += (a & c | b & ~c) + k[6] - 1069501632 | 0;
142 d = (d << 9 | d >>> 23) + a | 0;
143 c += (d & b | a & ~b) + k[11] + 643717713 | 0;
144 c = (c << 14 | c >>> 18) + d | 0;
145 b += (c & a | d & ~a) + k[0] - 373897302 | 0;
146 b = (b << 20 | b >>> 12) + c | 0;
147 a += (b & d | c & ~d) + k[5] - 701558691 | 0;
148 a = (a << 5 | a >>> 27) + b | 0;
149 d += (a & c | b & ~c) + k[10] + 38016083 | 0;
150 d = (d << 9 | d >>> 23) + a | 0;
151 c += (d & b | a & ~b) + k[15] - 660478335 | 0;
152 c = (c << 14 | c >>> 18) + d | 0;
153 b += (c & a | d & ~a) + k[4] - 405537848 | 0;
154 b = (b << 20 | b >>> 12) + c | 0;
155 a += (b & d | c & ~d) + k[9] + 568446438 | 0;
156 a = (a << 5 | a >>> 27) + b | 0;
157 d += (a & c | b & ~c) + k[14] - 1019803690 | 0;
158 d = (d << 9 | d >>> 23) + a | 0;
159 c += (d & b | a & ~b) + k[3] - 187363961 | 0;
160 c = (c << 14 | c >>> 18) + d | 0;
161 b += (c & a | d & ~a) + k[8] + 1163531501 | 0;
162 b = (b << 20 | b >>> 12) + c | 0;
163 a += (b & d | c & ~d) + k[13] - 1444681467 | 0;
164 a = (a << 5 | a >>> 27) + b | 0;
165 d += (a & c | b & ~c) + k[2] - 51403784 | 0;
166 d = (d << 9 | d >>> 23) + a | 0;
167 c += (d & b | a & ~b) + k[7] + 1735328473 | 0;
168 c = (c << 14 | c >>> 18) + d | 0;
169 b += (c & a | d & ~a) + k[12] - 1926607734 | 0;
170 b = (b << 20 | b >>> 12) + c | 0;
171 // hh()
172 a += (b ^ c ^ d) + k[5] - 378558 | 0;
173 a = (a << 4 | a >>> 28) + b | 0;
174 d += (a ^ b ^ c) + k[8] - 2022574463 | 0;
175 d = (d << 11 | d >>> 21) + a | 0;
176 c += (d ^ a ^ b) + k[11] + 1839030562 | 0;
177 c = (c << 16 | c >>> 16) + d | 0;
178 b += (c ^ d ^ a) + k[14] - 35309556 | 0;
179 b = (b << 23 | b >>> 9) + c | 0;
180 a += (b ^ c ^ d) + k[1] - 1530992060 | 0;
181 a = (a << 4 | a >>> 28) + b | 0;
182 d += (a ^ b ^ c) + k[4] + 1272893353 | 0;
183 d = (d << 11 | d >>> 21) + a | 0;
184 c += (d ^ a ^ b) + k[7] - 155497632 | 0;
185 c = (c << 16 | c >>> 16) + d | 0;
186 b += (c ^ d ^ a) + k[10] - 1094730640 | 0;
187 b = (b << 23 | b >>> 9) + c | 0;
188 a += (b ^ c ^ d) + k[13] + 681279174 | 0;
189 a = (a << 4 | a >>> 28) + b | 0;
190 d += (a ^ b ^ c) + k[0] - 358537222 | 0;
191 d = (d << 11 | d >>> 21) + a | 0;
192 c += (d ^ a ^ b) + k[3] - 722521979 | 0;
193 c = (c << 16 | c >>> 16) + d | 0;
194 b += (c ^ d ^ a) + k[6] + 76029189 | 0;
195 b = (b << 23 | b >>> 9) + c | 0;
196 a += (b ^ c ^ d) + k[9] - 640364487 | 0;
197 a = (a << 4 | a >>> 28) + b | 0;
198 d += (a ^ b ^ c) + k[12] - 421815835 | 0;
199 d = (d << 11 | d >>> 21) + a | 0;
200 c += (d ^ a ^ b) + k[15] + 530742520 | 0;
201 c = (c << 16 | c >>> 16) + d | 0;
202 b += (c ^ d ^ a) + k[2] - 995338651 | 0;
203 b = (b << 23 | b >>> 9) + c | 0;
204 // ii()
205 a += (c ^ (b | ~d)) + k[0] - 198630844 | 0;
206 a = (a << 6 | a >>> 26) + b | 0;
207 d += (b ^ (a | ~c)) + k[7] + 1126891415 | 0;
208 d = (d << 10 | d >>> 22) + a | 0;
209 c += (a ^ (d | ~b)) + k[14] - 1416354905 | 0;
210 c = (c << 15 | c >>> 17) + d | 0;
211 b += (d ^ (c | ~a)) + k[5] - 57434055 | 0;
212 b = (b << 21 | b >>> 11) + c | 0;
213 a += (c ^ (b | ~d)) + k[12] + 1700485571 | 0;
214 a = (a << 6 | a >>> 26) + b | 0;
215 d += (b ^ (a | ~c)) + k[3] - 1894986606 | 0;
216 d = (d << 10 | d >>> 22) + a | 0;
217 c += (a ^ (d | ~b)) + k[10] - 1051523 | 0;
218 c = (c << 15 | c >>> 17) + d | 0;
219 b += (d ^ (c | ~a)) + k[1] - 2054922799 | 0;
220 b = (b << 21 | b >>> 11) + c | 0;
221 a += (c ^ (b | ~d)) + k[8] + 1873313359 | 0;
222 a = (a << 6 | a >>> 26) + b | 0;
223 d += (b ^ (a | ~c)) + k[15] - 30611744 | 0;
224 d = (d << 10 | d >>> 22) + a | 0;
225 c += (a ^ (d | ~b)) + k[6] - 1560198380 | 0;
226 c = (c << 15 | c >>> 17) + d | 0;
227 b += (d ^ (c | ~a)) + k[13] + 1309151649 | 0;
228 b = (b << 21 | b >>> 11) + c | 0;
229 a += (c ^ (b | ~d)) + k[4] - 145523070 | 0;
230 a = (a << 6 | a >>> 26) + b | 0;
231 d += (b ^ (a | ~c)) + k[11] - 1120210379 | 0;
232 d = (d << 10 | d >>> 22) + a | 0;
233 c += (a ^ (d | ~b)) + k[2] + 718787259 | 0;
234 c = (c << 15 | c >>> 17) + d | 0;
235 b += (d ^ (c | ~a)) + k[9] - 343485551 | 0;
236 b = (b << 21 | b >>> 11) + c | 0;
237 x[0] = a + x[0] | 0;
238 x[1] = b + x[1] | 0;
239 x[2] = c + x[2] | 0;
240 x[3] = d + x[3] | 0;
241 };
242 /**
243 * Initialise buffer to be hashed
244 */
245 Md5.prototype.start = function () {
246 this._dataLength = 0;
247 this._bufferLength = 0;
248 this._state.set(Md5.stateIdentity);
249 return this;
250 };
251 // Char to code point to to array conversion:
252 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
253 // #Example.3A_Fixing_charCodeAt_to_handle_non-Basic-Multilingual-Plane_characters_if_their_presence_earlier_in_the_string_is_unknown
254 /**
255 * Append a UTF-8 string to the hash buffer
256 * @param str String to append
257 */
258 Md5.prototype.appendStr = function (str) {
259 var buf8 = this._buffer8;
260 var buf32 = this._buffer32;
261 var bufLen = this._bufferLength;
262 var code;
263 var i;
264 for (i = 0; i < str.length; i += 1) {
265 code = str.charCodeAt(i);
266 if (code < 128) {
267 buf8[bufLen++] = code;
268 }
269 else if (code < 0x800) {
270 buf8[bufLen++] = (code >>> 6) + 0xC0;
271 buf8[bufLen++] = code & 0x3F | 0x80;
272 }
273 else if (code < 0xD800 || code > 0xDBFF) {
274 buf8[bufLen++] = (code >>> 12) + 0xE0;
275 buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80;
276 buf8[bufLen++] = (code & 0x3F) | 0x80;
277 }
278 else {
279 code = ((code - 0xD800) * 0x400) + (str.charCodeAt(++i) - 0xDC00) + 0x10000;
280 if (code > 0x10FFFF) {
281 throw new Error('Unicode standard supports code points up to U+10FFFF');
282 }
283 buf8[bufLen++] = (code >>> 18) + 0xF0;
284 buf8[bufLen++] = (code >>> 12 & 0x3F) | 0x80;
285 buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80;
286 buf8[bufLen++] = (code & 0x3F) | 0x80;
287 }
288 if (bufLen >= 64) {
289 this._dataLength += 64;
290 Md5._md5cycle(this._state, buf32);
291 bufLen -= 64;
292 buf32[0] = buf32[16];
293 }
294 }
295 this._bufferLength = bufLen;
296 return this;
297 };
298 /**
299 * Append an ASCII string to the hash buffer
300 * @param str String to append
301 */
302 Md5.prototype.appendAsciiStr = function (str) {
303 var buf8 = this._buffer8;
304 var buf32 = this._buffer32;
305 var bufLen = this._bufferLength;
306 var i;
307 var j = 0;
308 for (;;) {
309 i = Math.min(str.length - j, 64 - bufLen);
310 while (i--) {
311 buf8[bufLen++] = str.charCodeAt(j++);
312 }
313 if (bufLen < 64) {
314 break;
315 }
316 this._dataLength += 64;
317 Md5._md5cycle(this._state, buf32);
318 bufLen = 0;
319 }
320 this._bufferLength = bufLen;
321 return this;
322 };
323 /**
324 * Append a byte array to the hash buffer
325 * @param input array to append
326 */
327 Md5.prototype.appendByteArray = function (input) {
328 var buf8 = this._buffer8;
329 var buf32 = this._buffer32;
330 var bufLen = this._bufferLength;
331 var i;
332 var j = 0;
333 for (;;) {
334 i = Math.min(input.length - j, 64 - bufLen);
335 while (i--) {
336 buf8[bufLen++] = input[j++];
337 }
338 if (bufLen < 64) {
339 break;
340 }
341 this._dataLength += 64;
342 Md5._md5cycle(this._state, buf32);
343 bufLen = 0;
344 }
345 this._bufferLength = bufLen;
346 return this;
347 };
348 /**
349 * Get the state of the hash buffer
350 */
351 Md5.prototype.getState = function () {
352 var s = this._state;
353 return {
354 buffer: String.fromCharCode.apply(null, Array.from(this._buffer8)),
355 buflen: this._bufferLength,
356 length: this._dataLength,
357 state: [s[0], s[1], s[2], s[3]]
358 };
359 };
360 /**
361 * Override the current state of the hash buffer
362 * @param state New hash buffer state
363 */
364 Md5.prototype.setState = function (state) {
365 var buf = state.buffer;
366 var x = state.state;
367 var s = this._state;
368 var i;
369 this._dataLength = state.length;
370 this._bufferLength = state.buflen;
371 s[0] = x[0];
372 s[1] = x[1];
373 s[2] = x[2];
374 s[3] = x[3];
375 for (i = 0; i < buf.length; i += 1) {
376 this._buffer8[i] = buf.charCodeAt(i);
377 }
378 };
379 /**
380 * Hash the current state of the hash buffer and return the result
381 * @param raw Whether to return the value as an `Int32Array`
382 */
383 Md5.prototype.end = function (raw) {
384 if (raw === void 0) { raw = false; }
385 var bufLen = this._bufferLength;
386 var buf8 = this._buffer8;
387 var buf32 = this._buffer32;
388 var i = (bufLen >> 2) + 1;
389 this._dataLength += bufLen;
390 var dataBitsLen = this._dataLength * 8;
391 buf8[bufLen] = 0x80;
392 buf8[bufLen + 1] = buf8[bufLen + 2] = buf8[bufLen + 3] = 0;
393 buf32.set(Md5.buffer32Identity.subarray(i), i);
394 if (bufLen > 55) {
395 Md5._md5cycle(this._state, buf32);
396 buf32.set(Md5.buffer32Identity);
397 }
398 // Do the final computation based on the tail and length
399 // Beware that the final length may not fit in 32 bits so we take care of that
400 if (dataBitsLen <= 0xFFFFFFFF) {
401 buf32[14] = dataBitsLen;
402 }
403 else {
404 var matches = dataBitsLen.toString(16).match(/(.*?)(.{0,8})$/);
405 if (matches === null) {
406 return;
407 }
408 var lo = parseInt(matches[2], 16);
409 var hi = parseInt(matches[1], 16) || 0;
410 buf32[14] = lo;
411 buf32[15] = hi;
412 }
413 Md5._md5cycle(this._state, buf32);
414 return raw ? this._state : Md5._hex(this._state);
415 };
416 // Private Static Variables
417 Md5.stateIdentity = new Int32Array([1732584193, -271733879, -1732584194, 271733878]);
418 Md5.buffer32Identity = new Int32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
419 Md5.hexChars = '0123456789abcdef';
420 Md5.hexOut = [];
421 // Permanent instance is to use for one-call hashing
422 Md5.onePassHasher = new Md5();
423 return Md5;
424}());
425exports.Md5 = Md5;
426if (Md5.hashStr('hello') !== '5d41402abc4b2a76b9719d911017c592') {
427 throw new Error('Md5 self test failed.');
428}
429//# sourceMappingURL=md5.js.map
\No newline at end of file