🏠 返回首頁 

Greasy Fork is available in English.

Portable MD5 Function

Usage: hex_md5 ('String');

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greasyfork.org/scripts/130/10066/Portable%20MD5%20Function.js

  1. // ==UserScript==
  2. // @name Portable MD5 Function
  3. // @version 0.1.2
  4. // @description Usage: hex_md5 ('String');
  5. // ==/UserScript==
  6. // Portable MD5 Function
  7. // Usage : md5 (<string> SourceText)
  8. // Return: Lower-case MD5.
  9. // Source: http://pajhome.org.uk/crypt/md5/md5.html
  10. /*
  11. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  12. * Digest Algorithm, as defined in RFC 1321.
  13. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  14. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  15. * Distributed under the BSD License
  16. * See http://pajhome.org.uk/crypt/md5 for more info.
  17. */
  18. var md5 = (function () {
  19. /*
  20. * These are the functions you'll usually want to call
  21. * They take string arguments and return either hex or base-64 encoded strings
  22. */
  23. var hex_md5 = function(s) { return rstr2hex(rstr_md5(str2rstr_utf8(s))); };
  24. var b64_md5 = function(s) { return rstr2b64(rstr_md5(str2rstr_utf8(s))); };
  25. var any_md5 = function(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); };
  26. var hex_hmac_md5 = function(k, d)
  27. { return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); };
  28. var b64_hmac_md5 = function(k, d)
  29. { return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); };
  30. var any_hmac_md5 = function(k, d, e)
  31. { return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); };
  32. /*
  33. * Perform a simple self-test to see if the VM is working
  34. */
  35. var md5_vm_test = function()
  36. {
  37. return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
  38. };
  39. /*
  40. * Calculate the MD5 of a raw string
  41. */
  42. var rstr_md5 = function(s)
  43. {
  44. return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
  45. };
  46. /*
  47. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  48. */
  49. var rstr_hmac_md5 = function(key, data)
  50. {
  51. var bkey = rstr2binl(key);
  52. if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);
  53. var ipad = Array(16), opad = Array(16);
  54. for(var i = 0; i < 16; i++)
  55. {
  56. ipad[i] = bkey[i] ^ 0x36363636;
  57. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  58. }
  59. var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  60. return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
  61. };
  62. /*
  63. * Convert a raw string to a hex string
  64. */
  65. var rstr2hex = function(input)
  66. {
  67. var hex_tab = "0123456789abcdef";
  68. var output = "";
  69. var x;
  70. for(var i = 0; i < input.length; i++)
  71. {
  72. x = input.charCodeAt(i);
  73. output += hex_tab.charAt((x >>> 4) & 0x0F) +
  74. hex_tab.charAt( x & 0x0F);
  75. }
  76. return output;
  77. };
  78. /*
  79. * Convert a raw string to a base-64 string
  80. */
  81. var rstr2b64 = function(input)
  82. {
  83. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  84. var output = "";
  85. var len = input.length;
  86. for(var i = 0; i < len; i += 3)
  87. {
  88. var triplet = (input.charCodeAt(i) << 16)
  89. | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
  90. | (i + 2 < len ? input.charCodeAt(i+2) : 0);
  91. for(var j = 0; j < 4; j++)
  92. {
  93. if(i * 8 + j * 6 > input.length * 8) {}
  94. else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
  95. }
  96. }
  97. return output;
  98. };
  99. /*
  100. * Convert a raw string to an arbitrary string encoding
  101. */
  102. var rstr2any = function(input, encoding)
  103. {
  104. var divisor = encoding.length;
  105. var i, j, q, x, quotient;
  106. /* Convert to an array of 16-bit big-endian values, forming the dividend */
  107. var dividend = Array(Math.ceil(input.length / 2));
  108. for(i = 0; i < dividend.length; i++)
  109. {
  110. dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  111. }
  112. /*
  113. * Repeatedly perform a long division. The binary array forms the dividend,
  114. * the length of the encoding is the divisor. Once computed, the quotient
  115. * forms the dividend for the next step. All remainders are stored for later
  116. * use.
  117. */
  118. var full_length = Math.ceil(input.length * 8 /
  119. (Math.log(encoding.length) / Math.log(2)));
  120. var remainders = Array(full_length);
  121. for(j = 0; j < full_length; j++)
  122. {
  123. quotient = Array();
  124. x = 0;
  125. for(i = 0; i < dividend.length; i++)
  126. {
  127. x = (x << 16) + dividend[i];
  128. q = Math.floor(x / divisor);
  129. x -= q * divisor;
  130. if(quotient.length > 0 || q > 0)
  131. quotient[quotient.length] = q;
  132. }
  133. remainders[j] = x;
  134. dividend = quotient;
  135. }
  136. /* Convert the remainders to the output string */
  137. var output = "";
  138. for(i = remainders.length - 1; i >= 0; i--)
  139. output += encoding.charAt(remainders[i]);
  140. return output;
  141. };
  142. /*
  143. * Encode a string as utf-8.
  144. * For efficiency, this assumes the input is valid utf-16.
  145. */
  146. var str2rstr_utf8 = function(input)
  147. {
  148. var output = "";
  149. var i = -1;
  150. var x, y;
  151. while(++i < input.length)
  152. {
  153. /* Decode utf-16 surrogate pairs */
  154. x = input.charCodeAt(i);
  155. y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
  156. if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
  157. {
  158. x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  159. i++;
  160. }
  161. /* Encode output as utf-8 */
  162. if(x <= 0x7F)
  163. output += String.fromCharCode(x);
  164. else if(x <= 0x7FF)
  165. output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
  166. 0x80 | ( x & 0x3F));
  167. else if(x <= 0xFFFF)
  168. output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
  169. 0x80 | ((x >>> 6 ) & 0x3F),
  170. 0x80 | ( x & 0x3F));
  171. else if(x <= 0x1FFFFF)
  172. output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
  173. 0x80 | ((x >>> 12) & 0x3F),
  174. 0x80 | ((x >>> 6 ) & 0x3F),
  175. 0x80 | ( x & 0x3F));
  176. }
  177. return output;
  178. };
  179. /*
  180. * Encode a string as utf-16
  181. */
  182. var str2rstr_utf16le = function(input)
  183. {
  184. var output = "";
  185. for(var i = 0; i < input.length; i++)
  186. output += String.fromCharCode( input.charCodeAt(i) & 0xFF,
  187. (input.charCodeAt(i) >>> 8) & 0xFF);
  188. return output;
  189. };
  190. var str2rstr_utf16be = function(input)
  191. {
  192. var output = "";
  193. for(var i = 0; i < input.length; i++)
  194. output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
  195. input.charCodeAt(i) & 0xFF);
  196. return output;
  197. };
  198. /*
  199. * Convert a raw string to an array of little-endian words
  200. * Characters >255 have their high-byte silently ignored.
  201. */
  202. var rstr2binl = function(input)
  203. {
  204. var output = Array(input.length >> 2);
  205. for(var i = 0; i < output.length; i++)
  206. output[i] = 0;
  207. for(i = 0; i < input.length * 8; i += 8)
  208. output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);
  209. return output;
  210. };
  211. /*
  212. * Convert an array of little-endian words to a string
  213. */
  214. var binl2rstr = function(input)
  215. {
  216. var output = "";
  217. for(var i = 0; i < input.length * 32; i += 8)
  218. output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
  219. return output;
  220. };
  221. /*
  222. * Calculate the MD5 of an array of little-endian words, and a bit length.
  223. */
  224. var binl_md5 = function(x, len)
  225. {
  226. /* append padding */
  227. x[len >> 5] |= 0x80 << ((len) % 32);
  228. x[(((len + 64) >>> 9) << 4) + 14] = len;
  229. var a = 1732584193;
  230. var b = -271733879;
  231. var c = -1732584194;
  232. var d = 271733878;
  233. for(var i = 0; i < x.length; i += 16)
  234. {
  235. var olda = a;
  236. var oldb = b;
  237. var oldc = c;
  238. var oldd = d;
  239. a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  240. d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
  241. c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
  242. b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  243. a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  244. d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
  245. c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  246. b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
  247. a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
  248. d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  249. c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
  250. b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
  251. a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
  252. d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
  253. c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
  254. b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
  255. a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  256. d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  257. c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
  258. b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
  259. a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  260. d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
  261. c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
  262. b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
  263. a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
  264. d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
  265. c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
  266. b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
  267. a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
  268. d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  269. c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
  270. b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
  271. a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
  272. d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  273. c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
  274. b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
  275. a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  276. d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
  277. c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
  278. b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
  279. a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
  280. d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
  281. c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
  282. b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
  283. a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  284. d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
  285. c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
  286. b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
  287. a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  288. d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
  289. c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
  290. b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
  291. a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
  292. d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  293. c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
  294. b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  295. a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
  296. d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
  297. c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  298. b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
  299. a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  300. d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
  301. c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
  302. b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
  303. a = safe_add(a, olda);
  304. b = safe_add(b, oldb);
  305. c = safe_add(c, oldc);
  306. d = safe_add(d, oldd);
  307. }
  308. return Array(a, b, c, d);
  309. };
  310. /*
  311. * These functions implement the four basic operations the algorithm uses.
  312. */
  313. var md5_cmn = function(q, a, b, x, s, t)
  314. {
  315. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
  316. };
  317. var md5_ff = function(a, b, c, d, x, s, t)
  318. {
  319. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  320. };
  321. var md5_gg = function(a, b, c, d, x, s, t)
  322. {
  323. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  324. };
  325. var md5_hh = function(a, b, c, d, x, s, t)
  326. {
  327. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  328. };
  329. var md5_ii = function(a, b, c, d, x, s, t)
  330. {
  331. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  332. };
  333. /*
  334. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  335. * to work around bugs in some JS interpreters.
  336. */
  337. var safe_add = function(x, y)
  338. {
  339. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  340. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  341. return (msw << 16) | (lsw & 0xFFFF);
  342. };
  343. /*
  344. * Bitwise rotate a 32-bit number to the left.
  345. */
  346. var bit_rol = function(num, cnt) {
  347. return (num << cnt) | (num >>> (32 - cnt));
  348. };
  349. return hex_md5;
  350. })();
  351. var hex_md5 = md5;