md5.js 24 KB

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