md5.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /**
  2. * Namespace for hashing and other cryptographic functions
  3. * Copyright (c) Andrew Valums
  4. * Licensed under the MIT license, http://valums.com/mit-license/
  5. */
  6. var V = V || {};
  7. V.Security = V.Security || {};
  8. (function () {
  9. // for faster access
  10. var S = V.Security;
  11. /**
  12. * The highest integer value a number can go to without losing precision.
  13. */
  14. S.maxExactInt = Math.pow(2, 53);
  15. /**
  16. * Converts string from internal UTF-16 to UTF-8
  17. * and saves it using array of numbers (bytes), 0-255 per cell
  18. * @param {String} str
  19. * @return {Array}
  20. */
  21. S.toUtf8ByteArr = function (str) {
  22. var arr = [],
  23. code;
  24. for (var i = 0; i < str.length; i++) {
  25. code = str.charCodeAt(i);
  26. /*
  27. Note that charCodeAt will always return a value that is less than 65,536.
  28. This is because the higher code points are represented by a pair of (lower valued)
  29. "surrogate" pseudo-characters which are used to comprise the real character.
  30. Because of this, in order to examine or reproduce the full character for
  31. individual characters of value 65,536 and above, for such characters,
  32. it is necessary to retrieve not only charCodeAt(0), but also charCodeAt(1).
  33. */
  34. if (0xD800 <= code && code <= 0xDBFF) {
  35. // UTF-16 high surrogate
  36. var hi = code,
  37. low = str.charCodeAt(i + 1);
  38. code = ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
  39. i++;
  40. }
  41. if (code <= 127) {
  42. arr[arr.length] = code;
  43. } else if (code <= 2047) {
  44. arr[arr.length] = (code >>> 6) + 0xC0;
  45. arr[arr.length] = code & 0x3F | 0x80;
  46. } else if (code <= 65535) {
  47. arr[arr.length] = (code >>> 12) + 0xE0;
  48. arr[arr.length] = (code >>> 6 & 0x3F) | 0x80;
  49. arr[arr.length] = (code & 0x3F) | 0x80;
  50. } else if (code <= 1114111) {
  51. arr[arr.length] = (code >>> 18) + 0xF0;
  52. arr[arr.length] = (code >>> 12 & 0x3F) | 0x80;
  53. arr[arr.length] = (code >>> 6 & 0x3F) | 0x80;
  54. arr[arr.length] = (code & 0x3F) | 0x80;
  55. } else {
  56. throw 'Unicode standart supports code points up-to U+10FFFF';
  57. }
  58. }
  59. return arr;
  60. };
  61. /**
  62. * Outputs 32 integer bits of a number in hex format.
  63. * Preserves leading zeros.
  64. * @param {Number} num
  65. */
  66. S.toHex32 = function (num) {
  67. // if negative
  68. if (num & 0x80000000) {
  69. // convert to positive number
  70. num = num & (~0x80000000);
  71. num += Math.pow(2, 31);
  72. }
  73. var str = num.toString(16);
  74. while (str.length < 8) {
  75. str = '0' + str;
  76. }
  77. return str;
  78. };
  79. /**
  80. * Changes the order of 4 bytes in integer representation of number.
  81. * From 1234 to 4321.
  82. * @param {Number} num Only 32 int bits are used.
  83. */
  84. S.reverseBytes = function (num) {
  85. var res = 0;
  86. res += ((num >>> 24) & 0xff);
  87. res += ((num >>> 16) & 0xff) << 8;
  88. res += ((num >>> 8) & 0xff) << 16;
  89. res += (num & 0xff) << 24;
  90. return res;
  91. };
  92. S.leftRotate = function (x, c) {
  93. return (x << c) | (x >>> (32 - c));
  94. };
  95. /**
  96. * RSA Data Security, Inc. MD5 Message-Digest Algorithm
  97. * http://tools.ietf.org/html/rfc1321
  98. * http://en.wikipedia.org/wiki/MD5
  99. * @param {String} message
  100. */
  101. S.md5 = function (message) {
  102. // r specifies the per-round shift amounts
  103. var r = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21];
  104. // Use binary integer part of the sines of integers (Radians) as constants:
  105. var k = [];
  106. for (var i = 0; i <= 63; i++) {
  107. k[i] = (Math.abs(Math.sin(i + 1)) * Math.pow(2, 32)) << 0;
  108. }
  109. var h0 = 0x67452301,
  110. h1 = 0xEFCDAB89,
  111. h2 = 0x98BADCFE,
  112. h3 = 0x10325476,
  113. bytes, unpadded;
  114. //Pre-processing:
  115. bytes = S.toUtf8ByteArr(message);
  116. message = null;
  117. unpadded = bytes.length;
  118. //append "1" bit to message
  119. //append "0" bits until message length in bits ≡ 448 (mod 512)
  120. bytes.push(0x80);
  121. var zeroBytes = Math.abs(448 - (bytes.length * 8) % 512) / 8;
  122. while (zeroBytes--) {
  123. bytes.push(0);
  124. }
  125. //append bit length of unpadded message as 64-bit little-endian integer to message
  126. bytes.push(unpadded * 8 & 0xff, unpadded * 8 >> 8 & 0xff, unpadded * 8 >> 16 & 0xff, unpadded * 8 >> 24 & 0xff);
  127. var i = 4;
  128. while (i--) {
  129. bytes.push(0);
  130. }
  131. var leftRotate = S.leftRotate;
  132. //Process the message in successive 512-bit chunks:
  133. var i = 0,
  134. w = [];
  135. while (i < bytes.length) {
  136. //break chunk into sixteen 32-bit words w[i], 0 ≤ i ≤ 15
  137. for (var j = 0; j <= 15; j++) {
  138. w[j] = (bytes[i + 4 * j] << 0) + (bytes[i + 4 * j + 1] << 8) + (bytes[i + 4 * j + 2] << 16) + (bytes[i + 4 * j + 3] << 24);
  139. }
  140. //Initialize hash value for this chunk:
  141. var a = h0,
  142. b = h1,
  143. c = h2,
  144. d = h3,
  145. f, g;
  146. //Main loop:
  147. for (var j = 0; j <= 63; j++) {
  148. if (j <= 15) {
  149. f = (b & c) | ((~b) & d);
  150. g = j;
  151. } else if (j <= 31) {
  152. f = (d & b) | ((~d) & c);
  153. g = (5 * j + 1) % 16;
  154. } else if (j <= 47) {
  155. f = b ^ c ^ d;
  156. g = (3 * j + 5) % 16;
  157. } else {
  158. f = c ^ (b | (~d));
  159. g = (7 * j) % 16;
  160. }
  161. var temp = d;
  162. d = c;
  163. c = b;
  164. b = b + leftRotate((a + f + k[j] + w[g]), r[j]);
  165. a = temp;
  166. }
  167. //Add this chunk's hash to result so far:
  168. h0 = (h0 + a) << 0;
  169. h1 = (h1 + b) << 0;
  170. h2 = (h2 + c) << 0;
  171. h3 = (h3 + d) << 0;
  172. i += 512 / 8;
  173. }
  174. // fix when starting with 0
  175. var res = out(h0) + out(h1) + out(h2) + out(h3);
  176. function out (h) {
  177. return S.toHex32(S.reverseBytes(h));
  178. }
  179. return res;
  180. };
  181. })();
  182. /*
  183. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  184. * Digest Algorithm, as defined in RFC 1321.
  185. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  186. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  187. * Distributed under the BSD License
  188. * See http://pajhome.org.uk/crypt/md5 for more info.
  189. */
  190. /*
  191. * Configurable variables. You may need to tweak these to be compatible with
  192. * the server-side, but the defaults work in most cases.
  193. */
  194. var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  195. var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
  196. /*
  197. * These are the functions you'll usually want to call
  198. * They take string arguments and return either hex or base-64 encoded strings
  199. */
  200. function hex_md5 (s) {
  201. return rstr2hex(rstr_md5(str2rstr_utf8(s)));
  202. }
  203. function b64_md5 (s) {
  204. return rstr2b64(rstr_md5(str2rstr_utf8(s)));
  205. }
  206. function any_md5 (s, e) {
  207. return rstr2any(rstr_md5(str2rstr_utf8(s)), e);
  208. }
  209. function hex_hmac_md5 (k, d) {
  210. return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)));
  211. }
  212. function b64_hmac_md5 (k, d) {
  213. return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)));
  214. }
  215. function any_hmac_md5 (k, d, e) {
  216. return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e);
  217. }
  218. /*
  219. * Perform a simple self-test to see if the VM is working
  220. */
  221. function md5_vm_test () {
  222. return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
  223. }
  224. /*
  225. * Calculate the MD5 of a raw string
  226. */
  227. function rstr_md5 (s) {
  228. return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
  229. }
  230. /*
  231. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  232. */
  233. function rstr_hmac_md5 (key, data) {
  234. var bkey = rstr2binl(key);
  235. if (bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);
  236. var ipad = Array(16),
  237. opad = Array(16);
  238. for (var i = 0; i < 16; i++) {
  239. ipad[i] = bkey[i] ^ 0x36363636;
  240. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  241. }
  242. var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  243. return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
  244. }
  245. /*
  246. * Convert a raw string to a hex string
  247. */
  248. function rstr2hex (input) {
  249. try {
  250. hexcase
  251. } catch (e) {
  252. hexcase = 0;
  253. }
  254. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  255. var output = "";
  256. var x;
  257. for (var i = 0; i < input.length; i++) {
  258. x = input.charCodeAt(i);
  259. output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt(x & 0x0F);
  260. }
  261. return output;
  262. }
  263. /*
  264. * Convert a raw string to a base-64 string
  265. */
  266. function rstr2b64 (input) {
  267. try {
  268. b64pad
  269. } catch (e) {
  270. b64pad = '';
  271. }
  272. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  273. var output = "";
  274. var len = input.length;
  275. for (var i = 0; i < len; i += 3) {
  276. var triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i + 2) : 0);
  277. for (var j = 0; j < 4; j++) {
  278. if (i * 8 + j * 6 > input.length * 8) output += b64pad;
  279. else output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
  280. }
  281. }
  282. return output;
  283. }
  284. /*
  285. * Convert a raw string to an arbitrary string encoding
  286. */
  287. function rstr2any (input, encoding) {
  288. var divisor = encoding.length;
  289. var i, j, q, x, quotient;
  290. /* Convert to an array of 16-bit big-endian values, forming the dividend */
  291. var dividend = Array(Math.ceil(input.length / 2));
  292. for (i = 0; i < dividend.length; i++) {
  293. dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  294. }
  295. /*
  296. * Repeatedly perform a long division. The binary array forms the dividend,
  297. * the length of the encoding is the divisor. Once computed, the quotient
  298. * forms the dividend for the next step. All remainders are stored for later
  299. * use.
  300. */
  301. var full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2)));
  302. var remainders = Array(full_length);
  303. for (j = 0; j < full_length; j++) {
  304. quotient = Array();
  305. x = 0;
  306. for (i = 0; i < dividend.length; i++) {
  307. x = (x << 16) + dividend[i];
  308. q = Math.floor(x / divisor);
  309. x -= q * divisor;
  310. if (quotient.length > 0 || q > 0) quotient[quotient.length] = q;
  311. }
  312. remainders[j] = x;
  313. dividend = quotient;
  314. }
  315. /* Convert the remainders to the output string */
  316. var output = "";
  317. for (i = remainders.length - 1; i >= 0; i--)
  318. output += encoding.charAt(remainders[i]);
  319. return output;
  320. }
  321. /*
  322. * Encode a string as utf-8.
  323. * For efficiency, this assumes the input is valid utf-16.
  324. */
  325. function str2rstr_utf8 (input) {
  326. return unescape(encodeURI(input));
  327. }
  328. /*
  329. * Encode a string as utf-16
  330. */
  331. function str2rstr_utf16le (input) {
  332. var output = "";
  333. for (var i = 0; i < input.length; i++)
  334. output += String.fromCharCode(input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF);
  335. return output;
  336. }
  337. function str2rstr_utf16be (input) {
  338. var output = "";
  339. for (var i = 0; i < input.length; i++)
  340. output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF);
  341. return output;
  342. }
  343. /*
  344. * Convert a raw string to an array of little-endian words
  345. * Characters >255 have their high-byte silently ignored.
  346. */
  347. function rstr2binl (input) {
  348. var output = Array(input.length >> 2);
  349. for (var i = 0; i < output.length; i++)
  350. output[i] = 0;
  351. for (var i = 0; i < input.length * 8; i += 8)
  352. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
  353. return output;
  354. }
  355. /*
  356. * Convert an array of little-endian words to a string
  357. */
  358. function binl2rstr (input) {
  359. var output = "";
  360. for (var i = 0; i < input.length * 32; i += 8)
  361. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
  362. return output;
  363. }
  364. /*
  365. * Calculate the MD5 of an array of little-endian words, and a bit length.
  366. */
  367. function binl_md5 (x, len) { /* append padding */
  368. x[len >> 5] |= 0x80 << ((len) % 32);
  369. x[(((len + 64) >>> 9) << 4) + 14] = len;
  370. var a = 1732584193;
  371. var b = -271733879;
  372. var c = -1732584194;
  373. var d = 271733878;
  374. for (var i = 0; i < x.length; i += 16) {
  375. var olda = a;
  376. var oldb = b;
  377. var oldc = c;
  378. var oldd = d;
  379. a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
  380. d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
  381. c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
  382. b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
  383. a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
  384. d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
  385. c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
  386. b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
  387. a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
  388. d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
  389. c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
  390. b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
  391. a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
  392. d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
  393. c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
  394. b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
  395. a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
  396. d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
  397. c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
  398. b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
  399. a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
  400. d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
  401. c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
  402. b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
  403. a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
  404. d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
  405. c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
  406. b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
  407. a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
  408. d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
  409. c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
  410. b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
  411. a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
  412. d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
  413. c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
  414. b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
  415. a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
  416. d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
  417. c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
  418. b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
  419. a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
  420. d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
  421. c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
  422. b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
  423. a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
  424. d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
  425. c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
  426. b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
  427. a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
  428. d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
  429. c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
  430. b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
  431. a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
  432. d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
  433. c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
  434. b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
  435. a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
  436. d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
  437. c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
  438. b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
  439. a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
  440. d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
  441. c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
  442. b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
  443. a = safe_add(a, olda);
  444. b = safe_add(b, oldb);
  445. c = safe_add(c, oldc);
  446. d = safe_add(d, oldd);
  447. }
  448. return Array(a, b, c, d);
  449. }
  450. /*
  451. * These functions implement the four basic operations the algorithm uses.
  452. */
  453. function md5_cmn (q, a, b, x, s, t) {
  454. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
  455. }
  456. function md5_ff (a, b, c, d, x, s, t) {
  457. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  458. }
  459. function md5_gg (a, b, c, d, x, s, t) {
  460. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  461. }
  462. function md5_hh (a, b, c, d, x, s, t) {
  463. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  464. }
  465. function md5_ii (a, b, c, d, x, s, t) {
  466. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  467. }
  468. /*
  469. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  470. * to work around bugs in some JS interpreters.
  471. */
  472. function safe_add (x, y) {
  473. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  474. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  475. return (msw << 16) | (lsw & 0xFFFF);
  476. }
  477. /*
  478. * Bitwise rotate a 32-bit number to the left.
  479. */
  480. function bit_rol (num, cnt) {
  481. return (num << cnt) | (num >>> (32 - cnt));
  482. }
  483. function md5cycle (x, k) {
  484. var a = x[0],
  485. b = x[1],
  486. c = x[2],
  487. d = x[3];
  488. a = ff(a, b, c, d, k[0], 7, -680876936);
  489. d = ff(d, a, b, c, k[1], 12, -389564586);
  490. c = ff(c, d, a, b, k[2], 17, 606105819);
  491. b = ff(b, c, d, a, k[3], 22, -1044525330);
  492. a = ff(a, b, c, d, k[4], 7, -176418897);
  493. d = ff(d, a, b, c, k[5], 12, 1200080426);
  494. c = ff(c, d, a, b, k[6], 17, -1473231341);
  495. b = ff(b, c, d, a, k[7], 22, -45705983);
  496. a = ff(a, b, c, d, k[8], 7, 1770035416);
  497. d = ff(d, a, b, c, k[9], 12, -1958414417);
  498. c = ff(c, d, a, b, k[10], 17, -42063);
  499. b = ff(b, c, d, a, k[11], 22, -1990404162);
  500. a = ff(a, b, c, d, k[12], 7, 1804603682);
  501. d = ff(d, a, b, c, k[13], 12, -40341101);
  502. c = ff(c, d, a, b, k[14], 17, -1502002290);
  503. b = ff(b, c, d, a, k[15], 22, 1236535329);
  504. a = gg(a, b, c, d, k[1], 5, -165796510);
  505. d = gg(d, a, b, c, k[6], 9, -1069501632);
  506. c = gg(c, d, a, b, k[11], 14, 643717713);
  507. b = gg(b, c, d, a, k[0], 20, -373897302);
  508. a = gg(a, b, c, d, k[5], 5, -701558691);
  509. d = gg(d, a, b, c, k[10], 9, 38016083);
  510. c = gg(c, d, a, b, k[15], 14, -660478335);
  511. b = gg(b, c, d, a, k[4], 20, -405537848);
  512. a = gg(a, b, c, d, k[9], 5, 568446438);
  513. d = gg(d, a, b, c, k[14], 9, -1019803690);
  514. c = gg(c, d, a, b, k[3], 14, -187363961);
  515. b = gg(b, c, d, a, k[8], 20, 1163531501);
  516. a = gg(a, b, c, d, k[13], 5, -1444681467);
  517. d = gg(d, a, b, c, k[2], 9, -51403784);
  518. c = gg(c, d, a, b, k[7], 14, 1735328473);
  519. b = gg(b, c, d, a, k[12], 20, -1926607734);
  520. a = hh(a, b, c, d, k[5], 4, -378558);
  521. d = hh(d, a, b, c, k[8], 11, -2022574463);
  522. c = hh(c, d, a, b, k[11], 16, 1839030562);
  523. b = hh(b, c, d, a, k[14], 23, -35309556);
  524. a = hh(a, b, c, d, k[1], 4, -1530992060);
  525. d = hh(d, a, b, c, k[4], 11, 1272893353);
  526. c = hh(c, d, a, b, k[7], 16, -155497632);
  527. b = hh(b, c, d, a, k[10], 23, -1094730640);
  528. a = hh(a, b, c, d, k[13], 4, 681279174);
  529. d = hh(d, a, b, c, k[0], 11, -358537222);
  530. c = hh(c, d, a, b, k[3], 16, -722521979);
  531. b = hh(b, c, d, a, k[6], 23, 76029189);
  532. a = hh(a, b, c, d, k[9], 4, -640364487);
  533. d = hh(d, a, b, c, k[12], 11, -421815835);
  534. c = hh(c, d, a, b, k[15], 16, 530742520);
  535. b = hh(b, c, d, a, k[2], 23, -995338651);
  536. a = ii(a, b, c, d, k[0], 6, -198630844);
  537. d = ii(d, a, b, c, k[7], 10, 1126891415);
  538. c = ii(c, d, a, b, k[14], 15, -1416354905);
  539. b = ii(b, c, d, a, k[5], 21, -57434055);
  540. a = ii(a, b, c, d, k[12], 6, 1700485571);
  541. d = ii(d, a, b, c, k[3], 10, -1894986606);
  542. c = ii(c, d, a, b, k[10], 15, -1051523);
  543. b = ii(b, c, d, a, k[1], 21, -2054922799);
  544. a = ii(a, b, c, d, k[8], 6, 1873313359);
  545. d = ii(d, a, b, c, k[15], 10, -30611744);
  546. c = ii(c, d, a, b, k[6], 15, -1560198380);
  547. b = ii(b, c, d, a, k[13], 21, 1309151649);
  548. a = ii(a, b, c, d, k[4], 6, -145523070);
  549. d = ii(d, a, b, c, k[11], 10, -1120210379);
  550. c = ii(c, d, a, b, k[2], 15, 718787259);
  551. b = ii(b, c, d, a, k[9], 21, -343485551);
  552. x[0] = add32(a, x[0]);
  553. x[1] = add32(b, x[1]);
  554. x[2] = add32(c, x[2]);
  555. x[3] = add32(d, x[3]);
  556. }
  557. function cmn (q, a, b, x, s, t) {
  558. a = add32(add32(a, q), add32(x, t));
  559. return add32((a << s) | (a >>> (32 - s)), b);
  560. }
  561. function ff (a, b, c, d, x, s, t) {
  562. return cmn((b & c) | ((~b) & d), a, b, x, s, t);
  563. }
  564. function gg (a, b, c, d, x, s, t) {
  565. return cmn((b & d) | (c & (~d)), a, b, x, s, t);
  566. }
  567. function hh (a, b, c, d, x, s, t) {
  568. return cmn(b ^ c ^ d, a, b, x, s, t);
  569. }
  570. function ii (a, b, c, d, x, s, t) {
  571. return cmn(c ^ (b | (~d)), a, b, x, s, t);
  572. }
  573. function md51 (s) {
  574. var txt = '';
  575. var n = s.length,
  576. state = [1732584193, -271733879, -1732584194, 271733878],
  577. i;
  578. for (i = 64; i <= s.length; i += 64) {
  579. md5cycle(state, md5blk(s.substring(i - 64, i)));
  580. }
  581. s = s.substring(i - 64);
  582. var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  583. for (i = 0; i < s.length; i++)
  584. tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3);
  585. tail[i >> 2] |= 0x80 << ((i % 4) << 3);
  586. if (i > 55) {
  587. md5cycle(state, tail);
  588. for (i = 0; i < 16; i++) tail[i] = 0;
  589. }
  590. tail[14] = n * 8;
  591. md5cycle(state, tail);
  592. return state;
  593. }
  594. /* there needs to be support for Unicode here,
  595. * unless we pretend that we can redefine the MD-5
  596. * algorithm for multi-byte characters (perhaps
  597. * by adding every four 16-bit characters and
  598. * shortening the sum to 32 bits). Otherwise
  599. * I suggest performing MD-5 as if every character
  600. * was two bytes--e.g., 0040 0025 = @%--but then
  601. * how will an ordinary MD-5 sum be matched?
  602. * There is no way to standardize text to something
  603. * like UTF-8 before transformation; speed cost is
  604. * utterly prohibitive. The JavaScript standard
  605. * itself needs to look at this: it should start
  606. * providing access to strings as preformed UTF-8
  607. * 8-bit unsigned value arrays.
  608. */
  609. function md5blk (s) { /* I figured global was faster. */
  610. var md5blks = [],
  611. i; /* Andy King said do it this way. */
  612. for (i = 0; i < 64; i += 4) {
  613. md5blks[i >> 2] = s.charCodeAt(i) + (s.charCodeAt(i + 1) << 8) + (s.charCodeAt(i + 2) << 16) + (s.charCodeAt(i + 3) << 24);
  614. }
  615. return md5blks;
  616. }
  617. var hex_chr = '0123456789abcdef'.split('');
  618. function rhex (n) {
  619. var s = '',
  620. j = 0;
  621. for (; j < 4; j++)
  622. s += hex_chr[(n >> (j * 8 + 4)) & 0x0F] + hex_chr[(n >> (j * 8)) & 0x0F];
  623. return s;
  624. }
  625. function hex (x) {
  626. for (var i = 0; i < x.length; i++)
  627. x[i] = rhex(x[i]);
  628. return x.join('');
  629. }
  630. function md5 (s) {
  631. return hex(md51(s));
  632. }
  633. /* this function is much faster,
  634. so if possible we use it. Some IEs
  635. are the only ones I know of that
  636. need the idiotic second function,
  637. generated by an if clause. */
  638. function add32 (a, b) {
  639. return (a + b) & 0xFFFFFFFF;
  640. }
  641. if (md5('hello') != '5d41402abc4b2a76b9719d911017c592') {
  642. function add32 (x, y) {
  643. var lsw = (x & 0xFFFF) + (y & 0xFFFF),
  644. msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  645. return (msw << 16) | (lsw & 0xFFFF);
  646. }
  647. }
  648. (function () {
  649. function md5cycle (x, k) {
  650. var a = x[0],
  651. b = x[1],
  652. c = x[2],
  653. d = x[3];
  654. a = ff(a, b, c, d, k[0], 7, -680876936);
  655. d = ff(d, a, b, c, k[1], 12, -389564586);
  656. c = ff(c, d, a, b, k[2], 17, 606105819);
  657. b = ff(b, c, d, a, k[3], 22, -1044525330);
  658. a = ff(a, b, c, d, k[4], 7, -176418897);
  659. d = ff(d, a, b, c, k[5], 12, 1200080426);
  660. c = ff(c, d, a, b, k[6], 17, -1473231341);
  661. b = ff(b, c, d, a, k[7], 22, -45705983);
  662. a = ff(a, b, c, d, k[8], 7, 1770035416);
  663. d = ff(d, a, b, c, k[9], 12, -1958414417);
  664. c = ff(c, d, a, b, k[10], 17, -42063);
  665. b = ff(b, c, d, a, k[11], 22, -1990404162);
  666. a = ff(a, b, c, d, k[12], 7, 1804603682);
  667. d = ff(d, a, b, c, k[13], 12, -40341101);
  668. c = ff(c, d, a, b, k[14], 17, -1502002290);
  669. b = ff(b, c, d, a, k[15], 22, 1236535329);
  670. a = gg(a, b, c, d, k[1], 5, -165796510);
  671. d = gg(d, a, b, c, k[6], 9, -1069501632);
  672. c = gg(c, d, a, b, k[11], 14, 643717713);
  673. b = gg(b, c, d, a, k[0], 20, -373897302);
  674. a = gg(a, b, c, d, k[5], 5, -701558691);
  675. d = gg(d, a, b, c, k[10], 9, 38016083);
  676. c = gg(c, d, a, b, k[15], 14, -660478335);
  677. b = gg(b, c, d, a, k[4], 20, -405537848);
  678. a = gg(a, b, c, d, k[9], 5, 568446438);
  679. d = gg(d, a, b, c, k[14], 9, -1019803690);
  680. c = gg(c, d, a, b, k[3], 14, -187363961);
  681. b = gg(b, c, d, a, k[8], 20, 1163531501);
  682. a = gg(a, b, c, d, k[13], 5, -1444681467);
  683. d = gg(d, a, b, c, k[2], 9, -51403784);
  684. c = gg(c, d, a, b, k[7], 14, 1735328473);
  685. b = gg(b, c, d, a, k[12], 20, -1926607734);
  686. a = hh(a, b, c, d, k[5], 4, -378558);
  687. d = hh(d, a, b, c, k[8], 11, -2022574463);
  688. c = hh(c, d, a, b, k[11], 16, 1839030562);
  689. b = hh(b, c, d, a, k[14], 23, -35309556);
  690. a = hh(a, b, c, d, k[1], 4, -1530992060);
  691. d = hh(d, a, b, c, k[4], 11, 1272893353);
  692. c = hh(c, d, a, b, k[7], 16, -155497632);
  693. b = hh(b, c, d, a, k[10], 23, -1094730640);
  694. a = hh(a, b, c, d, k[13], 4, 681279174);
  695. d = hh(d, a, b, c, k[0], 11, -358537222);
  696. c = hh(c, d, a, b, k[3], 16, -722521979);
  697. b = hh(b, c, d, a, k[6], 23, 76029189);
  698. a = hh(a, b, c, d, k[9], 4, -640364487);
  699. d = hh(d, a, b, c, k[12], 11, -421815835);
  700. c = hh(c, d, a, b, k[15], 16, 530742520);
  701. b = hh(b, c, d, a, k[2], 23, -995338651);
  702. a = ii(a, b, c, d, k[0], 6, -198630844);
  703. d = ii(d, a, b, c, k[7], 10, 1126891415);
  704. c = ii(c, d, a, b, k[14], 15, -1416354905);
  705. b = ii(b, c, d, a, k[5], 21, -57434055);
  706. a = ii(a, b, c, d, k[12], 6, 1700485571);
  707. d = ii(d, a, b, c, k[3], 10, -1894986606);
  708. c = ii(c, d, a, b, k[10], 15, -1051523);
  709. b = ii(b, c, d, a, k[1], 21, -2054922799);
  710. a = ii(a, b, c, d, k[8], 6, 1873313359);
  711. d = ii(d, a, b, c, k[15], 10, -30611744);
  712. c = ii(c, d, a, b, k[6], 15, -1560198380);
  713. b = ii(b, c, d, a, k[13], 21, 1309151649);
  714. a = ii(a, b, c, d, k[4], 6, -145523070);
  715. d = ii(d, a, b, c, k[11], 10, -1120210379);
  716. c = ii(c, d, a, b, k[2], 15, 718787259);
  717. b = ii(b, c, d, a, k[9], 21, -343485551);
  718. x[0] = add32(a, x[0]);
  719. x[1] = add32(b, x[1]);
  720. x[2] = add32(c, x[2]);
  721. x[3] = add32(d, x[3]);
  722. }
  723. function cmn (q, a, b, x, s, t) {
  724. a = add32(add32(a, q), add32(x, t));
  725. return add32((a << s) | (a >>> (32 - s)), b);
  726. }
  727. function ff (a, b, c, d, x, s, t) {
  728. return cmn((b & c) | ((~b) & d), a, b, x, s, t);
  729. }
  730. function gg (a, b, c, d, x, s, t) {
  731. return cmn((b & d) | (c & (~d)), a, b, x, s, t);
  732. }
  733. function hh (a, b, c, d, x, s, t) {
  734. return cmn(b ^ c ^ d, a, b, x, s, t);
  735. }
  736. function ii (a, b, c, d, x, s, t) {
  737. return cmn(c ^ (b | (~d)), a, b, x, s, t);
  738. }
  739. function md51 (s) {
  740. // Converts the string to UTF-8 "bytes" when necessary
  741. if (s.match(/[\x80-\xFF]/)) {
  742. s = unescape(encodeURI(s));
  743. }
  744. var txt = '';
  745. var n = s.length,
  746. state = [1732584193, -271733879, -1732584194, 271733878],
  747. i;
  748. for (i = 64; i <= s.length; i += 64) {
  749. md5cycle(state, md5blk(s.substring(i - 64, i)));
  750. }
  751. s = s.substring(i - 64);
  752. var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  753. for (i = 0; i < s.length; i++)
  754. tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3);
  755. tail[i >> 2] |= 0x80 << ((i % 4) << 3);
  756. if (i > 55) {
  757. md5cycle(state, tail);
  758. for (i = 0; i < 16; i++) tail[i] = 0;
  759. }
  760. tail[14] = n * 8;
  761. md5cycle(state, tail);
  762. return state;
  763. }
  764. /* there needs to be support for Unicode here,
  765. * unless we pretend that we can redefine the MD-5
  766. * algorithm for multi-byte characters (perhaps
  767. * by adding every four 16-bit characters and
  768. * shortening the sum to 32 bits). Otherwise
  769. * I suggest performing MD-5 as if every character
  770. * was two bytes--e.g., 0040 0025 = @%--but then
  771. * how will an ordinary MD-5 sum be matched?
  772. * There is no way to standardize text to something
  773. * like UTF-8 before transformation; speed cost is
  774. * utterly prohibitive. The JavaScript standard
  775. * itself needs to look at this: it should start
  776. * providing access to strings as preformed UTF-8
  777. * 8-bit unsigned value arrays.
  778. */
  779. function md5blk (s) { /* I figured global was faster. */
  780. var md5blks = [],
  781. i; /* Andy King said do it this way. */
  782. for (i = 0; i < 64; i += 4) {
  783. md5blks[i >> 2] = s.charCodeAt(i) + (s.charCodeAt(i + 1) << 8) + (s.charCodeAt(i + 2) << 16) + (s.charCodeAt(i + 3) << 24);
  784. }
  785. return md5blks;
  786. }
  787. var hex_chr = '0123456789abcdef'.split('');
  788. function rhex (n) {
  789. var s = '',
  790. j = 0;
  791. for (; j < 4; j++)
  792. s += hex_chr[(n >> (j * 8 + 4)) & 0x0F] + hex_chr[(n >> (j * 8)) & 0x0F];
  793. return s;
  794. }
  795. function hex (x) {
  796. for (var i = 0; i < x.length; i++)
  797. x[i] = rhex(x[i]);
  798. return x.join('');
  799. }
  800. var md5_utf8 = function (s) {
  801. return hex(md51(s));
  802. }
  803. /* this function is much faster,
  804. so if possible we use it. Some IEs
  805. are the only ones I know of that
  806. need the idiotic second function,
  807. generated by an if clause. */
  808. function add32 (a, b) {
  809. return (a + b) & 0xFFFFFFFF;
  810. }
  811. if (md5('hello') != '5d41402abc4b2a76b9719d911017c592') {
  812. function add32 (x, y) {
  813. var lsw = (x & 0xFFFF) + (y & 0xFFFF),
  814. msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  815. return (msw << 16) | (lsw & 0xFFFF);
  816. }
  817. }
  818. })();
  819. /* md5.js - MD5 Message-Digest
  820. * Copyright (C) 1999,2002 Masanao Izumo <iz@onicos.co.jp>
  821. * Version: 2.0.0
  822. * LastModified: May 13 2002
  823. *
  824. * This program is free software. You can redistribute it and/or modify
  825. * it without any warranty. This library calculates the MD5 based on RFC1321.
  826. * See RFC1321 for more information and algorism.
  827. */
  828. /* Interface:
  829. * md5_128bits = MD5_hash(data);
  830. * md5_hexstr = MD5_hexhash(data);
  831. */
  832. /* ChangeLog
  833. * 2002/05/13: Version 2.0.0 released
  834. * NOTICE: API is changed.
  835. * 2002/04/15: Bug fix about MD5 length.
  836. */
  837. // md5_T[i] = parseInt(Math.abs(Math.sin(i)) * 4294967296.0);
  838. var MD5_T = new Array(0x00000000, 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391);
  839. var MD5_round1 = new Array(new Array(0, 7, 1), new Array(1, 12, 2), new Array(2, 17, 3), new Array(3, 22, 4), new Array(4, 7, 5), new Array(5, 12, 6), new Array(6, 17, 7), new Array(7, 22, 8), new Array(8, 7, 9), new Array(9, 12, 10), new Array(10, 17, 11), new Array(11, 22, 12), new Array(12, 7, 13), new Array(13, 12, 14), new Array(14, 17, 15), new Array(15, 22, 16));
  840. var MD5_round2 = new Array(new Array(1, 5, 17), new Array(6, 9, 18), new Array(11, 14, 19), new Array(0, 20, 20), new Array(5, 5, 21), new Array(10, 9, 22), new Array(15, 14, 23), new Array(4, 20, 24), new Array(9, 5, 25), new Array(14, 9, 26), new Array(3, 14, 27), new Array(8, 20, 28), new Array(13, 5, 29), new Array(2, 9, 30), new Array(7, 14, 31), new Array(12, 20, 32));
  841. var MD5_round3 = new Array(new Array(5, 4, 33), new Array(8, 11, 34), new Array(11, 16, 35), new Array(14, 23, 36), new Array(1, 4, 37), new Array(4, 11, 38), new Array(7, 16, 39), new Array(10, 23, 40), new Array(13, 4, 41), new Array(0, 11, 42), new Array(3, 16, 43), new Array(6, 23, 44), new Array(9, 4, 45), new Array(12, 11, 46), new Array(15, 16, 47), new Array(2, 23, 48));
  842. var MD5_round4 = new Array(new Array(0, 6, 49), new Array(7, 10, 50), new Array(14, 15, 51), new Array(5, 21, 52), new Array(12, 6, 53), new Array(3, 10, 54), new Array(10, 15, 55), new Array(1, 21, 56), new Array(8, 6, 57), new Array(15, 10, 58), new Array(6, 15, 59), new Array(13, 21, 60), new Array(4, 6, 61), new Array(11, 10, 62), new Array(2, 15, 63), new Array(9, 21, 64));
  843. function MD5_F (x, y, z) {
  844. return (x & y) | (~x & z);
  845. }
  846. function MD5_G (x, y, z) {
  847. return (x & z) | (y & ~z);
  848. }
  849. function MD5_H (x, y, z) {
  850. return x ^ y ^ z;
  851. }
  852. function MD5_I (x, y, z) {
  853. return y ^ (x | ~z);
  854. }
  855. var MD5_round = new Array(new Array(MD5_F, MD5_round1), new Array(MD5_G, MD5_round2), new Array(MD5_H, MD5_round3), new Array(MD5_I, MD5_round4));
  856. function MD5_pack (n32) {
  857. return String.fromCharCode(n32 & 0xff) + String.fromCharCode((n32 >>> 8) & 0xff) + String.fromCharCode((n32 >>> 16) & 0xff) + String.fromCharCode((n32 >>> 24) & 0xff);
  858. }
  859. function MD5_unpack (s4) {
  860. return s4.charCodeAt(0) | (s4.charCodeAt(1) << 8) | (s4.charCodeAt(2) << 16) | (s4.charCodeAt(3) << 24);
  861. }
  862. function MD5_number (n) {
  863. while (n < 0)
  864. n += 4294967296;
  865. while (n > 4294967295)
  866. n -= 4294967296;
  867. return n;
  868. }
  869. function MD5_apply_round (x, s, f, abcd, r) {
  870. var a, b, c, d;
  871. var kk, ss, ii;
  872. var t, u;
  873. a = abcd[0];
  874. b = abcd[1];
  875. c = abcd[2];
  876. d = abcd[3];
  877. kk = r[0];
  878. ss = r[1];
  879. ii = r[2];
  880. u = f(s[b], s[c], s[d]);
  881. t = s[a] + u + x[kk] + MD5_T[ii];
  882. t = MD5_number(t);
  883. t = ((t << ss) | (t >>> (32 - ss)));
  884. t += s[b];
  885. s[a] = MD5_number(t);
  886. }
  887. function MD5_hash (data) {
  888. var abcd, x, state, s;
  889. var len, index, padLen, f, r;
  890. var i, j, k;
  891. var tmp;
  892. state = new Array(0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476);
  893. len = data.length;
  894. index = len & 0x3f;
  895. padLen = (index < 56) ? (56 - index) : (120 - index);
  896. if (padLen > 0) {
  897. data += "\x80";
  898. for (i = 0; i < padLen - 1; i++)
  899. data += "\x00";
  900. }
  901. data += MD5_pack(len * 8);
  902. data += MD5_pack(0);
  903. len += padLen + 8;
  904. abcd = new Array(0, 1, 2, 3);
  905. x = new Array(16);
  906. s = new Array(4);
  907. for (k = 0; k < len; k += 64) {
  908. for (i = 0, j = k; i < 16; i++, j += 4) {
  909. x[i] = data.charCodeAt(j) | (data.charCodeAt(j + 1) << 8) | (data.charCodeAt(j + 2) << 16) | (data.charCodeAt(j + 3) << 24);
  910. }
  911. for (i = 0; i < 4; i++)
  912. s[i] = state[i];
  913. for (i = 0; i < 4; i++) {
  914. f = MD5_round[i][0];
  915. r = MD5_round[i][1];
  916. for (j = 0; j < 16; j++) {
  917. MD5_apply_round(x, s, f, abcd, r[j]);
  918. tmp = abcd[0];
  919. abcd[0] = abcd[3];
  920. abcd[3] = abcd[2];
  921. abcd[2] = abcd[1];
  922. abcd[1] = tmp;
  923. }
  924. }
  925. for (i = 0; i < 4; i++) {
  926. state[i] += s[i];
  927. state[i] = MD5_number(state[i]);
  928. }
  929. }
  930. return MD5_pack(state[0]) + MD5_pack(state[1]) + MD5_pack(state[2]) + MD5_pack(state[3]);
  931. }
  932. function MD5_hexhash (data) {
  933. var i, out, c;
  934. var bit128;
  935. bit128 = MD5_hash(data);
  936. out = "";
  937. for (i = 0; i < 16; i++) {
  938. c = bit128.charCodeAt(i);
  939. out += "0123456789abcdef".charAt((c >> 4) & 0xf);
  940. out += "0123456789abcdef".charAt(c & 0xf);
  941. }
  942. return out;
  943. }
  944. module.exports = {
  945. hex_md5
  946. }