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