2 * [js-md4]{@link https://github.com/emn178/js-md4}
6 * @author Yi-Cyuan Chen [emn178@gmail.com]
7 * @copyright Yi-Cyuan Chen 2015-2027
10/*jslint bitwise: true */
14 var root = typeof window === 'object' ? window : {};
15 var NODE_JS = !root.JS_MD4_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
19 var COMMON_JS = !root.JS_MD4_NO_COMMON_JS && typeof module === 'object' && module.exports;
20 var AMD = typeof define === 'function' && define.amd;
21 var ARRAY_BUFFER = !root.JS_MD4_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
22 var HEX_CHARS = '0123456789abcdef'.split('');
23 var EXTRA = [128, 32768, 8388608, -2147483648];
24 var SHIFT = [0, 8, 16, 24];
25 var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer'];
27 var blocks = [], buffer8;
29 var buffer = new ArrayBuffer(68);
30 buffer8 = new Uint8Array(buffer);
31 blocks = new Uint32Array(buffer);
37 * @description Output hash as hex string
38 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
39 * @returns {String} Hex string
41 * md4.hex('The quick brown fox jumps over the lazy dog');
43 * md4('The quick brown fox jumps over the lazy dog');
48 * @description Output hash as bytes array
49 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
50 * @returns {Array} Bytes array
52 * md4.digest('The quick brown fox jumps over the lazy dog');
57 * @description Output hash as bytes array
58 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
59 * @returns {Array} Bytes array
61 * md4.array('The quick brown fox jumps over the lazy dog');
66 * @description Output hash as ArrayBuffer
67 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
68 * @returns {ArrayBuffer} ArrayBuffer
70 * md4.buffer('The quick brown fox jumps over the lazy dog');
72 var createOutputMethod = function (outputType) {
73 return function(message) {
74 return new Md4(true).update(message)[outputType]();
81 * @description Create Md4 object
82 * @returns {Md4} MD4 object.
84 * var hash = md4.create();
89 * @description Create and update Md4 object
90 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
91 * @returns {Md4} MD4 object.
93 * var hash = md4.update('The quick brown fox jumps over the lazy dog');
95 * var hash = md4.create();
96 * hash.update('The quick brown fox jumps over the lazy dog');
98 var createMethod = function () {
99 var method = createOutputMethod('hex');
100 method.create = function () {
103 method.update = function (message) {
104 return method.create().update(message);
106 for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
107 var type = OUTPUT_TYPES[i];
108 method[type] = createOutputMethod(type);
113 var nodeWrap = function (method) {
114 var crypto = require('crypto');
115 var Buffer = require('buffer').Buffer;
116 var nodeMethod = function (message) {
117 if (typeof message === 'string') {
118 return crypto.createHash('md4').update(message, 'utf8').digest('hex');
119 } else if (ARRAY_BUFFER && message instanceof ArrayBuffer) {
120 message = new Uint8Array(message);
121 } else if (message.length === undefined) {
122 return method(message);
124 return crypto.createHash('md4').update(Buffer.from(message)).digest('hex');
132 * @description This is internal class.
133 * @see {@link md4.create}
135 function Md4(sharedMemory) {
137 blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
138 blocks[4] = blocks[5] = blocks[6] = blocks[7] =
139 blocks[8] = blocks[9] = blocks[10] = blocks[11] =
140 blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
141 this.blocks = blocks;
142 this.buffer8 = buffer8;
145 var buffer = new ArrayBuffer(68);
146 this.buffer8 = new Uint8Array(buffer);
147 this.blocks = new Uint32Array(buffer);
149 this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
152 this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = 0;
153 this.finalized = this.hashed = false;
161 * @description Update hash
162 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
163 * @returns {Md4} MD4 object.
164 * @see {@link md4.update}
166 Md4.prototype.update = function (message) {
167 if (this.finalized) {
170 var notString = typeof message !== 'string';
171 if (notString && ARRAY_BUFFER && message instanceof ArrayBuffer) {
172 message = new Uint8Array(message);
174 var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
175 var buffer8 = this.buffer8;
177 while (index < length) {
180 blocks[0] = blocks[16];
181 blocks[16] = blocks[1] = blocks[2] = blocks[3] =
182 blocks[4] = blocks[5] = blocks[6] = blocks[7] =
183 blocks[8] = blocks[9] = blocks[10] = blocks[11] =
184 blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
189 for (i = this.start; index < length && i < 64; ++index) {
190 buffer8[i++] = message[index];
193 for (i = this.start; index < length && i < 64; ++index) {
194 blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
199 for (i = this.start; index < length && i < 64; ++index) {
200 code = message.charCodeAt(index);
203 } else if (code < 0x800) {
204 buffer8[i++] = 0xc0 | (code >> 6);
205 buffer8[i++] = 0x80 | (code & 0x3f);
206 } else if (code < 0xd800 || code >= 0xe000) {
207 buffer8[i++] = 0xe0 | (code >> 12);
208 buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
209 buffer8[i++] = 0x80 | (code & 0x3f);
211 code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
212 buffer8[i++] = 0xf0 | (code >> 18);
213 buffer8[i++] = 0x80 | ((code >> 12) & 0x3f);
214 buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
215 buffer8[i++] = 0x80 | (code & 0x3f);
219 for (i = this.start; index < length && i < 64; ++index) {
220 code = message.charCodeAt(index);
222 blocks[i >> 2] |= code << SHIFT[i++ & 3];
223 } else if (code < 0x800) {
224 blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
225 blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
226 } else if (code < 0xd800 || code >= 0xe000) {
227 blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
228 blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
229 blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
231 code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
232 blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
233 blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
234 blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
235 blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
240 this.lastByteIndex = i;
241 this.bytes += i - this.start;
253 Md4.prototype.finalize = function () {
254 if (this.finalized) {
257 this.finalized = true;
258 var blocks = this.blocks, i = this.lastByteIndex;
259 blocks[i >> 2] |= EXTRA[i & 3];
264 blocks[0] = blocks[16];
265 blocks[16] = blocks[1] = blocks[2] = blocks[3] =
266 blocks[4] = blocks[5] = blocks[6] = blocks[7] =
267 blocks[8] = blocks[9] = blocks[10] = blocks[11] =
268 blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
270 blocks[14] = this.bytes << 3;
274 Md4.prototype.hash = function () {
275 var a, b, c, d, ab, bc, cd, da, blocks = this.blocks;
279 a = (a << 3) | (a >>> 29);
280 d = ((a & 0xefcdab89) | (~a & 0x98badcfe)) + blocks[1] + 271733878;
281 d = (d << 7) | (d >>> 25);
282 c = ((d & a) | (~d & 0xefcdab89)) + blocks[2] - 1732584194;
283 c = (c << 11) | (c >>> 21);
284 b = ((c & d) | (~c & a)) + blocks[3] - 271733879;
285 b = (b << 19) | (b >>> 13);
291 a += ((b & c) | (~b & d)) + blocks[0];
292 a = (a << 3) | (a >>> 29);
293 d += ((a & b) | (~a & c)) + blocks[1];
294 d = (d << 7) | (d >>> 25);
295 c += ((d & a) | (~d & b)) + blocks[2];
296 c = (c << 11) | (c >>> 21);
297 b += ((c & d) | (~c & a)) + blocks[3];
298 b = (b << 19) | (b >>> 13);
300 a += ((b & c) | (~b & d)) + blocks[4];
301 a = (a << 3) | (a >>> 29);
302 d += ((a & b) | (~a & c)) + blocks[5];
303 d = (d << 7) | (d >>> 25);
304 c += ((d & a) | (~d & b)) + blocks[6];
305 c = (c << 11) | (c >>> 21);
306 b += ((c & d) | (~c & a)) + blocks[7];
307 b = (b << 19) | (b >>> 13);
308 a += ((b & c) | (~b & d)) + blocks[8];
309 a = (a << 3) | (a >>> 29);
310 d += ((a & b) | (~a & c)) + blocks[9];
311 d = (d << 7) | (d >>> 25);
312 c += ((d & a) | (~d & b)) + blocks[10];
313 c = (c << 11) | (c >>> 21);
314 b += ((c & d) | (~c & a)) + blocks[11];
315 b = (b << 19) | (b >>> 13);
316 a += ((b & c) | (~b & d)) + blocks[12];
317 a = (a << 3) | (a >>> 29);
318 d += ((a & b) | (~a & c)) + blocks[13];
319 d = (d << 7) | (d >>> 25);
320 c += ((d & a) | (~d & b)) + blocks[14];
321 c = (c << 11) | (c >>> 21);
322 b += ((c & d) | (~c & a)) + blocks[15];
323 b = (b << 19) | (b >>> 13);
326 a += (bc | (b & d) | (c & d)) + blocks[0] + 1518500249;
327 a = (a << 3) | (a >>> 29);
329 d += (ab | (a & c) | bc) + blocks[4] + 1518500249;
330 d = (d << 5) | (d >>> 27);
332 c += (da | (d & b) | ab) + blocks[8] + 1518500249;
333 c = (c << 9) | (c >>> 23);
335 b += (cd | (c & a) | da) + blocks[12] + 1518500249;
336 b = (b << 13) | (b >>> 19);
338 a += (bc | (b & d) | cd) + blocks[1] + 1518500249;
339 a = (a << 3) | (a >>> 29);
341 d += (ab | (a & c) | bc) + blocks[5] + 1518500249;
342 d = (d << 5) | (d >>> 27);
344 c += (da | (d & b) | ab) + blocks[9] + 1518500249;
345 c = (c << 9) | (c >>> 23);
347 b += (cd | (c & a) | da) + blocks[13] + 1518500249;
348 b = (b << 13) | (b >>> 19);
350 a += (bc | (b & d) | cd) + blocks[2] + 1518500249;
351 a = (a << 3) | (a >>> 29);
353 d += (ab | (a & c) | bc) + blocks[6] + 1518500249;
354 d = (d << 5) | (d >>> 27);
356 c += (da | (d & b) | ab) + blocks[10] + 1518500249;
357 c = (c << 9) | (c >>> 23);
359 b += (cd | (c & a) | da) + blocks[14] + 1518500249;
360 b = (b << 13) | (b >>> 19);
362 a += (bc | (b & d) | cd) + blocks[3] + 1518500249;
363 a = (a << 3) | (a >>> 29);
365 d += (ab | (a & c) | bc) + blocks[7] + 1518500249;
366 d = (d << 5) | (d >>> 27);
368 c += (da | (d & b) | ab) + blocks[11] + 1518500249;
369 c = (c << 9) | (c >>> 23);
370 b += ((c & d) | (c & a) | da) + blocks[15] + 1518500249;
371 b = (b << 13) | (b >>> 19);
374 a += (bc ^ d) + blocks[0] + 1859775393;
375 a = (a << 3) | (a >>> 29);
376 d += (bc ^ a) + blocks[8] + 1859775393;
377 d = (d << 9) | (d >>> 23);
379 c += (da ^ b) + blocks[4] + 1859775393;
380 c = (c << 11) | (c >>> 21);
381 b += (da ^ c) + blocks[12] + 1859775393;
382 b = (b << 15) | (b >>> 17);
384 a += (bc ^ d) + blocks[2] + 1859775393;
385 a = (a << 3) | (a >>> 29);
386 d += (bc ^ a) + blocks[10] + 1859775393;
387 d = (d << 9) | (d >>> 23);
389 c += (da ^ b) + blocks[6] + 1859775393;
390 c = (c << 11) | (c >>> 21);
391 b += (da ^ c) + blocks[14] + 1859775393;
392 b = (b << 15) | (b >>> 17);
394 a += (bc ^ d) + blocks[1] + 1859775393;
395 a = (a << 3) | (a >>> 29);
396 d += (bc ^ a) + blocks[9] + 1859775393;
397 d = (d << 9) | (d >>> 23);
399 c += (da ^ b) + blocks[5] + 1859775393;
400 c = (c << 11) | (c >>> 21);
401 b += (da ^ c) + blocks[13] + 1859775393;
402 b = (b << 15) | (b >>> 17);
404 a += (bc ^ d) + blocks[3] + 1859775393;
405 a = (a << 3) | (a >>> 29);
406 d += (bc ^ a) + blocks[11] + 1859775393;
407 d = (d << 9) | (d >>> 23);
409 c += (da ^ b) + blocks[7] + 1859775393;
410 c = (c << 11) | (c >>> 21);
411 b += (da ^ c) + blocks[15] + 1859775393;
412 b = (b << 15) | (b >>> 17);
415 this.h0 = a + 1732584193 << 0;
416 this.h1 = b - 271733879 << 0;
417 this.h2 = c - 1732584194 << 0;
418 this.h3 = d + 271733878 << 0;
421 this.h0 = this.h0 + a << 0;
422 this.h1 = this.h1 + b << 0;
423 this.h2 = this.h2 + c << 0;
424 this.h3 = this.h3 + d << 0;
432 * @description Output hash as hex string
433 * @returns {String} Hex string
434 * @see {@link md4.hex}
438 Md4.prototype.hex = function () {
441 var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
443 return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
444 HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
445 HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
446 HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
447 HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
448 HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
449 HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
450 HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
451 HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
452 HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
453 HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
454 HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
455 HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
456 HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
457 HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
458 HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F];
465 * @description Output hash as hex string
466 * @returns {String} Hex string
467 * @see {@link md4.hex}
471 Md4.prototype.toString = Md4.prototype.hex;
477 * @description Output hash as bytes array
478 * @returns {Array} Bytes array
479 * @see {@link md4.digest}
483 Md4.prototype.digest = function() {
486 var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
488 h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF,
489 h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF,
490 h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF,
491 h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF
499 * @description Output hash as bytes array
500 * @returns {Array} Bytes array
501 * @see {@link md4.array}
505 Md4.prototype.array = Md4.prototype.digest;
508 * @method arrayBuffer
511 * @description Output hash as ArrayBuffer
512 * @returns {ArrayBuffer} ArrayBuffer
513 * @see {@link md4.arrayBuffer}
515 * hash.arrayBuffer();
517 Md4.prototype.arrayBuffer = function() {
520 var buffer = new ArrayBuffer(16);
521 var blocks = new Uint32Array(buffer);
531 * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
534 * @description Output hash as ArrayBuffer
535 * @returns {ArrayBuffer} ArrayBuffer
536 * @see {@link md4.buffer}
540 Md4.prototype.buffer = Md4.prototype.arrayBuffer;
542 var exports = createMethod();
545 module.exports = exports;
549 * @description MD4 hash function, export to global in browsers.
550 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
551 * @returns {String} md4 hashes
553 * md4(''); // 31d6cfe0d16ae931b73c59d7e0c089c0
554 * md4('The quick brown fox jumps over the lazy dog'); // 1bee69a46ba811185c194762abaeae90
555 * md4('The quick brown fox jumps over the lazy dog.'); // 2812c6c7136898c51f6f6739ad08750e
557 * // It also supports UTF-8 encoding
558 * md4('中文'); // 223088bf7bd45a16436b15360c5fc5a0
560 * // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
561 * md4([]); // 31d6cfe0d16ae931b73c59d7e0c089c0
562 * md4(new Uint8Array([])); // 31d6cfe0d16ae931b73c59d7e0c089c0