md5

md5文件

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/441331/1026891/md5.js

  1. /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distributed under the BSD License * See http://pajhome.org.uk/crypt/md5 for more info. */
  2.  
  3. /* * Configurable variables. You may need to tweak these to be compatible with * the server-side, but the defaults work in most cases. */ var hexcase = 0;
  4. /* hex output format. 0 - lowercase; 1 - uppercase */ var b64pad = "";
  5. /* base-64 pad character. "=" for strict RFC compliance */ var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
  6.  
  7. /* * These are the functions you'll usually want to call * They take string arguments and return either hex or base-64 encoded strings */ function hex_md5(
  8. s
  9. ) {
  10. return binl2hex(core_md5(str2binl(s), s.length * chrsz));
  11. }
  12. function b64_md5(s) {
  13. return binl2b64(core_md5(str2binl(s), s.length * chrsz));
  14. }
  15. function str_md5(s) {
  16. return binl2str(core_md5(str2binl(s), s.length * chrsz));
  17. }
  18. function hex_hmac_md5(key, data) {
  19. return binl2hex(core_hmac_md5(key, data));
  20. }
  21. function b64_hmac_md5(key, data) {
  22. return binl2b64(core_hmac_md5(key, data));
  23. }
  24. function str_hmac_md5(key, data) {
  25. return binl2str(core_hmac_md5(key, data));
  26. }
  27.  
  28. /* * Perform a simple self-test to see if the VM is working */ function md5_vm_test() {
  29. return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
  30. }
  31.  
  32. /* * Calculate the MD5 of an array of little-endian words, and a bit length */ function core_md5(
  33. x,
  34. len
  35. ) {
  36. /* append padding */ x[len >> 5] |= 0x80 << len % 32;
  37. x[(((len + 64) >>> 9) << 4) + 14] = len;
  38.  
  39. var a = 1732584193;
  40. var b = -271733879;
  41. var c = -1732584194;
  42. var d = 271733878;
  43.  
  44. for (var i = 0; i < x.length; i += 16) {
  45. var olda = a;
  46. var oldb = b;
  47. var oldc = c;
  48. var oldd = d;
  49.  
  50. a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
  51. d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
  52. c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
  53. b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
  54. a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
  55. d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
  56. c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
  57. b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
  58. a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
  59. d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
  60. c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
  61. b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
  62. a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
  63. d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
  64. c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
  65. b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
  66.  
  67. a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
  68. d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
  69. c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
  70. b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
  71. a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
  72. d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
  73. c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
  74. b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
  75. a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
  76. d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
  77. c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
  78. b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
  79. a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
  80. d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
  81. c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
  82. b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
  83.  
  84. a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
  85. d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
  86. c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
  87. b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
  88. a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
  89. d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
  90. c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
  91. b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
  92. a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
  93. d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
  94. c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
  95. b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
  96. a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
  97. d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
  98. c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
  99. b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
  100.  
  101. a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
  102. d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
  103. c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
  104. b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
  105. a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
  106. d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
  107. c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
  108. b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
  109. a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
  110. d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
  111. c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
  112. b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
  113. a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
  114. d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
  115. c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
  116. b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
  117.  
  118. a = safe_add(a, olda);
  119. b = safe_add(b, oldb);
  120. c = safe_add(c, oldc);
  121. d = safe_add(d, oldd);
  122. }
  123. return Array(a, b, c, d);
  124. }
  125.  
  126. /* * These functions implement the four basic operations the algorithm uses. */ function md5_cmn(
  127. q,
  128. a,
  129. b,
  130. x,
  131. s,
  132. t
  133. ) {
  134. return safe_add(
  135. bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),
  136. b
  137. );
  138. }
  139. function md5_ff(a, b, c, d, x, s, t) {
  140. return md5_cmn((b & c) | (~b & d), a, b, x, s, t);
  141. }
  142. function md5_gg(a, b, c, d, x, s, t) {
  143. return md5_cmn((b & d) | (c & ~d), a, b, x, s, t);
  144. }
  145. function md5_hh(a, b, c, d, x, s, t) {
  146. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  147. }
  148. function md5_ii(a, b, c, d, x, s, t) {
  149. return md5_cmn(c ^ (b | ~d), a, b, x, s, t);
  150. }
  151.  
  152. /* * Calculate the HMAC-MD5, of a key and some data */ function core_hmac_md5(
  153. key,
  154. data
  155. ) {
  156. var bkey = str2binl(key);
  157. if (bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);
  158.  
  159. var ipad = Array(16),
  160. opad = Array(16);
  161. for (var i = 0; i < 16; i++) {
  162. ipad[i] = bkey[i] ^ 0x36363636;
  163. opad[i] = bkey[i] ^ 0x5c5c5c5c;
  164. }
  165.  
  166. var hash = core_md5(
  167. ipad.concat(str2binl(data)),
  168. 512 + data.length * chrsz
  169. );
  170. return core_md5(opad.concat(hash), 512 + 128);
  171. }
  172.  
  173. /* * Add integers, wrapping at 2^32. This uses 16-bit operations internally * to work around bugs in some JS interpreters. */ function safe_add(
  174. x,
  175. y
  176. ) {
  177. var lsw = (x & 0xffff) + (y & 0xffff);
  178. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  179. return (msw << 16) | (lsw & 0xffff);
  180. }
  181.  
  182. /* * Bitwise rotate a 32-bit number to the left. */ function bit_rol(
  183. num,
  184. cnt
  185. ) {
  186. return (num << cnt) | (num >>> (32 - cnt));
  187. }
  188.  
  189. /* * Convert a string to an array of little-endian words * If chrsz is ASCII, characters >255 have their hi-byte silently ignored. */ function str2binl(
  190. str
  191. ) {
  192. var bin = Array();
  193. var mask = (1 << chrsz) - 1;
  194. for (var i = 0; i < str.length * chrsz; i += chrsz)
  195. bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << i % 32;
  196. return bin;
  197. }
  198.  
  199. /* * Convert an array of little-endian words to a string */ function binl2str(
  200. bin
  201. ) {
  202. var str = "";
  203. var mask = (1 << chrsz) - 1;
  204. for (var i = 0; i < bin.length * 32; i += chrsz)
  205. str += String.fromCharCode((bin[i >> 5] >>> i % 32) & mask);
  206. return str;
  207. }
  208.  
  209. /* * Convert an array of little-endian words to a hex string. */ function binl2hex(
  210. binarray
  211. ) {
  212. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  213. var str = "";
  214. for (var i = 0; i < binarray.length * 4; i++) {
  215. str +=
  216. hex_tab.charAt(
  217. (binarray[i >> 2] >> ((i % 4) * 8 + 4)) & 0xf
  218. ) +
  219. hex_tab.charAt(
  220. (binarray[i >> 2] >> ((i % 4) * 8)) & 0xf
  221. );
  222. }
  223. return str;
  224. }
  225.  
  226. /* * Convert an array of little-endian words to a base-64 string */ function binl2b64(
  227. binarray
  228. ) {
  229. var tab =
  230. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  231. var str = "";
  232. for (var i = 0; i < binarray.length * 4; i += 3) {
  233. var triplet =
  234. (((binarray[i >> 2] >> (8 * (i % 4))) & 0xff) << 16) |
  235. (((binarray[(i + 1) >> 2] >> (8 * ((i + 1) % 4))) &
  236. 0xff) <<
  237. 8) |
  238. ((binarray[(i + 2) >> 2] >> (8 * ((i + 2) % 4))) &
  239. 0xff);
  240. for (var j = 0; j < 4; j++) {
  241. if (i * 8 + j * 6 > binarray.length * 32) str += b64pad;
  242. else
  243. str += tab.charAt(
  244. (triplet >> (6 * (3 - j))) & 0x3f
  245. );
  246. }
  247. }
  248. return str;
  249. }