js-md5 0.8.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,852 @@
1
+ /**
2
+ * [js-md5]{@link https://github.com/emn178/js-md5}
3
+ *
4
+ * @namespace md5
5
+ * @version 0.9.0
6
+ * @author Chen, Yi-Cyuan [emn178@gmail.com]
7
+ * @copyright Chen, Yi-Cyuan 2014-2026
8
+ * @license MIT
9
+ */
10
+ (function () {
11
+
12
+ var INPUT_ERROR = 'input is invalid type';
13
+ var FINALIZE_ERROR = 'finalize already called';
14
+ var WINDOW = typeof window === 'object';
15
+ var root = WINDOW ? window : {};
16
+ if (root.JS_MD5_NO_WINDOW) {
17
+ WINDOW = false;
18
+ }
19
+ var WEB_WORKER = !WINDOW && typeof self === 'object';
20
+ /* istanbul ignore next */
21
+ var NODE_JS = !root.JS_MD5_NO_NODE_JS && !WINDOW && typeof process === 'object' && process.versions && process.versions.node;
22
+ if (NODE_JS) {
23
+ root = global;
24
+ } else if (WEB_WORKER) {
25
+ root = self;
26
+ }
27
+ var COMMON_JS = !root.JS_MD5_NO_COMMON_JS && typeof module === 'object' && module.exports;
28
+ var AMD = typeof define === 'function' && define.amd;
29
+ var HEX_CHARS = '0123456789abcdef'.split('');
30
+ var EXTRA = [128, 32768, 8388608, -2147483648];
31
+ var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
32
+ /* istanbul ignore else */
33
+ {
34
+ OUTPUT_TYPES.push('buffer');
35
+ }
36
+ /* istanbul ignore else */
37
+ {
38
+ OUTPUT_TYPES.push('base64');
39
+ }
40
+ /* istanbul ignore next */
41
+ var BASE64_ENCODE_CHAR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('') ;
42
+
43
+ var blocks = [], buffer8;
44
+ {
45
+ var buffer = new ArrayBuffer(68);
46
+ buffer8 = new Uint8Array(buffer);
47
+ blocks = new Uint32Array(buffer);
48
+ }
49
+
50
+ var isArray = Array.isArray;
51
+
52
+ var isView = ArrayBuffer.isView;
53
+
54
+ // [message: string, isString: bool]
55
+ var formatMessage = function (message) {
56
+ var type = typeof message;
57
+ if (type === 'string') {
58
+ return [message, true];
59
+ }
60
+ if (type !== 'object' || message === null) {
61
+ throw new Error(INPUT_ERROR);
62
+ }
63
+ if (message.constructor === ArrayBuffer) {
64
+ return [new Uint8Array(message), false];
65
+ }
66
+ if (!isArray(message) && !isView(message)) {
67
+ throw new Error(INPUT_ERROR);
68
+ }
69
+ return [message, false];
70
+ };
71
+
72
+ /**
73
+ * @method hex
74
+ * @memberof md5
75
+ * @description Output hash as hex string
76
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
77
+ * @returns {String} Hex string
78
+ * @example
79
+ * md5.hex('The quick brown fox jumps over the lazy dog');
80
+ * // equal to
81
+ * md5('The quick brown fox jumps over the lazy dog');
82
+ */
83
+ /**
84
+ * @method digest
85
+ * @memberof md5
86
+ * @description Output hash as bytes array
87
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
88
+ * @returns {Array} Bytes array
89
+ * @example
90
+ * md5.digest('The quick brown fox jumps over the lazy dog');
91
+ */
92
+ /**
93
+ * @method array
94
+ * @memberof md5
95
+ * @description Output hash as bytes array
96
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
97
+ * @returns {Array} Bytes array
98
+ * @example
99
+ * md5.array('The quick brown fox jumps over the lazy dog');
100
+ */
101
+ /**
102
+ * @method arrayBuffer
103
+ * @memberof md5
104
+ * @description Output hash as ArrayBuffer
105
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
106
+ * @returns {ArrayBuffer} ArrayBuffer
107
+ * @example
108
+ * md5.arrayBuffer('The quick brown fox jumps over the lazy dog');
109
+ */
110
+ /**
111
+ * @method buffer
112
+ * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
113
+ * @memberof md5
114
+ * @description Output hash as ArrayBuffer
115
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
116
+ * @returns {ArrayBuffer} ArrayBuffer
117
+ * @example
118
+ * md5.buffer('The quick brown fox jumps over the lazy dog');
119
+ */
120
+ /**
121
+ * @method base64
122
+ * @memberof md5
123
+ * @description Output hash as base64 string
124
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
125
+ * @returns {String} base64 string
126
+ * @example
127
+ * md5.base64('The quick brown fox jumps over the lazy dog');
128
+ */
129
+ var createOutputMethod = function (outputType) {
130
+ return function (message) {
131
+ return new Md5(true).update(message)[outputType]();
132
+ };
133
+ };
134
+
135
+ /**
136
+ * @method create
137
+ * @memberof md5
138
+ * @description Create Md5 object
139
+ * @returns {Md5} Md5 object.
140
+ * @example
141
+ * var hash = md5.create();
142
+ */
143
+ /**
144
+ * @method update
145
+ * @memberof md5
146
+ * @description Create and update Md5 object
147
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
148
+ * @returns {Md5} Md5 object.
149
+ * @example
150
+ * var hash = md5.update('The quick brown fox jumps over the lazy dog');
151
+ * // equal to
152
+ * var hash = md5.create();
153
+ * hash.update('The quick brown fox jumps over the lazy dog');
154
+ */
155
+ var createMethod = function () {
156
+ var method = createOutputMethod('hex');
157
+ if (NODE_JS) {
158
+ method = nodeWrap(method);
159
+ }
160
+ method.create = function () {
161
+ return new Md5();
162
+ };
163
+ method.update = function (message) {
164
+ return method.create().update(message);
165
+ };
166
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
167
+ var type = OUTPUT_TYPES[i];
168
+ method[type] = createOutputMethod(type);
169
+ }
170
+ return method;
171
+ };
172
+
173
+ var nodeWrap = function (method) {
174
+ var crypto = require('crypto');
175
+ var Buffer = require('buffer').Buffer;
176
+ var bufferFrom = Buffer.from;
177
+ var nodeMethod = function (message) {
178
+ if (typeof message === 'string') {
179
+ return crypto.createHash('md5').update(message, 'utf8').digest('hex');
180
+ } else {
181
+ if (message === null || message === undefined) {
182
+ throw new Error(INPUT_ERROR);
183
+ } else if (message.constructor === ArrayBuffer) {
184
+ message = new Uint8Array(message);
185
+ }
186
+ }
187
+ if (isArray(message) || isView(message) ||
188
+ message.constructor === Buffer) {
189
+ return crypto.createHash('md5').update(bufferFrom(message)).digest('hex');
190
+ } else {
191
+ return method(message);
192
+ }
193
+ };
194
+ return nodeMethod;
195
+ };
196
+
197
+ /**
198
+ * @namespace md5.hmac
199
+ */
200
+ /**
201
+ * @method hex
202
+ * @memberof md5.hmac
203
+ * @description Output hash as hex string
204
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
205
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
206
+ * @returns {String} Hex string
207
+ * @example
208
+ * md5.hmac.hex('key', 'The quick brown fox jumps over the lazy dog');
209
+ * // equal to
210
+ * md5.hmac('key', 'The quick brown fox jumps over the lazy dog');
211
+ */
212
+
213
+ /**
214
+ * @method digest
215
+ * @memberof md5.hmac
216
+ * @description Output hash as bytes array
217
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
218
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
219
+ * @returns {Array} Bytes array
220
+ * @example
221
+ * md5.hmac.digest('key', 'The quick brown fox jumps over the lazy dog');
222
+ */
223
+ /**
224
+ * @method array
225
+ * @memberof md5.hmac
226
+ * @description Output hash as bytes array
227
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
228
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
229
+ * @returns {Array} Bytes array
230
+ * @example
231
+ * md5.hmac.array('key', 'The quick brown fox jumps over the lazy dog');
232
+ */
233
+ /**
234
+ * @method arrayBuffer
235
+ * @memberof md5.hmac
236
+ * @description Output hash as ArrayBuffer
237
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
238
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
239
+ * @returns {ArrayBuffer} ArrayBuffer
240
+ * @example
241
+ * md5.hmac.arrayBuffer('key', 'The quick brown fox jumps over the lazy dog');
242
+ */
243
+ /**
244
+ * @method buffer
245
+ * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
246
+ * @memberof md5.hmac
247
+ * @description Output hash as ArrayBuffer
248
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
249
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
250
+ * @returns {ArrayBuffer} ArrayBuffer
251
+ * @example
252
+ * md5.hmac.buffer('key', 'The quick brown fox jumps over the lazy dog');
253
+ */
254
+ /**
255
+ * @method base64
256
+ * @memberof md5.hmac
257
+ * @description Output hash as base64 string
258
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
259
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
260
+ * @returns {String} base64 string
261
+ * @example
262
+ * md5.hmac.base64('key', 'The quick brown fox jumps over the lazy dog');
263
+ */
264
+ var createHmacOutputMethod = function (outputType) {
265
+ return function (key, message) {
266
+ return new HmacMd5(key, true).update(message)[outputType]();
267
+ };
268
+ };
269
+
270
+ /**
271
+ * @method create
272
+ * @memberof md5.hmac
273
+ * @description Create HmacMd5 object
274
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
275
+ * @returns {HmacMd5} HmacMd5 object.
276
+ * @example
277
+ * var hash = md5.hmac.create('key');
278
+ */
279
+ /**
280
+ * @method update
281
+ * @memberof md5.hmac
282
+ * @description Create and update HmacMd5 object
283
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
284
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
285
+ * @returns {HmacMd5} HmacMd5 object.
286
+ * @example
287
+ * var hash = md5.hmac.update('key', 'The quick brown fox jumps over the lazy dog');
288
+ * // equal to
289
+ * var hash = md5.hmac.create('key');
290
+ * hash.update('The quick brown fox jumps over the lazy dog');
291
+ */
292
+ var createHmacMethod = function () {
293
+ var method = createHmacOutputMethod('hex');
294
+ method.create = function (key) {
295
+ return new HmacMd5(key);
296
+ };
297
+ method.update = function (key, message) {
298
+ return method.create(key).update(message);
299
+ };
300
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
301
+ var type = OUTPUT_TYPES[i];
302
+ method[type] = createHmacOutputMethod(type);
303
+ }
304
+ return method;
305
+ };
306
+
307
+ /**
308
+ * Md5 class
309
+ * @class Md5
310
+ * @description This is internal class.
311
+ * @see {@link md5.create}
312
+ */
313
+ function Md5(sharedMemory) {
314
+ if (sharedMemory) {
315
+ blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
316
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
317
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
318
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
319
+ this.blocks = blocks;
320
+ this.buffer8 = buffer8;
321
+ } else {
322
+ {
323
+ var buffer = new ArrayBuffer(68);
324
+ this.buffer8 = new Uint8Array(buffer);
325
+ this.blocks = new Uint32Array(buffer);
326
+ }
327
+ }
328
+ this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = this.hBytes = 0;
329
+ this.finalized = this.hashed = false;
330
+ this.first = true;
331
+ }
332
+
333
+ /**
334
+ * @method update
335
+ * @memberof Md5
336
+ * @instance
337
+ * @description Update hash
338
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
339
+ * @returns {Md5} Md5 object.
340
+ * @see {@link md5.update}
341
+ */
342
+ Md5.prototype.update = function (message) {
343
+ if (this.finalized) {
344
+ throw new Error(FINALIZE_ERROR);
345
+ }
346
+
347
+ var result = formatMessage(message);
348
+ message = result[0];
349
+ var isString = result[1];
350
+ var code, index = 0, i, length = message.length, blocks = this.blocks;
351
+ var buffer8 = this.buffer8;
352
+
353
+ while (index < length) {
354
+ if (this.hashed) {
355
+ this.hashed = false;
356
+ blocks[0] = blocks[16];
357
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
358
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
359
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
360
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
361
+ }
362
+
363
+ if (isString) {
364
+ {
365
+ for (i = this.start; index < length && i < 64; ++index) {
366
+ code = message.charCodeAt(index);
367
+ if (code < 0x80) {
368
+ buffer8[i++] = code;
369
+ } else if (code < 0x800) {
370
+ buffer8[i++] = 0xc0 | (code >>> 6);
371
+ buffer8[i++] = 0x80 | (code & 0x3f);
372
+ } else if (code < 0xd800 || code >= 0xe000) {
373
+ buffer8[i++] = 0xe0 | (code >>> 12);
374
+ buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f);
375
+ buffer8[i++] = 0x80 | (code & 0x3f);
376
+ } else {
377
+ code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
378
+ buffer8[i++] = 0xf0 | (code >>> 18);
379
+ buffer8[i++] = 0x80 | ((code >>> 12) & 0x3f);
380
+ buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f);
381
+ buffer8[i++] = 0x80 | (code & 0x3f);
382
+ }
383
+ }
384
+ }
385
+ } else {
386
+ {
387
+ for (i = this.start; index < length && i < 64; ++index) {
388
+ buffer8[i++] = message[index];
389
+ }
390
+ }
391
+ }
392
+ this.lastByteIndex = i;
393
+ this.bytes += i - this.start;
394
+ if (i >= 64) {
395
+ this.start = i - 64;
396
+ this.hash();
397
+ this.hashed = true;
398
+ } else {
399
+ this.start = i;
400
+ }
401
+ }
402
+ if (this.bytes > 4294967295) {
403
+ this.hBytes += this.bytes / 4294967296 << 0;
404
+ this.bytes = this.bytes % 4294967296;
405
+ }
406
+ return this;
407
+ };
408
+
409
+ Md5.prototype.finalize = function () {
410
+ if (this.finalized) {
411
+ return;
412
+ }
413
+ this.finalized = true;
414
+ var blocks = this.blocks, i = this.lastByteIndex;
415
+ blocks[i >>> 2] |= EXTRA[i & 3];
416
+ if (i >= 56) {
417
+ if (!this.hashed) {
418
+ this.hash();
419
+ }
420
+ blocks[0] = blocks[16];
421
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
422
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
423
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
424
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
425
+ }
426
+ blocks[14] = this.bytes << 3;
427
+ blocks[15] = this.hBytes << 3 | this.bytes >>> 29;
428
+ this.hash();
429
+ };
430
+
431
+ Md5.prototype.hash = function () {
432
+ var a, b, c, d, bc, da, blocks = this.blocks;
433
+
434
+ if (this.first) {
435
+ a = blocks[0] - 680876937;
436
+ a = (a << 7 | a >>> 25) - 271733879 << 0;
437
+ d = (-1732584194 ^ a & 2004318071) + blocks[1] - 117830708;
438
+ d = (d << 12 | d >>> 20) + a << 0;
439
+ c = (-271733879 ^ (d & (a ^ -271733879))) + blocks[2] - 1126478375;
440
+ c = (c << 17 | c >>> 15) + d << 0;
441
+ b = (a ^ (c & (d ^ a))) + blocks[3] - 1316259209;
442
+ b = (b << 22 | b >>> 10) + c << 0;
443
+ } else {
444
+ a = this.h0;
445
+ b = this.h1;
446
+ c = this.h2;
447
+ d = this.h3;
448
+ a += (d ^ (b & (c ^ d))) + blocks[0] - 680876936;
449
+ a = (a << 7 | a >>> 25) + b << 0;
450
+ d += (c ^ (a & (b ^ c))) + blocks[1] - 389564586;
451
+ d = (d << 12 | d >>> 20) + a << 0;
452
+ c += (b ^ (d & (a ^ b))) + blocks[2] + 606105819;
453
+ c = (c << 17 | c >>> 15) + d << 0;
454
+ b += (a ^ (c & (d ^ a))) + blocks[3] - 1044525330;
455
+ b = (b << 22 | b >>> 10) + c << 0;
456
+ }
457
+
458
+ a += (d ^ (b & (c ^ d))) + blocks[4] - 176418897;
459
+ a = (a << 7 | a >>> 25) + b << 0;
460
+ d += (c ^ (a & (b ^ c))) + blocks[5] + 1200080426;
461
+ d = (d << 12 | d >>> 20) + a << 0;
462
+ c += (b ^ (d & (a ^ b))) + blocks[6] - 1473231341;
463
+ c = (c << 17 | c >>> 15) + d << 0;
464
+ b += (a ^ (c & (d ^ a))) + blocks[7] - 45705983;
465
+ b = (b << 22 | b >>> 10) + c << 0;
466
+ a += (d ^ (b & (c ^ d))) + blocks[8] + 1770035416;
467
+ a = (a << 7 | a >>> 25) + b << 0;
468
+ d += (c ^ (a & (b ^ c))) + blocks[9] - 1958414417;
469
+ d = (d << 12 | d >>> 20) + a << 0;
470
+ c += (b ^ (d & (a ^ b))) + blocks[10] - 42063;
471
+ c = (c << 17 | c >>> 15) + d << 0;
472
+ b += (a ^ (c & (d ^ a))) + blocks[11] - 1990404162;
473
+ b = (b << 22 | b >>> 10) + c << 0;
474
+ a += (d ^ (b & (c ^ d))) + blocks[12] + 1804603682;
475
+ a = (a << 7 | a >>> 25) + b << 0;
476
+ d += (c ^ (a & (b ^ c))) + blocks[13] - 40341101;
477
+ d = (d << 12 | d >>> 20) + a << 0;
478
+ c += (b ^ (d & (a ^ b))) + blocks[14] - 1502002290;
479
+ c = (c << 17 | c >>> 15) + d << 0;
480
+ b += (a ^ (c & (d ^ a))) + blocks[15] + 1236535329;
481
+ b = (b << 22 | b >>> 10) + c << 0;
482
+ a += (c ^ (d & (b ^ c))) + blocks[1] - 165796510;
483
+ a = (a << 5 | a >>> 27) + b << 0;
484
+ d += (b ^ (c & (a ^ b))) + blocks[6] - 1069501632;
485
+ d = (d << 9 | d >>> 23) + a << 0;
486
+ c += (a ^ (b & (d ^ a))) + blocks[11] + 643717713;
487
+ c = (c << 14 | c >>> 18) + d << 0;
488
+ b += (d ^ (a & (c ^ d))) + blocks[0] - 373897302;
489
+ b = (b << 20 | b >>> 12) + c << 0;
490
+ a += (c ^ (d & (b ^ c))) + blocks[5] - 701558691;
491
+ a = (a << 5 | a >>> 27) + b << 0;
492
+ d += (b ^ (c & (a ^ b))) + blocks[10] + 38016083;
493
+ d = (d << 9 | d >>> 23) + a << 0;
494
+ c += (a ^ (b & (d ^ a))) + blocks[15] - 660478335;
495
+ c = (c << 14 | c >>> 18) + d << 0;
496
+ b += (d ^ (a & (c ^ d))) + blocks[4] - 405537848;
497
+ b = (b << 20 | b >>> 12) + c << 0;
498
+ a += (c ^ (d & (b ^ c))) + blocks[9] + 568446438;
499
+ a = (a << 5 | a >>> 27) + b << 0;
500
+ d += (b ^ (c & (a ^ b))) + blocks[14] - 1019803690;
501
+ d = (d << 9 | d >>> 23) + a << 0;
502
+ c += (a ^ (b & (d ^ a))) + blocks[3] - 187363961;
503
+ c = (c << 14 | c >>> 18) + d << 0;
504
+ b += (d ^ (a & (c ^ d))) + blocks[8] + 1163531501;
505
+ b = (b << 20 | b >>> 12) + c << 0;
506
+ a += (c ^ (d & (b ^ c))) + blocks[13] - 1444681467;
507
+ a = (a << 5 | a >>> 27) + b << 0;
508
+ d += (b ^ (c & (a ^ b))) + blocks[2] - 51403784;
509
+ d = (d << 9 | d >>> 23) + a << 0;
510
+ c += (a ^ (b & (d ^ a))) + blocks[7] + 1735328473;
511
+ c = (c << 14 | c >>> 18) + d << 0;
512
+ b += (d ^ (a & (c ^ d))) + blocks[12] - 1926607734;
513
+ b = (b << 20 | b >>> 12) + c << 0;
514
+ bc = b ^ c;
515
+ a += (bc ^ d) + blocks[5] - 378558;
516
+ a = (a << 4 | a >>> 28) + b << 0;
517
+ d += (bc ^ a) + blocks[8] - 2022574463;
518
+ d = (d << 11 | d >>> 21) + a << 0;
519
+ da = d ^ a;
520
+ c += (da ^ b) + blocks[11] + 1839030562;
521
+ c = (c << 16 | c >>> 16) + d << 0;
522
+ b += (da ^ c) + blocks[14] - 35309556;
523
+ b = (b << 23 | b >>> 9) + c << 0;
524
+ bc = b ^ c;
525
+ a += (bc ^ d) + blocks[1] - 1530992060;
526
+ a = (a << 4 | a >>> 28) + b << 0;
527
+ d += (bc ^ a) + blocks[4] + 1272893353;
528
+ d = (d << 11 | d >>> 21) + a << 0;
529
+ da = d ^ a;
530
+ c += (da ^ b) + blocks[7] - 155497632;
531
+ c = (c << 16 | c >>> 16) + d << 0;
532
+ b += (da ^ c) + blocks[10] - 1094730640;
533
+ b = (b << 23 | b >>> 9) + c << 0;
534
+ bc = b ^ c;
535
+ a += (bc ^ d) + blocks[13] + 681279174;
536
+ a = (a << 4 | a >>> 28) + b << 0;
537
+ d += (bc ^ a) + blocks[0] - 358537222;
538
+ d = (d << 11 | d >>> 21) + a << 0;
539
+ da = d ^ a;
540
+ c += (da ^ b) + blocks[3] - 722521979;
541
+ c = (c << 16 | c >>> 16) + d << 0;
542
+ b += (da ^ c) + blocks[6] + 76029189;
543
+ b = (b << 23 | b >>> 9) + c << 0;
544
+ bc = b ^ c;
545
+ a += (bc ^ d) + blocks[9] - 640364487;
546
+ a = (a << 4 | a >>> 28) + b << 0;
547
+ d += (bc ^ a) + blocks[12] - 421815835;
548
+ d = (d << 11 | d >>> 21) + a << 0;
549
+ da = d ^ a;
550
+ c += (da ^ b) + blocks[15] + 530742520;
551
+ c = (c << 16 | c >>> 16) + d << 0;
552
+ b += (da ^ c) + blocks[2] - 995338651;
553
+ b = (b << 23 | b >>> 9) + c << 0;
554
+ a += (c ^ (b | ~d)) + blocks[0] - 198630844;
555
+ a = (a << 6 | a >>> 26) + b << 0;
556
+ d += (b ^ (a | ~c)) + blocks[7] + 1126891415;
557
+ d = (d << 10 | d >>> 22) + a << 0;
558
+ c += (a ^ (d | ~b)) + blocks[14] - 1416354905;
559
+ c = (c << 15 | c >>> 17) + d << 0;
560
+ b += (d ^ (c | ~a)) + blocks[5] - 57434055;
561
+ b = (b << 21 | b >>> 11) + c << 0;
562
+ a += (c ^ (b | ~d)) + blocks[12] + 1700485571;
563
+ a = (a << 6 | a >>> 26) + b << 0;
564
+ d += (b ^ (a | ~c)) + blocks[3] - 1894986606;
565
+ d = (d << 10 | d >>> 22) + a << 0;
566
+ c += (a ^ (d | ~b)) + blocks[10] - 1051523;
567
+ c = (c << 15 | c >>> 17) + d << 0;
568
+ b += (d ^ (c | ~a)) + blocks[1] - 2054922799;
569
+ b = (b << 21 | b >>> 11) + c << 0;
570
+ a += (c ^ (b | ~d)) + blocks[8] + 1873313359;
571
+ a = (a << 6 | a >>> 26) + b << 0;
572
+ d += (b ^ (a | ~c)) + blocks[15] - 30611744;
573
+ d = (d << 10 | d >>> 22) + a << 0;
574
+ c += (a ^ (d | ~b)) + blocks[6] - 1560198380;
575
+ c = (c << 15 | c >>> 17) + d << 0;
576
+ b += (d ^ (c | ~a)) + blocks[13] + 1309151649;
577
+ b = (b << 21 | b >>> 11) + c << 0;
578
+ a += (c ^ (b | ~d)) + blocks[4] - 145523070;
579
+ a = (a << 6 | a >>> 26) + b << 0;
580
+ d += (b ^ (a | ~c)) + blocks[11] - 1120210379;
581
+ d = (d << 10 | d >>> 22) + a << 0;
582
+ c += (a ^ (d | ~b)) + blocks[2] + 718787259;
583
+ c = (c << 15 | c >>> 17) + d << 0;
584
+ b += (d ^ (c | ~a)) + blocks[9] - 343485551;
585
+ b = (b << 21 | b >>> 11) + c << 0;
586
+
587
+ if (this.first) {
588
+ this.h0 = a + 1732584193 << 0;
589
+ this.h1 = b - 271733879 << 0;
590
+ this.h2 = c - 1732584194 << 0;
591
+ this.h3 = d + 271733878 << 0;
592
+ this.first = false;
593
+ } else {
594
+ this.h0 = this.h0 + a << 0;
595
+ this.h1 = this.h1 + b << 0;
596
+ this.h2 = this.h2 + c << 0;
597
+ this.h3 = this.h3 + d << 0;
598
+ }
599
+ };
600
+
601
+ /**
602
+ * @method hex
603
+ * @memberof Md5
604
+ * @instance
605
+ * @description Output hash as hex string
606
+ * @returns {String} Hex string
607
+ * @see {@link md5.hex}
608
+ * @example
609
+ * hash.hex();
610
+ */
611
+ Md5.prototype.hex = function () {
612
+ this.finalize();
613
+
614
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
615
+
616
+ return HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
617
+ HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] +
618
+ HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] +
619
+ HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] +
620
+ HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
621
+ HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] +
622
+ HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] +
623
+ HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] +
624
+ HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
625
+ HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] +
626
+ HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] +
627
+ HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] +
628
+ HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
629
+ HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] +
630
+ HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] +
631
+ HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F];
632
+ };
633
+
634
+ /**
635
+ * @method toString
636
+ * @memberof Md5
637
+ * @instance
638
+ * @description Output hash as hex string
639
+ * @returns {String} Hex string
640
+ * @see {@link md5.hex}
641
+ * @example
642
+ * hash.toString();
643
+ */
644
+ Md5.prototype.toString = Md5.prototype.hex;
645
+
646
+ /**
647
+ * @method digest
648
+ * @memberof Md5
649
+ * @instance
650
+ * @description Output hash as bytes array
651
+ * @returns {Array} Bytes array
652
+ * @see {@link md5.digest}
653
+ * @example
654
+ * hash.digest();
655
+ */
656
+ Md5.prototype.digest = function () {
657
+ this.finalize();
658
+
659
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
660
+ return [
661
+ h0 & 0xFF, (h0 >>> 8) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 24) & 0xFF,
662
+ h1 & 0xFF, (h1 >>> 8) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 24) & 0xFF,
663
+ h2 & 0xFF, (h2 >>> 8) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 24) & 0xFF,
664
+ h3 & 0xFF, (h3 >>> 8) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 24) & 0xFF
665
+ ];
666
+ };
667
+
668
+ /**
669
+ * @method array
670
+ * @memberof Md5
671
+ * @instance
672
+ * @description Output hash as bytes array
673
+ * @returns {Array} Bytes array
674
+ * @see {@link md5.array}
675
+ * @example
676
+ * hash.array();
677
+ */
678
+ Md5.prototype.array = Md5.prototype.digest;
679
+
680
+ /**
681
+ * @method arrayBuffer
682
+ * @memberof Md5
683
+ * @instance
684
+ * @description Output hash as ArrayBuffer
685
+ * @returns {ArrayBuffer} ArrayBuffer
686
+ * @see {@link md5.arrayBuffer}
687
+ * @example
688
+ * hash.arrayBuffer();
689
+ */
690
+ Md5.prototype.arrayBuffer = function () {
691
+ this.finalize();
692
+
693
+ var buffer = new ArrayBuffer(16);
694
+ var blocks = new Uint32Array(buffer);
695
+ blocks[0] = this.h0;
696
+ blocks[1] = this.h1;
697
+ blocks[2] = this.h2;
698
+ blocks[3] = this.h3;
699
+ return buffer;
700
+ };
701
+
702
+ /**
703
+ * @method buffer
704
+ * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
705
+ * @memberof Md5
706
+ * @instance
707
+ * @description Output hash as ArrayBuffer
708
+ * @returns {ArrayBuffer} ArrayBuffer
709
+ * @see {@link md5.buffer}
710
+ * @example
711
+ * hash.buffer();
712
+ */
713
+ /* istanbul ignore else */
714
+ {
715
+ Md5.prototype.buffer = Md5.prototype.arrayBuffer;
716
+ }
717
+
718
+ /**
719
+ * @method base64
720
+ * @memberof Md5
721
+ * @instance
722
+ * @description Output hash as base64 string
723
+ * @returns {String} base64 string
724
+ * @see {@link md5.base64}
725
+ * @example
726
+ * hash.base64();
727
+ */
728
+ /* istanbul ignore else */
729
+ {
730
+ Md5.prototype.base64 = function () {
731
+ var v1, v2, v3, base64Str = '', bytes = this.array();
732
+ for (var i = 0; i < 15;) {
733
+ v1 = bytes[i++];
734
+ v2 = bytes[i++];
735
+ v3 = bytes[i++];
736
+ base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
737
+ BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
738
+ BASE64_ENCODE_CHAR[(v2 << 2 | v3 >>> 6) & 63] +
739
+ BASE64_ENCODE_CHAR[v3 & 63];
740
+ }
741
+ v1 = bytes[i];
742
+ base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
743
+ BASE64_ENCODE_CHAR[(v1 << 4) & 63] +
744
+ '==';
745
+ return base64Str;
746
+ };
747
+ }
748
+
749
+ /**
750
+ * HmacMd5 class
751
+ * @class HmacMd5
752
+ * @extends Md5
753
+ * @description This is internal class.
754
+ * @see {@link md5.hmac.create}
755
+ */
756
+ function HmacMd5(key, sharedMemory) {
757
+ var i, result = formatMessage(key);
758
+ key = result[0];
759
+ if (result[1]) {
760
+ var bytes = [], length = key.length, index = 0, code;
761
+ for (i = 0; i < length; ++i) {
762
+ code = key.charCodeAt(i);
763
+ if (code < 0x80) {
764
+ bytes[index++] = code;
765
+ } else if (code < 0x800) {
766
+ bytes[index++] = (0xc0 | (code >>> 6));
767
+ bytes[index++] = (0x80 | (code & 0x3f));
768
+ } else if (code < 0xd800 || code >= 0xe000) {
769
+ bytes[index++] = (0xe0 | (code >>> 12));
770
+ bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
771
+ bytes[index++] = (0x80 | (code & 0x3f));
772
+ } else {
773
+ code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
774
+ bytes[index++] = (0xf0 | (code >>> 18));
775
+ bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
776
+ bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
777
+ bytes[index++] = (0x80 | (code & 0x3f));
778
+ }
779
+ }
780
+ key = bytes;
781
+ }
782
+
783
+ if (key.length > 64) {
784
+ key = (new Md5(true)).update(key).array();
785
+ }
786
+
787
+ var oKeyPad = [], iKeyPad = [];
788
+ for (i = 0; i < 64; ++i) {
789
+ var b = key[i] || 0;
790
+ oKeyPad[i] = 0x5c ^ b;
791
+ iKeyPad[i] = 0x36 ^ b;
792
+ }
793
+
794
+ Md5.call(this, sharedMemory);
795
+
796
+ this.update(iKeyPad);
797
+ this.oKeyPad = oKeyPad;
798
+ this.inner = true;
799
+ this.sharedMemory = sharedMemory;
800
+ }
801
+ /* istanbul ignore else */
802
+ {
803
+ HmacMd5.prototype = new Md5();
804
+
805
+ HmacMd5.prototype.finalize = function () {
806
+ Md5.prototype.finalize.call(this);
807
+ if (this.inner) {
808
+ this.inner = false;
809
+ var innerHash = this.array();
810
+ Md5.call(this, this.sharedMemory);
811
+ this.update(this.oKeyPad);
812
+ this.update(innerHash);
813
+ Md5.prototype.finalize.call(this);
814
+ }
815
+ };
816
+ }
817
+
818
+ var exports = createMethod();
819
+ exports.md5 = exports;
820
+ /* istanbul ignore else */
821
+ {
822
+ exports.md5.hmac = createHmacMethod();
823
+ }
824
+
825
+ if (COMMON_JS) {
826
+ module.exports = exports;
827
+ } else {
828
+ /**
829
+ * @method md5
830
+ * @description Md5 hash function, export to global in browsers.
831
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
832
+ * @returns {String} md5 hashes
833
+ * @example
834
+ * md5(''); // d41d8cd98f00b204e9800998ecf8427e
835
+ * md5('The quick brown fox jumps over the lazy dog'); // 9e107d9d372bb6826bd81d3542a419d6
836
+ * md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffaddf22cbd0
837
+ *
838
+ * // It also supports UTF-8 encoding
839
+ * md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07
840
+ *
841
+ * // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
842
+ * md5([]); // d41d8cd98f00b204e9800998ecf8427e
843
+ * md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
844
+ */
845
+ root.md5 = exports;
846
+ if (AMD) {
847
+ define(function () {
848
+ return exports;
849
+ });
850
+ }
851
+ }
852
+ })();