md5.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. TypeScript Md5
  3. ==============
  4. Based on work by
  5. * Joseph Myers: http://www.myersdaily.org/joseph/javascript/md5-text.html
  6. * André Cruz: https://github.com/satazor/SparkMD5
  7. * Raymond Hill: https://github.com/gorhill/yamd5.js
  8. Effectively a TypeScrypt re-write of Raymond Hill JS Library
  9. The MIT License (MIT)
  10. Copyright (C) 2014 Raymond Hill
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17. The above copyright notice and this permission notice shall be included in
  18. all copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. THE SOFTWARE.
  26. DO WHAT YOU WANT TO PUBLIC LICENSE
  27. Version 2, December 2004
  28. Copyright (C) 2015 André Cruz <amdfcruz@gmail.com>
  29. Everyone is permitted to copy and distribute verbatim or modified
  30. copies of this license document, and changing it is allowed as long
  31. as the name is changed.
  32. DO WHAT YOU WANT TO PUBLIC LICENSE
  33. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  34. 0. You just DO WHAT YOU WANT TO.
  35. */
  36. ;
  37. export class Md5 {
  38. constructor() {
  39. this._dataLength = 0;
  40. this._bufferLength = 0;
  41. this._state = new Int32Array(4);
  42. this._buffer = new ArrayBuffer(68);
  43. this._buffer8 = new Uint8Array(this._buffer, 0, 68);
  44. this._buffer32 = new Uint32Array(this._buffer, 0, 17);
  45. this.start();
  46. }
  47. static hashStr(str, raw = false) {
  48. return this.onePassHasher
  49. .start()
  50. .appendStr(str)
  51. .end(raw);
  52. }
  53. static hashAsciiStr(str, raw = false) {
  54. return this.onePassHasher
  55. .start()
  56. .appendAsciiStr(str)
  57. .end(raw);
  58. }
  59. static _hex(x) {
  60. const hc = Md5.hexChars;
  61. const ho = Md5.hexOut;
  62. let n;
  63. let offset;
  64. let j;
  65. let i;
  66. for (i = 0; i < 4; i += 1) {
  67. offset = i * 8;
  68. n = x[i];
  69. for (j = 0; j < 8; j += 2) {
  70. ho[offset + 1 + j] = hc.charAt(n & 0x0F);
  71. n >>>= 4;
  72. ho[offset + 0 + j] = hc.charAt(n & 0x0F);
  73. n >>>= 4;
  74. }
  75. }
  76. return ho.join('');
  77. }
  78. static _md5cycle(x, k) {
  79. let a = x[0];
  80. let b = x[1];
  81. let c = x[2];
  82. let d = x[3];
  83. // ff()
  84. a += (b & c | ~b & d) + k[0] - 680876936 | 0;
  85. a = (a << 7 | a >>> 25) + b | 0;
  86. d += (a & b | ~a & c) + k[1] - 389564586 | 0;
  87. d = (d << 12 | d >>> 20) + a | 0;
  88. c += (d & a | ~d & b) + k[2] + 606105819 | 0;
  89. c = (c << 17 | c >>> 15) + d | 0;
  90. b += (c & d | ~c & a) + k[3] - 1044525330 | 0;
  91. b = (b << 22 | b >>> 10) + c | 0;
  92. a += (b & c | ~b & d) + k[4] - 176418897 | 0;
  93. a = (a << 7 | a >>> 25) + b | 0;
  94. d += (a & b | ~a & c) + k[5] + 1200080426 | 0;
  95. d = (d << 12 | d >>> 20) + a | 0;
  96. c += (d & a | ~d & b) + k[6] - 1473231341 | 0;
  97. c = (c << 17 | c >>> 15) + d | 0;
  98. b += (c & d | ~c & a) + k[7] - 45705983 | 0;
  99. b = (b << 22 | b >>> 10) + c | 0;
  100. a += (b & c | ~b & d) + k[8] + 1770035416 | 0;
  101. a = (a << 7 | a >>> 25) + b | 0;
  102. d += (a & b | ~a & c) + k[9] - 1958414417 | 0;
  103. d = (d << 12 | d >>> 20) + a | 0;
  104. c += (d & a | ~d & b) + k[10] - 42063 | 0;
  105. c = (c << 17 | c >>> 15) + d | 0;
  106. b += (c & d | ~c & a) + k[11] - 1990404162 | 0;
  107. b = (b << 22 | b >>> 10) + c | 0;
  108. a += (b & c | ~b & d) + k[12] + 1804603682 | 0;
  109. a = (a << 7 | a >>> 25) + b | 0;
  110. d += (a & b | ~a & c) + k[13] - 40341101 | 0;
  111. d = (d << 12 | d >>> 20) + a | 0;
  112. c += (d & a | ~d & b) + k[14] - 1502002290 | 0;
  113. c = (c << 17 | c >>> 15) + d | 0;
  114. b += (c & d | ~c & a) + k[15] + 1236535329 | 0;
  115. b = (b << 22 | b >>> 10) + c | 0;
  116. // gg()
  117. a += (b & d | c & ~d) + k[1] - 165796510 | 0;
  118. a = (a << 5 | a >>> 27) + b | 0;
  119. d += (a & c | b & ~c) + k[6] - 1069501632 | 0;
  120. d = (d << 9 | d >>> 23) + a | 0;
  121. c += (d & b | a & ~b) + k[11] + 643717713 | 0;
  122. c = (c << 14 | c >>> 18) + d | 0;
  123. b += (c & a | d & ~a) + k[0] - 373897302 | 0;
  124. b = (b << 20 | b >>> 12) + c | 0;
  125. a += (b & d | c & ~d) + k[5] - 701558691 | 0;
  126. a = (a << 5 | a >>> 27) + b | 0;
  127. d += (a & c | b & ~c) + k[10] + 38016083 | 0;
  128. d = (d << 9 | d >>> 23) + a | 0;
  129. c += (d & b | a & ~b) + k[15] - 660478335 | 0;
  130. c = (c << 14 | c >>> 18) + d | 0;
  131. b += (c & a | d & ~a) + k[4] - 405537848 | 0;
  132. b = (b << 20 | b >>> 12) + c | 0;
  133. a += (b & d | c & ~d) + k[9] + 568446438 | 0;
  134. a = (a << 5 | a >>> 27) + b | 0;
  135. d += (a & c | b & ~c) + k[14] - 1019803690 | 0;
  136. d = (d << 9 | d >>> 23) + a | 0;
  137. c += (d & b | a & ~b) + k[3] - 187363961 | 0;
  138. c = (c << 14 | c >>> 18) + d | 0;
  139. b += (c & a | d & ~a) + k[8] + 1163531501 | 0;
  140. b = (b << 20 | b >>> 12) + c | 0;
  141. a += (b & d | c & ~d) + k[13] - 1444681467 | 0;
  142. a = (a << 5 | a >>> 27) + b | 0;
  143. d += (a & c | b & ~c) + k[2] - 51403784 | 0;
  144. d = (d << 9 | d >>> 23) + a | 0;
  145. c += (d & b | a & ~b) + k[7] + 1735328473 | 0;
  146. c = (c << 14 | c >>> 18) + d | 0;
  147. b += (c & a | d & ~a) + k[12] - 1926607734 | 0;
  148. b = (b << 20 | b >>> 12) + c | 0;
  149. // hh()
  150. a += (b ^ c ^ d) + k[5] - 378558 | 0;
  151. a = (a << 4 | a >>> 28) + b | 0;
  152. d += (a ^ b ^ c) + k[8] - 2022574463 | 0;
  153. d = (d << 11 | d >>> 21) + a | 0;
  154. c += (d ^ a ^ b) + k[11] + 1839030562 | 0;
  155. c = (c << 16 | c >>> 16) + d | 0;
  156. b += (c ^ d ^ a) + k[14] - 35309556 | 0;
  157. b = (b << 23 | b >>> 9) + c | 0;
  158. a += (b ^ c ^ d) + k[1] - 1530992060 | 0;
  159. a = (a << 4 | a >>> 28) + b | 0;
  160. d += (a ^ b ^ c) + k[4] + 1272893353 | 0;
  161. d = (d << 11 | d >>> 21) + a | 0;
  162. c += (d ^ a ^ b) + k[7] - 155497632 | 0;
  163. c = (c << 16 | c >>> 16) + d | 0;
  164. b += (c ^ d ^ a) + k[10] - 1094730640 | 0;
  165. b = (b << 23 | b >>> 9) + c | 0;
  166. a += (b ^ c ^ d) + k[13] + 681279174 | 0;
  167. a = (a << 4 | a >>> 28) + b | 0;
  168. d += (a ^ b ^ c) + k[0] - 358537222 | 0;
  169. d = (d << 11 | d >>> 21) + a | 0;
  170. c += (d ^ a ^ b) + k[3] - 722521979 | 0;
  171. c = (c << 16 | c >>> 16) + d | 0;
  172. b += (c ^ d ^ a) + k[6] + 76029189 | 0;
  173. b = (b << 23 | b >>> 9) + c | 0;
  174. a += (b ^ c ^ d) + k[9] - 640364487 | 0;
  175. a = (a << 4 | a >>> 28) + b | 0;
  176. d += (a ^ b ^ c) + k[12] - 421815835 | 0;
  177. d = (d << 11 | d >>> 21) + a | 0;
  178. c += (d ^ a ^ b) + k[15] + 530742520 | 0;
  179. c = (c << 16 | c >>> 16) + d | 0;
  180. b += (c ^ d ^ a) + k[2] - 995338651 | 0;
  181. b = (b << 23 | b >>> 9) + c | 0;
  182. // ii()
  183. a += (c ^ (b | ~d)) + k[0] - 198630844 | 0;
  184. a = (a << 6 | a >>> 26) + b | 0;
  185. d += (b ^ (a | ~c)) + k[7] + 1126891415 | 0;
  186. d = (d << 10 | d >>> 22) + a | 0;
  187. c += (a ^ (d | ~b)) + k[14] - 1416354905 | 0;
  188. c = (c << 15 | c >>> 17) + d | 0;
  189. b += (d ^ (c | ~a)) + k[5] - 57434055 | 0;
  190. b = (b << 21 | b >>> 11) + c | 0;
  191. a += (c ^ (b | ~d)) + k[12] + 1700485571 | 0;
  192. a = (a << 6 | a >>> 26) + b | 0;
  193. d += (b ^ (a | ~c)) + k[3] - 1894986606 | 0;
  194. d = (d << 10 | d >>> 22) + a | 0;
  195. c += (a ^ (d | ~b)) + k[10] - 1051523 | 0;
  196. c = (c << 15 | c >>> 17) + d | 0;
  197. b += (d ^ (c | ~a)) + k[1] - 2054922799 | 0;
  198. b = (b << 21 | b >>> 11) + c | 0;
  199. a += (c ^ (b | ~d)) + k[8] + 1873313359 | 0;
  200. a = (a << 6 | a >>> 26) + b | 0;
  201. d += (b ^ (a | ~c)) + k[15] - 30611744 | 0;
  202. d = (d << 10 | d >>> 22) + a | 0;
  203. c += (a ^ (d | ~b)) + k[6] - 1560198380 | 0;
  204. c = (c << 15 | c >>> 17) + d | 0;
  205. b += (d ^ (c | ~a)) + k[13] + 1309151649 | 0;
  206. b = (b << 21 | b >>> 11) + c | 0;
  207. a += (c ^ (b | ~d)) + k[4] - 145523070 | 0;
  208. a = (a << 6 | a >>> 26) + b | 0;
  209. d += (b ^ (a | ~c)) + k[11] - 1120210379 | 0;
  210. d = (d << 10 | d >>> 22) + a | 0;
  211. c += (a ^ (d | ~b)) + k[2] + 718787259 | 0;
  212. c = (c << 15 | c >>> 17) + d | 0;
  213. b += (d ^ (c | ~a)) + k[9] - 343485551 | 0;
  214. b = (b << 21 | b >>> 11) + c | 0;
  215. x[0] = a + x[0] | 0;
  216. x[1] = b + x[1] | 0;
  217. x[2] = c + x[2] | 0;
  218. x[3] = d + x[3] | 0;
  219. }
  220. /**
  221. * Initialise buffer to be hashed
  222. */
  223. start() {
  224. this._dataLength = 0;
  225. this._bufferLength = 0;
  226. this._state.set(Md5.stateIdentity);
  227. return this;
  228. }
  229. // Char to code point to to array conversion:
  230. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
  231. // #Example.3A_Fixing_charCodeAt_to_handle_non-Basic-Multilingual-Plane_characters_if_their_presence_earlier_in_the_string_is_unknown
  232. /**
  233. * Append a UTF-8 string to the hash buffer
  234. * @param str String to append
  235. */
  236. appendStr(str) {
  237. const buf8 = this._buffer8;
  238. const buf32 = this._buffer32;
  239. let bufLen = this._bufferLength;
  240. let code;
  241. let i;
  242. for (i = 0; i < str.length; i += 1) {
  243. code = str.charCodeAt(i);
  244. if (code < 128) {
  245. buf8[bufLen++] = code;
  246. }
  247. else if (code < 0x800) {
  248. buf8[bufLen++] = (code >>> 6) + 0xC0;
  249. buf8[bufLen++] = code & 0x3F | 0x80;
  250. }
  251. else if (code < 0xD800 || code > 0xDBFF) {
  252. buf8[bufLen++] = (code >>> 12) + 0xE0;
  253. buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80;
  254. buf8[bufLen++] = (code & 0x3F) | 0x80;
  255. }
  256. else {
  257. code = ((code - 0xD800) * 0x400) + (str.charCodeAt(++i) - 0xDC00) + 0x10000;
  258. if (code > 0x10FFFF) {
  259. throw new Error('Unicode standard supports code points up to U+10FFFF');
  260. }
  261. buf8[bufLen++] = (code >>> 18) + 0xF0;
  262. buf8[bufLen++] = (code >>> 12 & 0x3F) | 0x80;
  263. buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80;
  264. buf8[bufLen++] = (code & 0x3F) | 0x80;
  265. }
  266. if (bufLen >= 64) {
  267. this._dataLength += 64;
  268. Md5._md5cycle(this._state, buf32);
  269. bufLen -= 64;
  270. buf32[0] = buf32[16];
  271. }
  272. }
  273. this._bufferLength = bufLen;
  274. return this;
  275. }
  276. /**
  277. * Append an ASCII string to the hash buffer
  278. * @param str String to append
  279. */
  280. appendAsciiStr(str) {
  281. const buf8 = this._buffer8;
  282. const buf32 = this._buffer32;
  283. let bufLen = this._bufferLength;
  284. let i;
  285. let j = 0;
  286. for (;;) {
  287. i = Math.min(str.length - j, 64 - bufLen);
  288. while (i--) {
  289. buf8[bufLen++] = str.charCodeAt(j++);
  290. }
  291. if (bufLen < 64) {
  292. break;
  293. }
  294. this._dataLength += 64;
  295. Md5._md5cycle(this._state, buf32);
  296. bufLen = 0;
  297. }
  298. this._bufferLength = bufLen;
  299. return this;
  300. }
  301. /**
  302. * Append a byte array to the hash buffer
  303. * @param input array to append
  304. */
  305. appendByteArray(input) {
  306. const buf8 = this._buffer8;
  307. const buf32 = this._buffer32;
  308. let bufLen = this._bufferLength;
  309. let i;
  310. let j = 0;
  311. for (;;) {
  312. i = Math.min(input.length - j, 64 - bufLen);
  313. while (i--) {
  314. buf8[bufLen++] = input[j++];
  315. }
  316. if (bufLen < 64) {
  317. break;
  318. }
  319. this._dataLength += 64;
  320. Md5._md5cycle(this._state, buf32);
  321. bufLen = 0;
  322. }
  323. this._bufferLength = bufLen;
  324. return this;
  325. }
  326. /**
  327. * Get the state of the hash buffer
  328. */
  329. getState() {
  330. const s = this._state;
  331. return {
  332. buffer: String.fromCharCode.apply(null, Array.from(this._buffer8)),
  333. buflen: this._bufferLength,
  334. length: this._dataLength,
  335. state: [s[0], s[1], s[2], s[3]]
  336. };
  337. }
  338. /**
  339. * Override the current state of the hash buffer
  340. * @param state New hash buffer state
  341. */
  342. setState(state) {
  343. const buf = state.buffer;
  344. const x = state.state;
  345. const s = this._state;
  346. let i;
  347. this._dataLength = state.length;
  348. this._bufferLength = state.buflen;
  349. s[0] = x[0];
  350. s[1] = x[1];
  351. s[2] = x[2];
  352. s[3] = x[3];
  353. for (i = 0; i < buf.length; i += 1) {
  354. this._buffer8[i] = buf.charCodeAt(i);
  355. }
  356. }
  357. /**
  358. * Hash the current state of the hash buffer and return the result
  359. * @param raw Whether to return the value as an `Int32Array`
  360. */
  361. end(raw = false) {
  362. const bufLen = this._bufferLength;
  363. const buf8 = this._buffer8;
  364. const buf32 = this._buffer32;
  365. const i = (bufLen >> 2) + 1;
  366. this._dataLength += bufLen;
  367. const dataBitsLen = this._dataLength * 8;
  368. buf8[bufLen] = 0x80;
  369. buf8[bufLen + 1] = buf8[bufLen + 2] = buf8[bufLen + 3] = 0;
  370. buf32.set(Md5.buffer32Identity.subarray(i), i);
  371. if (bufLen > 55) {
  372. Md5._md5cycle(this._state, buf32);
  373. buf32.set(Md5.buffer32Identity);
  374. }
  375. // Do the final computation based on the tail and length
  376. // Beware that the final length may not fit in 32 bits so we take care of that
  377. if (dataBitsLen <= 0xFFFFFFFF) {
  378. buf32[14] = dataBitsLen;
  379. }
  380. else {
  381. const matches = dataBitsLen.toString(16).match(/(.*?)(.{0,8})$/);
  382. if (matches === null) {
  383. return;
  384. }
  385. const lo = parseInt(matches[2], 16);
  386. const hi = parseInt(matches[1], 16) || 0;
  387. buf32[14] = lo;
  388. buf32[15] = hi;
  389. }
  390. Md5._md5cycle(this._state, buf32);
  391. return raw ? this._state : Md5._hex(this._state);
  392. }
  393. }
  394. // Private Static Variables
  395. Md5.stateIdentity = new Int32Array([1732584193, -271733879, -1732584194, 271733878]);
  396. Md5.buffer32Identity = new Int32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
  397. Md5.hexChars = '0123456789abcdef';
  398. Md5.hexOut = [];
  399. // Permanent instance is to use for one-call hashing
  400. Md5.onePassHasher = new Md5();
  401. if (Md5.hashStr('hello') !== '5d41402abc4b2a76b9719d911017c592') {
  402. throw new Error('Md5 self test failed.');
  403. }
  404. //# sourceMappingURL=md5.js.map