Encrypted.js 20 KB

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