md4.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 [[email protected]]
  7. * @copyright Yi-Cyuan Chen 2015-2027
  8. * @license MIT
  9. */
  10. /*jslint bitwise: true */
  11. (function () {
  12. 'use strict';
  13. var root = typeof window === 'object' ? window : {};
  14. var NODE_JS = !root.JS_MD4_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
  15. if (NODE_JS) {
  16. root = global;
  17. }
  18. var COMMON_JS = !root.JS_MD4_NO_COMMON_JS && typeof module === 'object' && module.exports;
  19. var AMD = typeof define === 'function' && define.amd;
  20. var ARRAY_BUFFER = !root.JS_MD4_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
  21. var HEX_CHARS = '0123456789abcdef'.split('');
  22. var EXTRA = [128, 32768, 8388608, -2147483648];
  23. var SHIFT = [0, 8, 16, 24];
  24. var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer'];
  25. var blocks = [], buffer8;
  26. if (ARRAY_BUFFER) {
  27. var buffer = new ArrayBuffer(68);
  28. buffer8 = new Uint8Array(buffer);
  29. blocks = new Uint32Array(buffer);
  30. }
  31. /**
  32. * @method hex
  33. * @memberof md4
  34. * @description Output hash as hex string
  35. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  36. * @returns {String} Hex string
  37. * @example
  38. * md4.hex('The quick brown fox jumps over the lazy dog');
  39. * // equal to
  40. * md4('The quick brown fox jumps over the lazy dog');
  41. */
  42. /**
  43. * @method digest
  44. * @memberof md4
  45. * @description Output hash as bytes array
  46. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  47. * @returns {Array} Bytes array
  48. * @example
  49. * md4.digest('The quick brown fox jumps over the lazy dog');
  50. */
  51. /**
  52. * @method array
  53. * @memberof md4
  54. * @description Output hash as bytes array
  55. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  56. * @returns {Array} Bytes array
  57. * @example
  58. * md4.array('The quick brown fox jumps over the lazy dog');
  59. */
  60. /**
  61. * @method buffer
  62. * @memberof md4
  63. * @description Output hash as ArrayBuffer
  64. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  65. * @returns {ArrayBuffer} ArrayBuffer
  66. * @example
  67. * md4.buffer('The quick brown fox jumps over the lazy dog');
  68. */
  69. var createOutputMethod = function (outputType) {
  70. return function(message) {
  71. return new Md4(true).update(message)[outputType]();
  72. }
  73. };
  74. /**
  75. * @method create
  76. * @memberof md4
  77. * @description Create Md4 object
  78. * @returns {Md4} MD4 object.
  79. * @example
  80. * var hash = md4.create();
  81. */
  82. /**
  83. * @method update
  84. * @memberof md4
  85. * @description Create and update Md4 object
  86. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  87. * @returns {Md4} MD4 object.
  88. * @example
  89. * var hash = md4.update('The quick brown fox jumps over the lazy dog');
  90. * // equal to
  91. * var hash = md4.create();
  92. * hash.update('The quick brown fox jumps over the lazy dog');
  93. */
  94. var createMethod = function () {
  95. var method = createOutputMethod('hex');
  96. method.create = function () {
  97. return new Md4();
  98. };
  99. method.update = function (message) {
  100. return method.create().update(message);
  101. };
  102. for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
  103. var type = OUTPUT_TYPES[i];
  104. method[type] = createOutputMethod(type);
  105. }
  106. return method;
  107. };
  108. var nodeWrap = function (method) {
  109. var crypto = require('crypto');
  110. var Buffer = require('buffer').Buffer;
  111. var nodeMethod = function (message) {
  112. if (typeof message === 'string') {
  113. return crypto.createHash('md4').update(message, 'utf8').digest('hex');
  114. } else if (ARRAY_BUFFER && message instanceof ArrayBuffer) {
  115. message = new Uint8Array(message);
  116. } else if (message.length === undefined) {
  117. return method(message);
  118. }
  119. return crypto.createHash('md4').update(Buffer.from(message)).digest('hex');
  120. };
  121. return nodeMethod;
  122. };
  123. /**
  124. * Md4 class
  125. * @class Md4
  126. * @description This is internal class.
  127. * @see {@link md4.create}
  128. */
  129. function Md4(sharedMemory) {
  130. if (sharedMemory) {
  131. blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  132. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  133. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  134. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  135. this.blocks = blocks;
  136. this.buffer8 = buffer8;
  137. } else {
  138. if (ARRAY_BUFFER) {
  139. var buffer = new ArrayBuffer(68);
  140. this.buffer8 = new Uint8Array(buffer);
  141. this.blocks = new Uint32Array(buffer);
  142. } else {
  143. this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  144. }
  145. }
  146. this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = 0;
  147. this.finalized = this.hashed = false;
  148. this.first = true;
  149. }
  150. /**
  151. * @method update
  152. * @memberof Md4
  153. * @instance
  154. * @description Update hash
  155. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  156. * @returns {Md4} MD4 object.
  157. * @see {@link md4.update}
  158. */
  159. Md4.prototype.update = function (message) {
  160. if (this.finalized) {
  161. return;
  162. }
  163. var notString = typeof message !== 'string';
  164. if (notString && ARRAY_BUFFER && message instanceof ArrayBuffer) {
  165. message = new Uint8Array(message);
  166. }
  167. var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
  168. var buffer8 = this.buffer8;
  169. while (index < length) {
  170. if (this.hashed) {
  171. this.hashed = false;
  172. blocks[0] = blocks[16];
  173. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  174. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  175. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  176. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  177. }
  178. if (notString) {
  179. if (ARRAY_BUFFER) {
  180. for (i = this.start; index < length && i < 64; ++index) {
  181. buffer8[i++] = message[index];
  182. }
  183. } else {
  184. for (i = this.start; index < length && i < 64; ++index) {
  185. blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
  186. }
  187. }
  188. } else {
  189. if (ARRAY_BUFFER) {
  190. for (i = this.start; index < length && i < 64; ++index) {
  191. code = message.charCodeAt(index);
  192. if (code < 0x80) {
  193. buffer8[i++] = code;
  194. } else if (code < 0x800) {
  195. buffer8[i++] = 0xc0 | (code >> 6);
  196. buffer8[i++] = 0x80 | (code & 0x3f);
  197. } else if (code < 0xd800 || code >= 0xe000) {
  198. buffer8[i++] = 0xe0 | (code >> 12);
  199. buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
  200. buffer8[i++] = 0x80 | (code & 0x3f);
  201. } else {
  202. code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
  203. buffer8[i++] = 0xf0 | (code >> 18);
  204. buffer8[i++] = 0x80 | ((code >> 12) & 0x3f);
  205. buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
  206. buffer8[i++] = 0x80 | (code & 0x3f);
  207. }
  208. }
  209. } else {
  210. for (i = this.start; index < length && i < 64; ++index) {
  211. code = message.charCodeAt(index);
  212. if (code < 0x80) {
  213. blocks[i >> 2] |= code << SHIFT[i++ & 3];
  214. } else if (code < 0x800) {
  215. blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
  216. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  217. } else if (code < 0xd800 || code >= 0xe000) {
  218. blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
  219. blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
  220. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  221. } else {
  222. code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
  223. blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
  224. blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
  225. blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
  226. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  227. }
  228. }
  229. }
  230. }
  231. this.lastByteIndex = i;
  232. this.bytes += i - this.start;
  233. if (i >= 64) {
  234. this.start = i - 64;
  235. this.hash();
  236. this.hashed = true;
  237. } else {
  238. this.start = i;
  239. }
  240. }
  241. return this;
  242. };
  243. Md4.prototype.finalize = function () {
  244. if (this.finalized) {
  245. return;
  246. }
  247. this.finalized = true;
  248. var blocks = this.blocks, i = this.lastByteIndex;
  249. blocks[i >> 2] |= EXTRA[i & 3];
  250. if (i >= 56) {
  251. if (!this.hashed) {
  252. this.hash();
  253. }
  254. blocks[0] = blocks[16];
  255. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  256. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  257. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  258. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  259. }
  260. blocks[14] = this.bytes << 3;
  261. this.hash();
  262. };
  263. Md4.prototype.hash = function () {
  264. var a, b, c, d, ab, bc, cd, da, blocks = this.blocks;
  265. if (this.first) {
  266. a = blocks[0] - 1;
  267. a = (a << 3) | (a >>> 29);
  268. d = ((a & 0xefcdab89) | (~a & 0x98badcfe)) + blocks[1] + 271733878;
  269. d = (d << 7) | (d >>> 25);
  270. c = ((d & a) | (~d & 0xefcdab89)) + blocks[2] - 1732584194;
  271. c = (c << 11) | (c >>> 21);
  272. b = ((c & d) | (~c & a)) + blocks[3] - 271733879;
  273. b = (b << 19) | (b >>> 13);
  274. } else {
  275. a = this.h0;
  276. b = this.h1;
  277. c = this.h2;
  278. d = this.h3;
  279. a += ((b & c) | (~b & d)) + blocks[0];
  280. a = (a << 3) | (a >>> 29);
  281. d += ((a & b) | (~a & c)) + blocks[1];
  282. d = (d << 7) | (d >>> 25);
  283. c += ((d & a) | (~d & b)) + blocks[2];
  284. c = (c << 11) | (c >>> 21);
  285. b += ((c & d) | (~c & a)) + blocks[3];
  286. b = (b << 19) | (b >>> 13);
  287. }
  288. a += ((b & c) | (~b & d)) + blocks[4];
  289. a = (a << 3) | (a >>> 29);
  290. d += ((a & b) | (~a & c)) + blocks[5];
  291. d = (d << 7) | (d >>> 25);
  292. c += ((d & a) | (~d & b)) + blocks[6];
  293. c = (c << 11) | (c >>> 21);
  294. b += ((c & d) | (~c & a)) + blocks[7];
  295. b = (b << 19) | (b >>> 13);
  296. a += ((b & c) | (~b & d)) + blocks[8];
  297. a = (a << 3) | (a >>> 29);
  298. d += ((a & b) | (~a & c)) + blocks[9];
  299. d = (d << 7) | (d >>> 25);
  300. c += ((d & a) | (~d & b)) + blocks[10];
  301. c = (c << 11) | (c >>> 21);
  302. b += ((c & d) | (~c & a)) + blocks[11];
  303. b = (b << 19) | (b >>> 13);
  304. a += ((b & c) | (~b & d)) + blocks[12];
  305. a = (a << 3) | (a >>> 29);
  306. d += ((a & b) | (~a & c)) + blocks[13];
  307. d = (d << 7) | (d >>> 25);
  308. c += ((d & a) | (~d & b)) + blocks[14];
  309. c = (c << 11) | (c >>> 21);
  310. b += ((c & d) | (~c & a)) + blocks[15];
  311. b = (b << 19) | (b >>> 13);
  312. bc = b & c;
  313. a += (bc | (b & d) | (c & d)) + blocks[0] + 1518500249;
  314. a = (a << 3) | (a >>> 29);
  315. ab = a & b;
  316. d += (ab | (a & c) | bc) + blocks[4] + 1518500249;
  317. d = (d << 5) | (d >>> 27);
  318. da = d & a;
  319. c += (da | (d & b) | ab) + blocks[8] + 1518500249;
  320. c = (c << 9) | (c >>> 23);
  321. cd = c & d;
  322. b += (cd | (c & a) | da) + blocks[12] + 1518500249;
  323. b = (b << 13) | (b >>> 19);
  324. bc = b & c;
  325. a += (bc | (b & d) | cd) + blocks[1] + 1518500249;
  326. a = (a << 3) | (a >>> 29);
  327. ab = a & b;
  328. d += (ab | (a & c) | bc) + blocks[5] + 1518500249;
  329. d = (d << 5) | (d >>> 27);
  330. da = d & a;
  331. c += (da | (d & b) | ab) + blocks[9] + 1518500249;
  332. c = (c << 9) | (c >>> 23);
  333. cd = c & d;
  334. b += (cd | (c & a) | da) + blocks[13] + 1518500249;
  335. b = (b << 13) | (b >>> 19);
  336. bc = b & c;
  337. a += (bc | (b & d) | cd) + blocks[2] + 1518500249;
  338. a = (a << 3) | (a >>> 29);
  339. ab = a & b;
  340. d += (ab | (a & c) | bc) + blocks[6] + 1518500249;
  341. d = (d << 5) | (d >>> 27);
  342. da = d & a;
  343. c += (da | (d & b) | ab) + blocks[10] + 1518500249;
  344. c = (c << 9) | (c >>> 23);
  345. cd = c & d;
  346. b += (cd | (c & a) | da) + blocks[14] + 1518500249;
  347. b = (b << 13) | (b >>> 19);
  348. bc = b & c;
  349. a += (bc | (b & d) | cd) + blocks[3] + 1518500249;
  350. a = (a << 3) | (a >>> 29);
  351. ab = a & b;
  352. d += (ab | (a & c) | bc) + blocks[7] + 1518500249;
  353. d = (d << 5) | (d >>> 27);
  354. da = d & a;
  355. c += (da | (d & b) | ab) + blocks[11] + 1518500249;
  356. c = (c << 9) | (c >>> 23);
  357. b += ((c & d) | (c & a) | da) + blocks[15] + 1518500249;
  358. b = (b << 13) | (b >>> 19);
  359. bc = b ^ c;
  360. a += (bc ^ d) + blocks[0] + 1859775393;
  361. a = (a << 3) | (a >>> 29);
  362. d += (bc ^ a) + blocks[8] + 1859775393;
  363. d = (d << 9) | (d >>> 23);
  364. da = d ^ a;
  365. c += (da ^ b) + blocks[4] + 1859775393;
  366. c = (c << 11) | (c >>> 21);
  367. b += (da ^ c) + blocks[12] + 1859775393;
  368. b = (b << 15) | (b >>> 17);
  369. bc = b ^ c;
  370. a += (bc ^ d) + blocks[2] + 1859775393;
  371. a = (a << 3) | (a >>> 29);
  372. d += (bc ^ a) + blocks[10] + 1859775393;
  373. d = (d << 9) | (d >>> 23);
  374. da = d ^ a;
  375. c += (da ^ b) + blocks[6] + 1859775393;
  376. c = (c << 11) | (c >>> 21);
  377. b += (da ^ c) + blocks[14] + 1859775393;
  378. b = (b << 15) | (b >>> 17);
  379. bc = b ^ c;
  380. a += (bc ^ d) + blocks[1] + 1859775393;
  381. a = (a << 3) | (a >>> 29);
  382. d += (bc ^ a) + blocks[9] + 1859775393;
  383. d = (d << 9) | (d >>> 23);
  384. da = d ^ a;
  385. c += (da ^ b) + blocks[5] + 1859775393;
  386. c = (c << 11) | (c >>> 21);
  387. b += (da ^ c) + blocks[13] + 1859775393;
  388. b = (b << 15) | (b >>> 17);
  389. bc = b ^ c;
  390. a += (bc ^ d) + blocks[3] + 1859775393;
  391. a = (a << 3) | (a >>> 29);
  392. d += (bc ^ a) + blocks[11] + 1859775393;
  393. d = (d << 9) | (d >>> 23);
  394. da = d ^ a;
  395. c += (da ^ b) + blocks[7] + 1859775393;
  396. c = (c << 11) | (c >>> 21);
  397. b += (da ^ c) + blocks[15] + 1859775393;
  398. b = (b << 15) | (b >>> 17);
  399. if (this.first) {
  400. this.h0 = a + 1732584193 << 0;
  401. this.h1 = b - 271733879 << 0;
  402. this.h2 = c - 1732584194 << 0;
  403. this.h3 = d + 271733878 << 0;
  404. this.first = false;
  405. } else {
  406. this.h0 = this.h0 + a << 0;
  407. this.h1 = this.h1 + b << 0;
  408. this.h2 = this.h2 + c << 0;
  409. this.h3 = this.h3 + d << 0;
  410. }
  411. };
  412. /**
  413. * @method hex
  414. * @memberof Md4
  415. * @instance
  416. * @description Output hash as hex string
  417. * @returns {String} Hex string
  418. * @see {@link md4.hex}
  419. * @example
  420. * hash.hex();
  421. */
  422. Md4.prototype.hex = function () {
  423. this.finalize();
  424. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
  425. return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
  426. HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
  427. HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
  428. HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
  429. HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
  430. HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
  431. HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
  432. HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
  433. HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
  434. HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
  435. HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
  436. HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
  437. HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
  438. HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
  439. HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
  440. HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F];
  441. };
  442. /**
  443. * @method toString
  444. * @memberof Md4
  445. * @instance
  446. * @description Output hash as hex string
  447. * @returns {String} Hex string
  448. * @see {@link md4.hex}
  449. * @example
  450. * hash.toString();
  451. */
  452. Md4.prototype.toString = Md4.prototype.hex;
  453. /**
  454. * @method digest
  455. * @memberof Md4
  456. * @instance
  457. * @description Output hash as bytes array
  458. * @returns {Array} Bytes array
  459. * @see {@link md4.digest}
  460. * @example
  461. * hash.digest();
  462. */
  463. Md4.prototype.digest = function() {
  464. this.finalize();
  465. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
  466. return [
  467. h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF,
  468. h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF,
  469. h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF,
  470. h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF
  471. ];
  472. };
  473. /**
  474. * @method array
  475. * @memberof Md4
  476. * @instance
  477. * @description Output hash as bytes array
  478. * @returns {Array} Bytes array
  479. * @see {@link md4.array}
  480. * @example
  481. * hash.array();
  482. */
  483. Md4.prototype.array = Md4.prototype.digest;
  484. /**
  485. * @method arrayBuffer
  486. * @memberof Md4
  487. * @instance
  488. * @description Output hash as ArrayBuffer
  489. * @returns {ArrayBuffer} ArrayBuffer
  490. * @see {@link md4.arrayBuffer}
  491. * @example
  492. * hash.arrayBuffer();
  493. */
  494. Md4.prototype.arrayBuffer = function() {
  495. this.finalize();
  496. var buffer = new ArrayBuffer(16);
  497. var blocks = new Uint32Array(buffer);
  498. blocks[0] = this.h0;
  499. blocks[1] = this.h1;
  500. blocks[2] = this.h2;
  501. blocks[3] = this.h3;
  502. return buffer;
  503. };
  504. /**
  505. * @method buffer
  506. * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
  507. * @memberof Md4
  508. * @instance
  509. * @description Output hash as ArrayBuffer
  510. * @returns {ArrayBuffer} ArrayBuffer
  511. * @see {@link md4.buffer}
  512. * @example
  513. * hash.buffer();
  514. */
  515. Md4.prototype.buffer = Md4.prototype.arrayBuffer;
  516. var exports = createMethod();
  517. if (COMMON_JS) {
  518. module.exports = exports;
  519. } else {
  520. /**
  521. * @method md4
  522. * @description MD4 hash function, export to global in browsers.
  523. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  524. * @returns {String} md4 hashes
  525. * @example
  526. * md4(''); // 31d6cfe0d16ae931b73c59d7e0c089c0
  527. * md4('The quick brown fox jumps over the lazy dog'); // 1bee69a46ba811185c194762abaeae90
  528. * md4('The quick brown fox jumps over the lazy dog.'); // 2812c6c7136898c51f6f6739ad08750e
  529. *
  530. * // It also supports UTF-8 encoding
  531. * md4('中文'); // 223088bf7bd45a16436b15360c5fc5a0
  532. *
  533. * // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
  534. * md4([]); // 31d6cfe0d16ae931b73c59d7e0c089c0
  535. * md4(new Uint8Array([])); // 31d6cfe0d16ae931b73c59d7e0c089c0
  536. */
  537. root.md4 = exports;
  538. if (AMD) {
  539. define(function () {
  540. return exports;
  541. });
  542. }
  543. }
  544. })();