spark-md5.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. "use strict";
  2. /*
  3. * Fastest md5 implementation around (JKM md5).
  4. * Credits: Joseph Myers
  5. *
  6. * @see http://www.myersdaily.org/joseph/javascript/md5-text.html
  7. * @see http://jsperf.com/md5-shootout/7
  8. */
  9. /* this function is much faster,
  10. so if possible we use it. Some IEs
  11. are the only ones I know of that
  12. need the idiotic second function,
  13. generated by an if clause. */
  14. var add32 = function (a, b) {
  15. return (a + b) & 0xffffffff;
  16. },
  17. hex_chr = [
  18. "0",
  19. "1",
  20. "2",
  21. "3",
  22. "4",
  23. "5",
  24. "6",
  25. "7",
  26. "8",
  27. "9",
  28. "a",
  29. "b",
  30. "c",
  31. "d",
  32. "e",
  33. "f",
  34. ];
  35. function cmn(q, a, b, x, s, t) {
  36. a = add32(add32(a, q), add32(x, t));
  37. return add32((a << s) | (a >>> (32 - s)), b);
  38. }
  39. function md5cycle(x, k) {
  40. var a = x[0],
  41. b = x[1],
  42. c = x[2],
  43. d = x[3];
  44. a += (((b & c) | (~b & d)) + k[0] - 680876936) | 0;
  45. a = (((a << 7) | (a >>> 25)) + b) | 0;
  46. d += (((a & b) | (~a & c)) + k[1] - 389564586) | 0;
  47. d = (((d << 12) | (d >>> 20)) + a) | 0;
  48. c += (((d & a) | (~d & b)) + k[2] + 606105819) | 0;
  49. c = (((c << 17) | (c >>> 15)) + d) | 0;
  50. b += (((c & d) | (~c & a)) + k[3] - 1044525330) | 0;
  51. b = (((b << 22) | (b >>> 10)) + c) | 0;
  52. a += (((b & c) | (~b & d)) + k[4] - 176418897) | 0;
  53. a = (((a << 7) | (a >>> 25)) + b) | 0;
  54. d += (((a & b) | (~a & c)) + k[5] + 1200080426) | 0;
  55. d = (((d << 12) | (d >>> 20)) + a) | 0;
  56. c += (((d & a) | (~d & b)) + k[6] - 1473231341) | 0;
  57. c = (((c << 17) | (c >>> 15)) + d) | 0;
  58. b += (((c & d) | (~c & a)) + k[7] - 45705983) | 0;
  59. b = (((b << 22) | (b >>> 10)) + c) | 0;
  60. a += (((b & c) | (~b & d)) + k[8] + 1770035416) | 0;
  61. a = (((a << 7) | (a >>> 25)) + b) | 0;
  62. d += (((a & b) | (~a & c)) + k[9] - 1958414417) | 0;
  63. d = (((d << 12) | (d >>> 20)) + a) | 0;
  64. c += (((d & a) | (~d & b)) + k[10] - 42063) | 0;
  65. c = (((c << 17) | (c >>> 15)) + d) | 0;
  66. b += (((c & d) | (~c & a)) + k[11] - 1990404162) | 0;
  67. b = (((b << 22) | (b >>> 10)) + c) | 0;
  68. a += (((b & c) | (~b & d)) + k[12] + 1804603682) | 0;
  69. a = (((a << 7) | (a >>> 25)) + b) | 0;
  70. d += (((a & b) | (~a & c)) + k[13] - 40341101) | 0;
  71. d = (((d << 12) | (d >>> 20)) + a) | 0;
  72. c += (((d & a) | (~d & b)) + k[14] - 1502002290) | 0;
  73. c = (((c << 17) | (c >>> 15)) + d) | 0;
  74. b += (((c & d) | (~c & a)) + k[15] + 1236535329) | 0;
  75. b = (((b << 22) | (b >>> 10)) + c) | 0;
  76. a += (((b & d) | (c & ~d)) + k[1] - 165796510) | 0;
  77. a = (((a << 5) | (a >>> 27)) + b) | 0;
  78. d += (((a & c) | (b & ~c)) + k[6] - 1069501632) | 0;
  79. d = (((d << 9) | (d >>> 23)) + a) | 0;
  80. c += (((d & b) | (a & ~b)) + k[11] + 643717713) | 0;
  81. c = (((c << 14) | (c >>> 18)) + d) | 0;
  82. b += (((c & a) | (d & ~a)) + k[0] - 373897302) | 0;
  83. b = (((b << 20) | (b >>> 12)) + c) | 0;
  84. a += (((b & d) | (c & ~d)) + k[5] - 701558691) | 0;
  85. a = (((a << 5) | (a >>> 27)) + b) | 0;
  86. d += (((a & c) | (b & ~c)) + k[10] + 38016083) | 0;
  87. d = (((d << 9) | (d >>> 23)) + a) | 0;
  88. c += (((d & b) | (a & ~b)) + k[15] - 660478335) | 0;
  89. c = (((c << 14) | (c >>> 18)) + d) | 0;
  90. b += (((c & a) | (d & ~a)) + k[4] - 405537848) | 0;
  91. b = (((b << 20) | (b >>> 12)) + c) | 0;
  92. a += (((b & d) | (c & ~d)) + k[9] + 568446438) | 0;
  93. a = (((a << 5) | (a >>> 27)) + b) | 0;
  94. d += (((a & c) | (b & ~c)) + k[14] - 1019803690) | 0;
  95. d = (((d << 9) | (d >>> 23)) + a) | 0;
  96. c += (((d & b) | (a & ~b)) + k[3] - 187363961) | 0;
  97. c = (((c << 14) | (c >>> 18)) + d) | 0;
  98. b += (((c & a) | (d & ~a)) + k[8] + 1163531501) | 0;
  99. b = (((b << 20) | (b >>> 12)) + c) | 0;
  100. a += (((b & d) | (c & ~d)) + k[13] - 1444681467) | 0;
  101. a = (((a << 5) | (a >>> 27)) + b) | 0;
  102. d += (((a & c) | (b & ~c)) + k[2] - 51403784) | 0;
  103. d = (((d << 9) | (d >>> 23)) + a) | 0;
  104. c += (((d & b) | (a & ~b)) + k[7] + 1735328473) | 0;
  105. c = (((c << 14) | (c >>> 18)) + d) | 0;
  106. b += (((c & a) | (d & ~a)) + k[12] - 1926607734) | 0;
  107. b = (((b << 20) | (b >>> 12)) + c) | 0;
  108. a += ((b ^ c ^ d) + k[5] - 378558) | 0;
  109. a = (((a << 4) | (a >>> 28)) + b) | 0;
  110. d += ((a ^ b ^ c) + k[8] - 2022574463) | 0;
  111. d = (((d << 11) | (d >>> 21)) + a) | 0;
  112. c += ((d ^ a ^ b) + k[11] + 1839030562) | 0;
  113. c = (((c << 16) | (c >>> 16)) + d) | 0;
  114. b += ((c ^ d ^ a) + k[14] - 35309556) | 0;
  115. b = (((b << 23) | (b >>> 9)) + c) | 0;
  116. a += ((b ^ c ^ d) + k[1] - 1530992060) | 0;
  117. a = (((a << 4) | (a >>> 28)) + b) | 0;
  118. d += ((a ^ b ^ c) + k[4] + 1272893353) | 0;
  119. d = (((d << 11) | (d >>> 21)) + a) | 0;
  120. c += ((d ^ a ^ b) + k[7] - 155497632) | 0;
  121. c = (((c << 16) | (c >>> 16)) + d) | 0;
  122. b += ((c ^ d ^ a) + k[10] - 1094730640) | 0;
  123. b = (((b << 23) | (b >>> 9)) + c) | 0;
  124. a += ((b ^ c ^ d) + k[13] + 681279174) | 0;
  125. a = (((a << 4) | (a >>> 28)) + b) | 0;
  126. d += ((a ^ b ^ c) + k[0] - 358537222) | 0;
  127. d = (((d << 11) | (d >>> 21)) + a) | 0;
  128. c += ((d ^ a ^ b) + k[3] - 722521979) | 0;
  129. c = (((c << 16) | (c >>> 16)) + d) | 0;
  130. b += ((c ^ d ^ a) + k[6] + 76029189) | 0;
  131. b = (((b << 23) | (b >>> 9)) + c) | 0;
  132. a += ((b ^ c ^ d) + k[9] - 640364487) | 0;
  133. a = (((a << 4) | (a >>> 28)) + b) | 0;
  134. d += ((a ^ b ^ c) + k[12] - 421815835) | 0;
  135. d = (((d << 11) | (d >>> 21)) + a) | 0;
  136. c += ((d ^ a ^ b) + k[15] + 530742520) | 0;
  137. c = (((c << 16) | (c >>> 16)) + d) | 0;
  138. b += ((c ^ d ^ a) + k[2] - 995338651) | 0;
  139. b = (((b << 23) | (b >>> 9)) + c) | 0;
  140. a += ((c ^ (b | ~d)) + k[0] - 198630844) | 0;
  141. a = (((a << 6) | (a >>> 26)) + b) | 0;
  142. d += ((b ^ (a | ~c)) + k[7] + 1126891415) | 0;
  143. d = (((d << 10) | (d >>> 22)) + a) | 0;
  144. c += ((a ^ (d | ~b)) + k[14] - 1416354905) | 0;
  145. c = (((c << 15) | (c >>> 17)) + d) | 0;
  146. b += ((d ^ (c | ~a)) + k[5] - 57434055) | 0;
  147. b = (((b << 21) | (b >>> 11)) + c) | 0;
  148. a += ((c ^ (b | ~d)) + k[12] + 1700485571) | 0;
  149. a = (((a << 6) | (a >>> 26)) + b) | 0;
  150. d += ((b ^ (a | ~c)) + k[3] - 1894986606) | 0;
  151. d = (((d << 10) | (d >>> 22)) + a) | 0;
  152. c += ((a ^ (d | ~b)) + k[10] - 1051523) | 0;
  153. c = (((c << 15) | (c >>> 17)) + d) | 0;
  154. b += ((d ^ (c | ~a)) + k[1] - 2054922799) | 0;
  155. b = (((b << 21) | (b >>> 11)) + c) | 0;
  156. a += ((c ^ (b | ~d)) + k[8] + 1873313359) | 0;
  157. a = (((a << 6) | (a >>> 26)) + b) | 0;
  158. d += ((b ^ (a | ~c)) + k[15] - 30611744) | 0;
  159. d = (((d << 10) | (d >>> 22)) + a) | 0;
  160. c += ((a ^ (d | ~b)) + k[6] - 1560198380) | 0;
  161. c = (((c << 15) | (c >>> 17)) + d) | 0;
  162. b += ((d ^ (c | ~a)) + k[13] + 1309151649) | 0;
  163. b = (((b << 21) | (b >>> 11)) + c) | 0;
  164. a += ((c ^ (b | ~d)) + k[4] - 145523070) | 0;
  165. a = (((a << 6) | (a >>> 26)) + b) | 0;
  166. d += ((b ^ (a | ~c)) + k[11] - 1120210379) | 0;
  167. d = (((d << 10) | (d >>> 22)) + a) | 0;
  168. c += ((a ^ (d | ~b)) + k[2] + 718787259) | 0;
  169. c = (((c << 15) | (c >>> 17)) + d) | 0;
  170. b += ((d ^ (c | ~a)) + k[9] - 343485551) | 0;
  171. b = (((b << 21) | (b >>> 11)) + c) | 0;
  172. x[0] = (a + x[0]) | 0;
  173. x[1] = (b + x[1]) | 0;
  174. x[2] = (c + x[2]) | 0;
  175. x[3] = (d + x[3]) | 0;
  176. }
  177. function md5blk(s) {
  178. var md5blks = [],
  179. i; /* Andy King said do it this way. */
  180. for (i = 0; i < 64; i += 4) {
  181. md5blks[i >> 2] =
  182. s.charCodeAt(i) +
  183. (s.charCodeAt(i + 1) << 8) +
  184. (s.charCodeAt(i + 2) << 16) +
  185. (s.charCodeAt(i + 3) << 24);
  186. }
  187. return md5blks;
  188. }
  189. function md5blk_array(a) {
  190. var md5blks = [],
  191. i; /* Andy King said do it this way. */
  192. for (i = 0; i < 64; i += 4) {
  193. md5blks[i >> 2] =
  194. a[i] + (a[i + 1] << 8) + (a[i + 2] << 16) + (a[i + 3] << 24);
  195. }
  196. return md5blks;
  197. }
  198. function md51(s) {
  199. var n = s.length,
  200. state = [1732584193, -271733879, -1732584194, 271733878],
  201. i,
  202. length,
  203. tail,
  204. tmp,
  205. lo,
  206. hi;
  207. for (i = 64; i <= n; i += 64) {
  208. md5cycle(state, md5blk(s.substring(i - 64, i)));
  209. }
  210. s = s.substring(i - 64);
  211. length = s.length;
  212. tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  213. for (i = 0; i < length; i += 1) {
  214. tail[i >> 2] |= s.charCodeAt(i) << (i % 4 << 3);
  215. }
  216. tail[i >> 2] |= 0x80 << (i % 4 << 3);
  217. if (i > 55) {
  218. md5cycle(state, tail);
  219. for (i = 0; i < 16; i += 1) {
  220. tail[i] = 0;
  221. }
  222. }
  223. // Beware that the final length might not fit in 32 bits so we take care of that
  224. tmp = n * 8;
  225. tmp = tmp.toString(16).match(/(.*?)(.{0,8})$/);
  226. lo = parseInt(tmp[2], 16);
  227. hi = parseInt(tmp[1], 16) || 0;
  228. tail[14] = lo;
  229. tail[15] = hi;
  230. md5cycle(state, tail);
  231. return state;
  232. }
  233. function md51_array(a) {
  234. var n = a.length,
  235. state = [1732584193, -271733879, -1732584194, 271733878],
  236. i,
  237. length,
  238. tail,
  239. tmp,
  240. lo,
  241. hi;
  242. for (i = 64; i <= n; i += 64) {
  243. md5cycle(state, md5blk_array(a.subarray(i - 64, i)));
  244. }
  245. // Not sure if it is a bug, however IE10 will always produce a sub array of length 1
  246. // containing the last element of the parent array if the sub array specified starts
  247. // beyond the length of the parent array - weird.
  248. // https://connect.microsoft.com/IE/feedback/details/771452/typed-array-subarray-issue
  249. a = i - 64 < n ? a.subarray(i - 64) : new Uint8Array(0);
  250. length = a.length;
  251. tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  252. for (i = 0; i < length; i += 1) {
  253. tail[i >> 2] |= a[i] << (i % 4 << 3);
  254. }
  255. tail[i >> 2] |= 0x80 << (i % 4 << 3);
  256. if (i > 55) {
  257. md5cycle(state, tail);
  258. for (i = 0; i < 16; i += 1) {
  259. tail[i] = 0;
  260. }
  261. }
  262. // Beware that the final length might not fit in 32 bits so we take care of that
  263. tmp = n * 8;
  264. tmp = tmp.toString(16).match(/(.*?)(.{0,8})$/);
  265. lo = parseInt(tmp[2], 16);
  266. hi = parseInt(tmp[1], 16) || 0;
  267. tail[14] = lo;
  268. tail[15] = hi;
  269. md5cycle(state, tail);
  270. return state;
  271. }
  272. function rhex(n) {
  273. var s = "",
  274. j;
  275. for (j = 0; j < 4; j += 1) {
  276. s += hex_chr[(n >> (j * 8 + 4)) & 0x0f] + hex_chr[(n >> (j * 8)) & 0x0f];
  277. }
  278. return s;
  279. }
  280. function hex(x) {
  281. var i;
  282. for (i = 0; i < x.length; i += 1) {
  283. x[i] = rhex(x[i]);
  284. }
  285. return x.join("");
  286. }
  287. // In some cases the fast add32 function cannot be used..
  288. if (hex(md51("hello")) !== "5d41402abc4b2a76b9719d911017c592") {
  289. add32 = function (x, y) {
  290. var lsw = (x & 0xffff) + (y & 0xffff),
  291. msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  292. return (msw << 16) | (lsw & 0xffff);
  293. };
  294. }
  295. // ---------------------------------------------------
  296. /**
  297. * ArrayBuffer slice polyfill.
  298. *
  299. * @see https://github.com/ttaubert/node-arraybuffer-slice
  300. */
  301. if (typeof ArrayBuffer !== "undefined" && !ArrayBuffer.prototype.slice) {
  302. (function () {
  303. function clamp(val, length) {
  304. val = val | 0 || 0;
  305. if (val < 0) {
  306. return Math.max(val + length, 0);
  307. }
  308. return Math.min(val, length);
  309. }
  310. ArrayBuffer.prototype.slice = function (from, to) {
  311. var length = this.byteLength,
  312. begin = clamp(from, length),
  313. end = length,
  314. num,
  315. target,
  316. targetArray,
  317. sourceArray;
  318. if (to !== undefined) {
  319. end = clamp(to, length);
  320. }
  321. if (begin > end) {
  322. return new ArrayBuffer(0);
  323. }
  324. num = end - begin;
  325. target = new ArrayBuffer(num);
  326. targetArray = new Uint8Array(target);
  327. sourceArray = new Uint8Array(this, begin, num);
  328. targetArray.set(sourceArray);
  329. return target;
  330. };
  331. })();
  332. }
  333. // ---------------------------------------------------
  334. /**
  335. * Helpers.
  336. */
  337. function toUtf8(str) {
  338. if (/[\u0080-\uFFFF]/.test(str)) {
  339. str = unescape(encodeURIComponent(str));
  340. }
  341. return str;
  342. }
  343. function utf8Str2ArrayBuffer(str, returnUInt8Array) {
  344. var length = str.length,
  345. buff = new ArrayBuffer(length),
  346. arr = new Uint8Array(buff),
  347. i;
  348. for (i = 0; i < length; i += 1) {
  349. arr[i] = str.charCodeAt(i);
  350. }
  351. return returnUInt8Array ? arr : buff;
  352. }
  353. function arrayBuffer2Utf8Str(buff) {
  354. return String.fromCharCode.apply(null, new Uint8Array(buff));
  355. }
  356. function concatenateArrayBuffers(first, second, returnUInt8Array) {
  357. var result = new Uint8Array(first.byteLength + second.byteLength);
  358. result.set(new Uint8Array(first));
  359. result.set(new Uint8Array(second), first.byteLength);
  360. return returnUInt8Array ? result : result.buffer;
  361. }
  362. function hexToBinaryString(hex) {
  363. var bytes = [],
  364. length = hex.length,
  365. x;
  366. for (x = 0; x < length - 1; x += 2) {
  367. bytes.push(parseInt(hex.substr(x, 2), 16));
  368. }
  369. return String.fromCharCode.apply(String, bytes);
  370. }
  371. // ---------------------------------------------------
  372. /**
  373. * SparkMD5 OOP implementation.
  374. *
  375. * Use this class to perform an incremental md5, otherwise use the
  376. * static methods instead.
  377. */
  378. function SparkMD5() {
  379. // call reset to init the instance
  380. this.reset();
  381. }
  382. /**
  383. * Appends a string.
  384. * A conversion will be applied if an utf8 string is detected.
  385. *
  386. * @param {String} str The string to be appended
  387. *
  388. * @return {SparkMD5} The instance itself
  389. */
  390. SparkMD5.prototype.append = function (str) {
  391. // Converts the string to utf8 bytes if necessary
  392. // Then append as binary
  393. this.appendBinary(toUtf8(str));
  394. return this;
  395. };
  396. /**
  397. * Appends a binary string.
  398. *
  399. * @param {String} contents The binary string to be appended
  400. *
  401. * @return {SparkMD5} The instance itself
  402. */
  403. SparkMD5.prototype.appendBinary = function (contents) {
  404. this._buff += contents;
  405. this._length += contents.length;
  406. var length = this._buff.length,
  407. i;
  408. for (i = 64; i <= length; i += 64) {
  409. md5cycle(this._hash, md5blk(this._buff.substring(i - 64, i)));
  410. }
  411. this._buff = this._buff.substring(i - 64);
  412. return this;
  413. };
  414. /**
  415. * Finishes the incremental computation, reseting the internal state and
  416. * returning the result.
  417. *
  418. * @param {Boolean} raw True to get the raw string, false to get the hex string
  419. *
  420. * @return {String} The result
  421. */
  422. SparkMD5.prototype.end = function (raw) {
  423. var buff = this._buff,
  424. length = buff.length,
  425. i,
  426. tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  427. ret;
  428. for (i = 0; i < length; i += 1) {
  429. tail[i >> 2] |= buff.charCodeAt(i) << (i % 4 << 3);
  430. }
  431. this._finish(tail, length);
  432. ret = hex(this._hash);
  433. if (raw) {
  434. ret = hexToBinaryString(ret);
  435. }
  436. this.reset();
  437. return ret;
  438. };
  439. /**
  440. * Resets the internal state of the computation.
  441. *
  442. * @return {SparkMD5} The instance itself
  443. */
  444. SparkMD5.prototype.reset = function () {
  445. this._buff = "";
  446. this._length = 0;
  447. this._hash = [1732584193, -271733879, -1732584194, 271733878];
  448. return this;
  449. };
  450. /**
  451. * Gets the internal state of the computation.
  452. *
  453. * @return {Object} The state
  454. */
  455. SparkMD5.prototype.getState = function () {
  456. return {
  457. buff: this._buff,
  458. length: this._length,
  459. hash: this._hash.slice(),
  460. };
  461. };
  462. /**
  463. * Gets the internal state of the computation.
  464. *
  465. * @param {Object} state The state
  466. *
  467. * @return {SparkMD5} The instance itself
  468. */
  469. SparkMD5.prototype.setState = function (state) {
  470. this._buff = state.buff;
  471. this._length = state.length;
  472. this._hash = state.hash;
  473. return this;
  474. };
  475. /**
  476. * Releases memory used by the incremental buffer and other additional
  477. * resources. If you plan to use the instance again, use reset instead.
  478. */
  479. SparkMD5.prototype.destroy = function () {
  480. delete this._hash;
  481. delete this._buff;
  482. delete this._length;
  483. };
  484. /**
  485. * Finish the final calculation based on the tail.
  486. *
  487. * @param {Array} tail The tail (will be modified)
  488. * @param {Number} length The length of the remaining buffer
  489. */
  490. SparkMD5.prototype._finish = function (tail, length) {
  491. var i = length,
  492. tmp,
  493. lo,
  494. hi;
  495. tail[i >> 2] |= 0x80 << (i % 4 << 3);
  496. if (i > 55) {
  497. md5cycle(this._hash, tail);
  498. for (i = 0; i < 16; i += 1) {
  499. tail[i] = 0;
  500. }
  501. }
  502. // Do the final computation based on the tail and length
  503. // Beware that the final length may not fit in 32 bits so we take care of that
  504. tmp = this._length * 8;
  505. tmp = tmp.toString(16).match(/(.*?)(.{0,8})$/);
  506. lo = parseInt(tmp[2], 16);
  507. hi = parseInt(tmp[1], 16) || 0;
  508. tail[14] = lo;
  509. tail[15] = hi;
  510. md5cycle(this._hash, tail);
  511. };
  512. /**
  513. * Performs the md5 hash on a string.
  514. * A conversion will be applied if utf8 string is detected.
  515. *
  516. * @param {String} str The string
  517. * @param {Boolean} [raw] True to get the raw string, false to get the hex string
  518. *
  519. * @return {String} The result
  520. */
  521. SparkMD5.hash = function (str, raw) {
  522. // Converts the string to utf8 bytes if necessary
  523. // Then compute it using the binary function
  524. return SparkMD5.hashBinary(toUtf8(str), raw);
  525. };
  526. /**
  527. * Performs the md5 hash on a binary string.
  528. *
  529. * @param {String} content The binary string
  530. * @param {Boolean} [raw] True to get the raw string, false to get the hex string
  531. *
  532. * @return {String} The result
  533. */
  534. SparkMD5.hashBinary = function (content, raw) {
  535. var hash = md51(content),
  536. ret = hex(hash);
  537. return raw ? hexToBinaryString(ret) : ret;
  538. };
  539. // ---------------------------------------------------
  540. /**
  541. * SparkMD5 OOP implementation for array buffers.
  542. *
  543. * Use this class to perform an incremental md5 ONLY for array buffers.
  544. */
  545. SparkMD5.ArrayBuffer = function () {
  546. // call reset to init the instance
  547. this.reset();
  548. };
  549. /**
  550. * Appends an array buffer.
  551. *
  552. * @param {ArrayBuffer} arr The array to be appended
  553. *
  554. * @return {SparkMD5.ArrayBuffer} The instance itself
  555. */
  556. SparkMD5.ArrayBuffer.prototype.append = function (arr) {
  557. var buff = concatenateArrayBuffers(this._buff.buffer, arr, true),
  558. length = buff.length,
  559. i;
  560. this._length += arr.byteLength;
  561. for (i = 64; i <= length; i += 64) {
  562. md5cycle(this._hash, md5blk_array(buff.subarray(i - 64, i)));
  563. }
  564. this._buff =
  565. i - 64 < length
  566. ? new Uint8Array(buff.buffer.slice(i - 64))
  567. : new Uint8Array(0);
  568. return this;
  569. };
  570. /**
  571. * Finishes the incremental computation, reseting the internal state and
  572. * returning the result.
  573. *
  574. * @param {Boolean} raw True to get the raw string, false to get the hex string
  575. *
  576. * @return {String} The result
  577. */
  578. SparkMD5.ArrayBuffer.prototype.end = function (raw) {
  579. var buff = this._buff,
  580. length = buff.length,
  581. tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  582. i,
  583. ret;
  584. for (i = 0; i < length; i += 1) {
  585. tail[i >> 2] |= buff[i] << (i % 4 << 3);
  586. }
  587. this._finish(tail, length);
  588. ret = hex(this._hash);
  589. if (raw) {
  590. ret = hexToBinaryString(ret);
  591. }
  592. this.reset();
  593. return ret;
  594. };
  595. /**
  596. * Resets the internal state of the computation.
  597. *
  598. * @return {SparkMD5.ArrayBuffer} The instance itself
  599. */
  600. SparkMD5.ArrayBuffer.prototype.reset = function () {
  601. this._buff = new Uint8Array(0);
  602. this._length = 0;
  603. this._hash = [1732584193, -271733879, -1732584194, 271733878];
  604. return this;
  605. };
  606. /**
  607. * Gets the internal state of the computation.
  608. *
  609. * @return {Object} The state
  610. */
  611. SparkMD5.ArrayBuffer.prototype.getState = function () {
  612. var state = SparkMD5.prototype.getState.call(this);
  613. // Convert buffer to a string
  614. state.buff = arrayBuffer2Utf8Str(state.buff);
  615. return state;
  616. };
  617. /**
  618. * Gets the internal state of the computation.
  619. *
  620. * @param {Object} state The state
  621. *
  622. * @return {SparkMD5.ArrayBuffer} The instance itself
  623. */
  624. SparkMD5.ArrayBuffer.prototype.setState = function (state) {
  625. // Convert string to buffer
  626. state.buff = utf8Str2ArrayBuffer(state.buff, true);
  627. return SparkMD5.prototype.setState.call(this, state);
  628. };
  629. SparkMD5.ArrayBuffer.prototype.destroy = SparkMD5.prototype.destroy;
  630. SparkMD5.ArrayBuffer.prototype._finish = SparkMD5.prototype._finish;
  631. /**
  632. * Performs the md5 hash on an array buffer.
  633. *
  634. * @param {ArrayBuffer} arr The array buffer
  635. * @param {Boolean} [raw] True to get the raw string, false to get the hex one
  636. *
  637. * @return {String} The result
  638. */
  639. SparkMD5.ArrayBuffer.hash = function (arr, raw) {
  640. var hash = md51_array(new Uint8Array(arr)),
  641. ret = hex(hash);
  642. return raw ? hexToBinaryString(ret) : ret;
  643. };
  644. export default SparkMD5;