md5.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /* https://github.com/emn178/js-md5 */
  2. (function () {
  3. 'use strict';
  4. var ERROR = 'input is invalid type';
  5. var WINDOW = typeof window === 'object';
  6. var root = WINDOW ? window : {};
  7. if (root.JS_MD5_NO_WINDOW) {
  8. WINDOW = false;
  9. }
  10. var WEB_WORKER = !WINDOW && typeof self === 'object';
  11. var NODE_JS = !root.JS_MD5_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
  12. if (NODE_JS) {
  13. root = global;
  14. } else if (WEB_WORKER) {
  15. root = self;
  16. }
  17. var COMMON_JS = !root.JS_MD5_NO_COMMON_JS && typeof module === 'object' && module.exports;
  18. var AMD = typeof define === 'function' && define.amd;
  19. var ARRAY_BUFFER = !root.JS_MD5_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
  20. var HEX_CHARS = '0123456789abcdef'.split('');
  21. var EXTRA = [128, 32768, 8388608, -2147483648];
  22. var SHIFT = [0, 8, 16, 24];
  23. var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer', 'base64'];
  24. var BASE64_ENCODE_CHAR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
  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. if (root.JS_MD5_NO_NODE_JS || !Array.isArray) {
  32. Array.isArray = function (obj) {
  33. return Object.prototype.toString.call(obj) === '[object Array]';
  34. };
  35. }
  36. if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
  37. ArrayBuffer.isView = function (obj) {
  38. return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
  39. };
  40. }
  41. /**
  42. * @method hex
  43. * @memberof md5
  44. * @description Output hash as hex string
  45. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  46. * @returns {String} Hex string
  47. * @example
  48. * md5.hex('The quick brown fox jumps over the lazy dog');
  49. * // equal to
  50. * md5('The quick brown fox jumps over the lazy dog');
  51. */
  52. /**
  53. * @method digest
  54. * @memberof md5
  55. * @description Output hash as bytes array
  56. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  57. * @returns {Array} Bytes array
  58. * @example
  59. * md5.digest('The quick brown fox jumps over the lazy dog');
  60. */
  61. /**
  62. * @method array
  63. * @memberof md5
  64. * @description Output hash as bytes array
  65. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  66. * @returns {Array} Bytes array
  67. * @example
  68. * md5.array('The quick brown fox jumps over the lazy dog');
  69. */
  70. /**
  71. * @method arrayBuffer
  72. * @memberof md5
  73. * @description Output hash as ArrayBuffer
  74. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  75. * @returns {ArrayBuffer} ArrayBuffer
  76. * @example
  77. * md5.arrayBuffer('The quick brown fox jumps over the lazy dog');
  78. */
  79. /**
  80. * @method buffer
  81. * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
  82. * @memberof md5
  83. * @description Output hash as ArrayBuffer
  84. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  85. * @returns {ArrayBuffer} ArrayBuffer
  86. * @example
  87. * md5.buffer('The quick brown fox jumps over the lazy dog');
  88. */
  89. /**
  90. * @method base64
  91. * @memberof md5
  92. * @description Output hash as base64 string
  93. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  94. * @returns {String} base64 string
  95. * @example
  96. * md5.base64('The quick brown fox jumps over the lazy dog');
  97. */
  98. var createOutputMethod = function (outputType) {
  99. return function (message, isBinStr) {
  100. return new Md5(true).update(message, isBinStr)[outputType]();
  101. };
  102. };
  103. /**
  104. * @method create
  105. * @memberof md5
  106. * @description Create Md5 object
  107. * @returns {Md5} Md5 object.
  108. * @example
  109. * var hash = md5.create();
  110. */
  111. /**
  112. * @method update
  113. * @memberof md5
  114. * @description Create and update Md5 object
  115. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  116. * @returns {Md5} Md5 object.
  117. * @example
  118. * var hash = md5.update('The quick brown fox jumps over the lazy dog');
  119. * // equal to
  120. * var hash = md5.create();
  121. * hash.update('The quick brown fox jumps over the lazy dog');
  122. */
  123. var createMethod = function () {
  124. var method = createOutputMethod('hex');
  125. if (NODE_JS) {
  126. method = nodeWrap(method);
  127. }
  128. method.getCtx = method.create = function () {
  129. return new Md5();
  130. };
  131. method.update = function (message) {
  132. return method.create().update(message);
  133. };
  134. for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
  135. var type = OUTPUT_TYPES[i];
  136. method[type] = createOutputMethod(type);
  137. }
  138. return method;
  139. };
  140. var nodeWrap = function (method) {
  141. var crypto = eval("require('crypto')");
  142. var Buffer = eval("require('buffer').Buffer");
  143. var nodeMethod = function (message) {
  144. if (typeof message === 'string') {
  145. return crypto.createHash('md5').update(message, 'utf8').digest('hex');
  146. } else {
  147. if (message === null || message === undefined) {
  148. throw ERROR;
  149. } else if (message.constructor === ArrayBuffer) {
  150. message = new Uint8Array(message);
  151. }
  152. }
  153. if (Array.isArray(message) || ArrayBuffer.isView(message) ||
  154. message.constructor === Buffer) {
  155. return crypto.createHash('md5').update(new Buffer(message)).digest('hex');
  156. } else {
  157. return method(message);
  158. }
  159. };
  160. return nodeMethod;
  161. };
  162. /**
  163. * Md5 class
  164. * @class Md5
  165. * @description This is internal class.
  166. * @see {@link md5.create}
  167. */
  168. function Md5(sharedMemory) {
  169. if (sharedMemory) {
  170. blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  171. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  172. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  173. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  174. this.blocks = blocks;
  175. this.buffer8 = buffer8;
  176. } else {
  177. if (ARRAY_BUFFER) {
  178. var buffer = new ArrayBuffer(68);
  179. this.buffer8 = new Uint8Array(buffer);
  180. this.blocks = new Uint32Array(buffer);
  181. } else {
  182. this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  183. }
  184. }
  185. this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = this.hBytes = 0;
  186. this.finalized = this.hashed = false;
  187. this.first = true;
  188. }
  189. /**
  190. * @method update
  191. * @memberof Md5
  192. * @instance
  193. * @description Update hash
  194. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  195. * @returns {Md5} Md5 object.
  196. * @see {@link md5.update}
  197. */
  198. Md5.prototype.update = function (message, isBinStr) {
  199. if (this.finalized) {
  200. return;
  201. }
  202. var code, index = 0, i, length = message.length, blocks = this.blocks;
  203. var buffer8 = this.buffer8;
  204. while (index < length) {
  205. if (this.hashed) {
  206. this.hashed = false;
  207. blocks[0] = blocks[16];
  208. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  209. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  210. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  211. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  212. }
  213. if (ARRAY_BUFFER) {
  214. for (i = this.start; index < length && i < 64; ++index) {
  215. code = message.charCodeAt(index);
  216. if (isBinStr || code < 0x80) {
  217. buffer8[i++] = code;
  218. } else if (code < 0x800) {
  219. buffer8[i++] = 0xc0 | (code >> 6);
  220. buffer8[i++] = 0x80 | (code & 0x3f);
  221. } else if (code < 0xd800 || code >= 0xe000) {
  222. buffer8[i++] = 0xe0 | (code >> 12);
  223. buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
  224. buffer8[i++] = 0x80 | (code & 0x3f);
  225. } else {
  226. code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
  227. buffer8[i++] = 0xf0 | (code >> 18);
  228. buffer8[i++] = 0x80 | ((code >> 12) & 0x3f);
  229. buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
  230. buffer8[i++] = 0x80 | (code & 0x3f);
  231. }
  232. }
  233. } else {
  234. for (i = this.start; index < length && i < 64; ++index) {
  235. code = message.charCodeAt(index);
  236. if (isBinStr || code < 0x80) {
  237. blocks[i >> 2] |= code << SHIFT[i++ & 3];
  238. } else if (code < 0x800) {
  239. blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
  240. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  241. } else if (code < 0xd800 || code >= 0xe000) {
  242. blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
  243. blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
  244. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  245. } else {
  246. code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
  247. blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
  248. blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
  249. blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
  250. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  251. }
  252. }
  253. }
  254. this.lastByteIndex = i;
  255. this.bytes += i - this.start;
  256. if (i >= 64) {
  257. this.start = i - 64;
  258. this.hash();
  259. this.hashed = true;
  260. } else {
  261. this.start = i;
  262. }
  263. }
  264. if (this.bytes > 4294967295) {
  265. this.hBytes += this.bytes / 4294967296 << 0;
  266. this.bytes = this.bytes % 4294967296;
  267. }
  268. return this;
  269. };
  270. Md5.prototype.finalize = function () {
  271. if (this.finalized) {
  272. return;
  273. }
  274. this.finalized = true;
  275. var blocks = this.blocks, i = this.lastByteIndex;
  276. blocks[i >> 2] |= EXTRA[i & 3];
  277. if (i >= 56) {
  278. if (!this.hashed) {
  279. this.hash();
  280. }
  281. blocks[0] = blocks[16];
  282. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  283. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  284. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  285. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  286. }
  287. blocks[14] = this.bytes << 3;
  288. blocks[15] = this.hBytes << 3 | this.bytes >>> 29;
  289. this.hash();
  290. };
  291. Md5.prototype.hash = function () {
  292. var a, b, c, d, bc, da, blocks = this.blocks;
  293. if (this.first) {
  294. a = blocks[0] - 680876937;
  295. a = (a << 7 | a >>> 25) - 271733879 << 0;
  296. d = (-1732584194 ^ a & 2004318071) + blocks[1] - 117830708;
  297. d = (d << 12 | d >>> 20) + a << 0;
  298. c = (-271733879 ^ (d & (a ^ -271733879))) + blocks[2] - 1126478375;
  299. c = (c << 17 | c >>> 15) + d << 0;
  300. b = (a ^ (c & (d ^ a))) + blocks[3] - 1316259209;
  301. b = (b << 22 | b >>> 10) + c << 0;
  302. } else {
  303. a = this.h0;
  304. b = this.h1;
  305. c = this.h2;
  306. d = this.h3;
  307. a += (d ^ (b & (c ^ d))) + blocks[0] - 680876936;
  308. a = (a << 7 | a >>> 25) + b << 0;
  309. d += (c ^ (a & (b ^ c))) + blocks[1] - 389564586;
  310. d = (d << 12 | d >>> 20) + a << 0;
  311. c += (b ^ (d & (a ^ b))) + blocks[2] + 606105819;
  312. c = (c << 17 | c >>> 15) + d << 0;
  313. b += (a ^ (c & (d ^ a))) + blocks[3] - 1044525330;
  314. b = (b << 22 | b >>> 10) + c << 0;
  315. }
  316. a += (d ^ (b & (c ^ d))) + blocks[4] - 176418897;
  317. a = (a << 7 | a >>> 25) + b << 0;
  318. d += (c ^ (a & (b ^ c))) + blocks[5] + 1200080426;
  319. d = (d << 12 | d >>> 20) + a << 0;
  320. c += (b ^ (d & (a ^ b))) + blocks[6] - 1473231341;
  321. c = (c << 17 | c >>> 15) + d << 0;
  322. b += (a ^ (c & (d ^ a))) + blocks[7] - 45705983;
  323. b = (b << 22 | b >>> 10) + c << 0;
  324. a += (d ^ (b & (c ^ d))) + blocks[8] + 1770035416;
  325. a = (a << 7 | a >>> 25) + b << 0;
  326. d += (c ^ (a & (b ^ c))) + blocks[9] - 1958414417;
  327. d = (d << 12 | d >>> 20) + a << 0;
  328. c += (b ^ (d & (a ^ b))) + blocks[10] - 42063;
  329. c = (c << 17 | c >>> 15) + d << 0;
  330. b += (a ^ (c & (d ^ a))) + blocks[11] - 1990404162;
  331. b = (b << 22 | b >>> 10) + c << 0;
  332. a += (d ^ (b & (c ^ d))) + blocks[12] + 1804603682;
  333. a = (a << 7 | a >>> 25) + b << 0;
  334. d += (c ^ (a & (b ^ c))) + blocks[13] - 40341101;
  335. d = (d << 12 | d >>> 20) + a << 0;
  336. c += (b ^ (d & (a ^ b))) + blocks[14] - 1502002290;
  337. c = (c << 17 | c >>> 15) + d << 0;
  338. b += (a ^ (c & (d ^ a))) + blocks[15] + 1236535329;
  339. b = (b << 22 | b >>> 10) + c << 0;
  340. a += (c ^ (d & (b ^ c))) + blocks[1] - 165796510;
  341. a = (a << 5 | a >>> 27) + b << 0;
  342. d += (b ^ (c & (a ^ b))) + blocks[6] - 1069501632;
  343. d = (d << 9 | d >>> 23) + a << 0;
  344. c += (a ^ (b & (d ^ a))) + blocks[11] + 643717713;
  345. c = (c << 14 | c >>> 18) + d << 0;
  346. b += (d ^ (a & (c ^ d))) + blocks[0] - 373897302;
  347. b = (b << 20 | b >>> 12) + c << 0;
  348. a += (c ^ (d & (b ^ c))) + blocks[5] - 701558691;
  349. a = (a << 5 | a >>> 27) + b << 0;
  350. d += (b ^ (c & (a ^ b))) + blocks[10] + 38016083;
  351. d = (d << 9 | d >>> 23) + a << 0;
  352. c += (a ^ (b & (d ^ a))) + blocks[15] - 660478335;
  353. c = (c << 14 | c >>> 18) + d << 0;
  354. b += (d ^ (a & (c ^ d))) + blocks[4] - 405537848;
  355. b = (b << 20 | b >>> 12) + c << 0;
  356. a += (c ^ (d & (b ^ c))) + blocks[9] + 568446438;
  357. a = (a << 5 | a >>> 27) + b << 0;
  358. d += (b ^ (c & (a ^ b))) + blocks[14] - 1019803690;
  359. d = (d << 9 | d >>> 23) + a << 0;
  360. c += (a ^ (b & (d ^ a))) + blocks[3] - 187363961;
  361. c = (c << 14 | c >>> 18) + d << 0;
  362. b += (d ^ (a & (c ^ d))) + blocks[8] + 1163531501;
  363. b = (b << 20 | b >>> 12) + c << 0;
  364. a += (c ^ (d & (b ^ c))) + blocks[13] - 1444681467;
  365. a = (a << 5 | a >>> 27) + b << 0;
  366. d += (b ^ (c & (a ^ b))) + blocks[2] - 51403784;
  367. d = (d << 9 | d >>> 23) + a << 0;
  368. c += (a ^ (b & (d ^ a))) + blocks[7] + 1735328473;
  369. c = (c << 14 | c >>> 18) + d << 0;
  370. b += (d ^ (a & (c ^ d))) + blocks[12] - 1926607734;
  371. b = (b << 20 | b >>> 12) + c << 0;
  372. bc = b ^ c;
  373. a += (bc ^ d) + blocks[5] - 378558;
  374. a = (a << 4 | a >>> 28) + b << 0;
  375. d += (bc ^ a) + blocks[8] - 2022574463;
  376. d = (d << 11 | d >>> 21) + a << 0;
  377. da = d ^ a;
  378. c += (da ^ b) + blocks[11] + 1839030562;
  379. c = (c << 16 | c >>> 16) + d << 0;
  380. b += (da ^ c) + blocks[14] - 35309556;
  381. b = (b << 23 | b >>> 9) + c << 0;
  382. bc = b ^ c;
  383. a += (bc ^ d) + blocks[1] - 1530992060;
  384. a = (a << 4 | a >>> 28) + b << 0;
  385. d += (bc ^ a) + blocks[4] + 1272893353;
  386. d = (d << 11 | d >>> 21) + a << 0;
  387. da = d ^ a;
  388. c += (da ^ b) + blocks[7] - 155497632;
  389. c = (c << 16 | c >>> 16) + d << 0;
  390. b += (da ^ c) + blocks[10] - 1094730640;
  391. b = (b << 23 | b >>> 9) + c << 0;
  392. bc = b ^ c;
  393. a += (bc ^ d) + blocks[13] + 681279174;
  394. a = (a << 4 | a >>> 28) + b << 0;
  395. d += (bc ^ a) + blocks[0] - 358537222;
  396. d = (d << 11 | d >>> 21) + a << 0;
  397. da = d ^ a;
  398. c += (da ^ b) + blocks[3] - 722521979;
  399. c = (c << 16 | c >>> 16) + d << 0;
  400. b += (da ^ c) + blocks[6] + 76029189;
  401. b = (b << 23 | b >>> 9) + c << 0;
  402. bc = b ^ c;
  403. a += (bc ^ d) + blocks[9] - 640364487;
  404. a = (a << 4 | a >>> 28) + b << 0;
  405. d += (bc ^ a) + blocks[12] - 421815835;
  406. d = (d << 11 | d >>> 21) + a << 0;
  407. da = d ^ a;
  408. c += (da ^ b) + blocks[15] + 530742520;
  409. c = (c << 16 | c >>> 16) + d << 0;
  410. b += (da ^ c) + blocks[2] - 995338651;
  411. b = (b << 23 | b >>> 9) + c << 0;
  412. a += (c ^ (b | ~d)) + blocks[0] - 198630844;
  413. a = (a << 6 | a >>> 26) + b << 0;
  414. d += (b ^ (a | ~c)) + blocks[7] + 1126891415;
  415. d = (d << 10 | d >>> 22) + a << 0;
  416. c += (a ^ (d | ~b)) + blocks[14] - 1416354905;
  417. c = (c << 15 | c >>> 17) + d << 0;
  418. b += (d ^ (c | ~a)) + blocks[5] - 57434055;
  419. b = (b << 21 | b >>> 11) + c << 0;
  420. a += (c ^ (b | ~d)) + blocks[12] + 1700485571;
  421. a = (a << 6 | a >>> 26) + b << 0;
  422. d += (b ^ (a | ~c)) + blocks[3] - 1894986606;
  423. d = (d << 10 | d >>> 22) + a << 0;
  424. c += (a ^ (d | ~b)) + blocks[10] - 1051523;
  425. c = (c << 15 | c >>> 17) + d << 0;
  426. b += (d ^ (c | ~a)) + blocks[1] - 2054922799;
  427. b = (b << 21 | b >>> 11) + c << 0;
  428. a += (c ^ (b | ~d)) + blocks[8] + 1873313359;
  429. a = (a << 6 | a >>> 26) + b << 0;
  430. d += (b ^ (a | ~c)) + blocks[15] - 30611744;
  431. d = (d << 10 | d >>> 22) + a << 0;
  432. c += (a ^ (d | ~b)) + blocks[6] - 1560198380;
  433. c = (c << 15 | c >>> 17) + d << 0;
  434. b += (d ^ (c | ~a)) + blocks[13] + 1309151649;
  435. b = (b << 21 | b >>> 11) + c << 0;
  436. a += (c ^ (b | ~d)) + blocks[4] - 145523070;
  437. a = (a << 6 | a >>> 26) + b << 0;
  438. d += (b ^ (a | ~c)) + blocks[11] - 1120210379;
  439. d = (d << 10 | d >>> 22) + a << 0;
  440. c += (a ^ (d | ~b)) + blocks[2] + 718787259;
  441. c = (c << 15 | c >>> 17) + d << 0;
  442. b += (d ^ (c | ~a)) + blocks[9] - 343485551;
  443. b = (b << 21 | b >>> 11) + c << 0;
  444. if (this.first) {
  445. this.h0 = a + 1732584193 << 0;
  446. this.h1 = b - 271733879 << 0;
  447. this.h2 = c - 1732584194 << 0;
  448. this.h3 = d + 271733878 << 0;
  449. this.first = false;
  450. } else {
  451. this.h0 = this.h0 + a << 0;
  452. this.h1 = this.h1 + b << 0;
  453. this.h2 = this.h2 + c << 0;
  454. this.h3 = this.h3 + d << 0;
  455. }
  456. };
  457. /**
  458. * @method hex
  459. * @memberof Md5
  460. * @instance
  461. * @description Output hash as hex string
  462. * @returns {String} Hex string
  463. * @see {@link md5.hex}
  464. * @example
  465. * hash.hex();
  466. */
  467. Md5.prototype.hex = function () {
  468. this.finalize();
  469. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
  470. return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
  471. HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
  472. HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
  473. HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
  474. HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
  475. HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
  476. HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
  477. HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
  478. HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
  479. HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
  480. HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
  481. HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
  482. HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
  483. HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
  484. HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
  485. HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F];
  486. };
  487. /**
  488. * @method toString
  489. * @memberof Md5
  490. * @instance
  491. * @description Output hash as hex string
  492. * @returns {String} Hex string
  493. * @see {@link md5.hex}
  494. * @example
  495. * hash.toString();
  496. */
  497. Md5.prototype.toString = Md5.prototype.hex;
  498. /**
  499. * @method digest
  500. * @memberof Md5
  501. * @instance
  502. * @description Output hash as bytes array
  503. * @returns {Array} Bytes array
  504. * @see {@link md5.digest}
  505. * @example
  506. * hash.digest();
  507. */
  508. Md5.prototype.digest = function (format) {
  509. if (format === 'hex') return this.hex();
  510. this.finalize();
  511. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
  512. var res = [
  513. h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF,
  514. h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF,
  515. h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF,
  516. h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF
  517. ];
  518. return res;
  519. };
  520. /**
  521. * @method array
  522. * @memberof Md5
  523. * @instance
  524. * @description Output hash as bytes array
  525. * @returns {Array} Bytes array
  526. * @see {@link md5.array}
  527. * @example
  528. * hash.array();
  529. */
  530. Md5.prototype.array = Md5.prototype.digest;
  531. /**
  532. * @method arrayBuffer
  533. * @memberof Md5
  534. * @instance
  535. * @description Output hash as ArrayBuffer
  536. * @returns {ArrayBuffer} ArrayBuffer
  537. * @see {@link md5.arrayBuffer}
  538. * @example
  539. * hash.arrayBuffer();
  540. */
  541. Md5.prototype.arrayBuffer = function () {
  542. this.finalize();
  543. var buffer = new ArrayBuffer(16);
  544. var blocks = new Uint32Array(buffer);
  545. blocks[0] = this.h0;
  546. blocks[1] = this.h1;
  547. blocks[2] = this.h2;
  548. blocks[3] = this.h3;
  549. return buffer;
  550. };
  551. /**
  552. * @method buffer
  553. * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
  554. * @memberof Md5
  555. * @instance
  556. * @description Output hash as ArrayBuffer
  557. * @returns {ArrayBuffer} ArrayBuffer
  558. * @see {@link md5.buffer}
  559. * @example
  560. * hash.buffer();
  561. */
  562. Md5.prototype.buffer = Md5.prototype.arrayBuffer;
  563. /**
  564. * @method base64
  565. * @memberof Md5
  566. * @instance
  567. * @description Output hash as base64 string
  568. * @returns {String} base64 string
  569. * @see {@link md5.base64}
  570. * @example
  571. * hash.base64();
  572. */
  573. Md5.prototype.base64 = function () {
  574. var v1, v2, v3, base64Str = '', bytes = this.array();
  575. for (var i = 0; i < 15;) {
  576. v1 = bytes[i++];
  577. v2 = bytes[i++];
  578. v3 = bytes[i++];
  579. base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
  580. BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
  581. BASE64_ENCODE_CHAR[(v2 << 2 | v3 >>> 6) & 63] +
  582. BASE64_ENCODE_CHAR[v3 & 63];
  583. }
  584. v1 = bytes[i];
  585. base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
  586. BASE64_ENCODE_CHAR[(v1 << 4) & 63] +
  587. '==';
  588. return base64Str;
  589. };
  590. var exports = createMethod();
  591. if (COMMON_JS) {
  592. module.exports = exports;
  593. } else {
  594. /**
  595. * @method md5
  596. * @description Md5 hash function, export to global in browsers.
  597. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  598. * @returns {String} md5 hashes
  599. * @example
  600. * md5(''); // d41d8cd98f00b204e9800998ecf8427e
  601. * md5('The quick brown fox jumps over the lazy dog'); // 9e107d9d372bb6826bd81d3542a419d6
  602. * md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffaddf22cbd0
  603. *
  604. * // It also supports UTF-8 encoding
  605. * md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07
  606. *
  607. * // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
  608. * md5([]); // d41d8cd98f00b204e9800998ecf8427e
  609. * md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
  610. */
  611. root.md5 = exports;
  612. if (AMD) {
  613. define(function () {
  614. return exports;
  615. });
  616. }
  617. }
  618. })();