md5.js 24 KB

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