EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
md4.js
Go to the documentation of this file.
1/**
2 * [js-md4]{@link https://github.com/emn178/js-md4}
3 *
4 * @namespace md4
5 * @version 0.3.2
6 * @author Yi-Cyuan Chen [emn178@gmail.com]
7 * @copyright Yi-Cyuan Chen 2015-2027
8 * @license MIT
9 */
10/*jslint bitwise: true */
11(function () {
12 'use strict';
13
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;
16 if (NODE_JS) {
17 root = global;
18 }
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'];
26
27 var blocks = [], buffer8;
28 if (ARRAY_BUFFER) {
29 var buffer = new ArrayBuffer(68);
30 buffer8 = new Uint8Array(buffer);
31 blocks = new Uint32Array(buffer);
32 }
33
34 /**
35 * @method hex
36 * @memberof md4
37 * @description Output hash as hex string
38 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
39 * @returns {String} Hex string
40 * @example
41 * md4.hex('The quick brown fox jumps over the lazy dog');
42 * // equal to
43 * md4('The quick brown fox jumps over the lazy dog');
44 */
45 /**
46 * @method digest
47 * @memberof md4
48 * @description Output hash as bytes array
49 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
50 * @returns {Array} Bytes array
51 * @example
52 * md4.digest('The quick brown fox jumps over the lazy dog');
53 */
54 /**
55 * @method array
56 * @memberof md4
57 * @description Output hash as bytes array
58 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
59 * @returns {Array} Bytes array
60 * @example
61 * md4.array('The quick brown fox jumps over the lazy dog');
62 */
63 /**
64 * @method buffer
65 * @memberof md4
66 * @description Output hash as ArrayBuffer
67 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
68 * @returns {ArrayBuffer} ArrayBuffer
69 * @example
70 * md4.buffer('The quick brown fox jumps over the lazy dog');
71 */
72 var createOutputMethod = function (outputType) {
73 return function(message) {
74 return new Md4(true).update(message)[outputType]();
75 }
76 };
77
78 /**
79 * @method create
80 * @memberof md4
81 * @description Create Md4 object
82 * @returns {Md4} MD4 object.
83 * @example
84 * var hash = md4.create();
85 */
86 /**
87 * @method update
88 * @memberof md4
89 * @description Create and update Md4 object
90 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
91 * @returns {Md4} MD4 object.
92 * @example
93 * var hash = md4.update('The quick brown fox jumps over the lazy dog');
94 * // equal to
95 * var hash = md4.create();
96 * hash.update('The quick brown fox jumps over the lazy dog');
97 */
98 var createMethod = function () {
99 var method = createOutputMethod('hex');
100 method.create = function () {
101 return new Md4();
102 };
103 method.update = function (message) {
104 return method.create().update(message);
105 };
106 for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
107 var type = OUTPUT_TYPES[i];
108 method[type] = createOutputMethod(type);
109 }
110 return method;
111 };
112
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);
123 }
124 return crypto.createHash('md4').update(Buffer.from(message)).digest('hex');
125 };
126 return nodeMethod;
127 };
128
129 /**
130 * Md4 class
131 * @class Md4
132 * @description This is internal class.
133 * @see {@link md4.create}
134 */
135 function Md4(sharedMemory) {
136 if (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;
143 } else {
144 if (ARRAY_BUFFER) {
145 var buffer = new ArrayBuffer(68);
146 this.buffer8 = new Uint8Array(buffer);
147 this.blocks = new Uint32Array(buffer);
148 } else {
149 this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
150 }
151 }
152 this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = 0;
153 this.finalized = this.hashed = false;
154 this.first = true;
155 }
156
157 /**
158 * @method update
159 * @memberof Md4
160 * @instance
161 * @description Update hash
162 * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
163 * @returns {Md4} MD4 object.
164 * @see {@link md4.update}
165 */
166 Md4.prototype.update = function (message) {
167 if (this.finalized) {
168 return;
169 }
170 var notString = typeof message !== 'string';
171 if (notString && ARRAY_BUFFER && message instanceof ArrayBuffer) {
172 message = new Uint8Array(message);
173 }
174 var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
175 var buffer8 = this.buffer8;
176
177 while (index < length) {
178 if (this.hashed) {
179 this.hashed = false;
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;
185 }
186
187 if (notString) {
188 if (ARRAY_BUFFER) {
189 for (i = this.start; index < length && i < 64; ++index) {
190 buffer8[i++] = message[index];
191 }
192 } else {
193 for (i = this.start; index < length && i < 64; ++index) {
194 blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
195 }
196 }
197 } else {
198 if (ARRAY_BUFFER) {
199 for (i = this.start; index < length && i < 64; ++index) {
200 code = message.charCodeAt(index);
201 if (code < 0x80) {
202 buffer8[i++] = code;
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);
210 } else {
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);
216 }
217 }
218 } else {
219 for (i = this.start; index < length && i < 64; ++index) {
220 code = message.charCodeAt(index);
221 if (code < 0x80) {
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];
230 } else {
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];
236 }
237 }
238 }
239 }
240 this.lastByteIndex = i;
241 this.bytes += i - this.start;
242 if (i >= 64) {
243 this.start = i - 64;
244 this.hash();
245 this.hashed = true;
246 } else {
247 this.start = i;
248 }
249 }
250 return this;
251 };
252
253 Md4.prototype.finalize = function () {
254 if (this.finalized) {
255 return;
256 }
257 this.finalized = true;
258 var blocks = this.blocks, i = this.lastByteIndex;
259 blocks[i >> 2] |= EXTRA[i & 3];
260 if (i >= 56) {
261 if (!this.hashed) {
262 this.hash();
263 }
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;
269 }
270 blocks[14] = this.bytes << 3;
271 this.hash();
272 };
273
274 Md4.prototype.hash = function () {
275 var a, b, c, d, ab, bc, cd, da, blocks = this.blocks;
276
277 if (this.first) {
278 a = blocks[0] - 1;
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);
286 } else {
287 a = this.h0;
288 b = this.h1;
289 c = this.h2;
290 d = this.h3;
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);
299 }
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);
324
325 bc = b & c;
326 a += (bc | (b & d) | (c & d)) + blocks[0] + 1518500249;
327 a = (a << 3) | (a >>> 29);
328 ab = a & b;
329 d += (ab | (a & c) | bc) + blocks[4] + 1518500249;
330 d = (d << 5) | (d >>> 27);
331 da = d & a;
332 c += (da | (d & b) | ab) + blocks[8] + 1518500249;
333 c = (c << 9) | (c >>> 23);
334 cd = c & d;
335 b += (cd | (c & a) | da) + blocks[12] + 1518500249;
336 b = (b << 13) | (b >>> 19);
337 bc = b & c;
338 a += (bc | (b & d) | cd) + blocks[1] + 1518500249;
339 a = (a << 3) | (a >>> 29);
340 ab = a & b;
341 d += (ab | (a & c) | bc) + blocks[5] + 1518500249;
342 d = (d << 5) | (d >>> 27);
343 da = d & a;
344 c += (da | (d & b) | ab) + blocks[9] + 1518500249;
345 c = (c << 9) | (c >>> 23);
346 cd = c & d;
347 b += (cd | (c & a) | da) + blocks[13] + 1518500249;
348 b = (b << 13) | (b >>> 19);
349 bc = b & c;
350 a += (bc | (b & d) | cd) + blocks[2] + 1518500249;
351 a = (a << 3) | (a >>> 29);
352 ab = a & b;
353 d += (ab | (a & c) | bc) + blocks[6] + 1518500249;
354 d = (d << 5) | (d >>> 27);
355 da = d & a;
356 c += (da | (d & b) | ab) + blocks[10] + 1518500249;
357 c = (c << 9) | (c >>> 23);
358 cd = c & d;
359 b += (cd | (c & a) | da) + blocks[14] + 1518500249;
360 b = (b << 13) | (b >>> 19);
361 bc = b & c;
362 a += (bc | (b & d) | cd) + blocks[3] + 1518500249;
363 a = (a << 3) | (a >>> 29);
364 ab = a & b;
365 d += (ab | (a & c) | bc) + blocks[7] + 1518500249;
366 d = (d << 5) | (d >>> 27);
367 da = d & a;
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);
372
373 bc = b ^ c;
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);
378 da = d ^ a;
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);
383 bc = b ^ c;
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);
388 da = d ^ a;
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);
393 bc = b ^ c;
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);
398 da = d ^ a;
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);
403 bc = b ^ c;
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);
408 da = d ^ a;
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);
413
414 if (this.first) {
415 this.h0 = a + 1732584193 << 0;
416 this.h1 = b - 271733879 << 0;
417 this.h2 = c - 1732584194 << 0;
418 this.h3 = d + 271733878 << 0;
419 this.first = false;
420 } else {
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;
425 }
426 };
427
428 /**
429 * @method hex
430 * @memberof Md4
431 * @instance
432 * @description Output hash as hex string
433 * @returns {String} Hex string
434 * @see {@link md4.hex}
435 * @example
436 * hash.hex();
437 */
438 Md4.prototype.hex = function () {
439 this.finalize();
440
441 var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
442
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];
459 };
460
461 /**
462 * @method toString
463 * @memberof Md4
464 * @instance
465 * @description Output hash as hex string
466 * @returns {String} Hex string
467 * @see {@link md4.hex}
468 * @example
469 * hash.toString();
470 */
471 Md4.prototype.toString = Md4.prototype.hex;
472
473 /**
474 * @method digest
475 * @memberof Md4
476 * @instance
477 * @description Output hash as bytes array
478 * @returns {Array} Bytes array
479 * @see {@link md4.digest}
480 * @example
481 * hash.digest();
482 */
483 Md4.prototype.digest = function() {
484 this.finalize();
485
486 var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
487 return [
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
492 ];
493 };
494
495 /**
496 * @method array
497 * @memberof Md4
498 * @instance
499 * @description Output hash as bytes array
500 * @returns {Array} Bytes array
501 * @see {@link md4.array}
502 * @example
503 * hash.array();
504 */
505 Md4.prototype.array = Md4.prototype.digest;
506
507 /**
508 * @method arrayBuffer
509 * @memberof Md4
510 * @instance
511 * @description Output hash as ArrayBuffer
512 * @returns {ArrayBuffer} ArrayBuffer
513 * @see {@link md4.arrayBuffer}
514 * @example
515 * hash.arrayBuffer();
516 */
517 Md4.prototype.arrayBuffer = function() {
518 this.finalize();
519
520 var buffer = new ArrayBuffer(16);
521 var blocks = new Uint32Array(buffer);
522 blocks[0] = this.h0;
523 blocks[1] = this.h1;
524 blocks[2] = this.h2;
525 blocks[3] = this.h3;
526 return buffer;
527 };
528
529 /**
530 * @method buffer
531 * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
532 * @memberof Md4
533 * @instance
534 * @description Output hash as ArrayBuffer
535 * @returns {ArrayBuffer} ArrayBuffer
536 * @see {@link md4.buffer}
537 * @example
538 * hash.buffer();
539 */
540 Md4.prototype.buffer = Md4.prototype.arrayBuffer;
541
542 var exports = createMethod();
543
544 if (COMMON_JS) {
545 module.exports = exports;
546 } else {
547 /**
548 * @method md4␈
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
552 * @example
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
556 *
557 * // It also supports UTF-8 encoding
558 * md4('中文'); // 223088bf7bd45a16436b15360c5fc5a0
559 *
560 * // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
561 * md4([]); // 31d6cfe0d16ae931b73c59d7e0c089c0
562 * md4(new Uint8Array([])); // 31d6cfe0d16ae931b73c59d7e0c089c0
563 */
564 root.md4 = exports;
565 if (AMD) {
566 define(function () {
567 return exports;
568 });
569 }
570 }
571})();