Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * JavaScript MD5 1.0.1
  3. * https://github.com/blueimp/JavaScript-MD5
  4. *
  5. * Copyright 2011, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * http://www.opensource.org/licenses/MIT
  10. *
  11. * Based on
  12. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  13. * Digest Algorithm, as defined in RFC 1321.
  14. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  15. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  16. * Distributed under the BSD License
  17. * See http://pajhome.org.uk/crypt/md5 for more info.
  18. */
  19. /*jslint bitwise: true */
  20. /*global unescape, define */
  21. /*
  22. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  23. * to work around bugs in some JS interpreters.
  24. */
  25. function safe_add(x, y) {
  26. var lsw = (x & 0xFFFF) + (y & 0xFFFF),
  27. msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  28. return (msw << 16) | (lsw & 0xFFFF);
  29. }
  30. /*
  31. * Bitwise rotate a 32-bit number to the left.
  32. */
  33. function bit_rol(num, cnt) {
  34. return (num << cnt) | (num >>> (32 - cnt));
  35. }
  36. /*
  37. * These functions implement the four basic operations the algorithm uses.
  38. */
  39. function md5_cmn(q, a, b, x, s, t) {
  40. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
  41. }
  42. function md5_ff(a, b, c, d, x, s, t) {
  43. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  44. }
  45. function md5_gg(a, b, c, d, x, s, t) {
  46. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  47. }
  48. function md5_hh(a, b, c, d, x, s, t) {
  49. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  50. }
  51. function md5_ii(a, b, c, d, x, s, t) {
  52. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  53. }
  54. /*
  55. * Calculate the MD5 of an array of little-endian words, and a bit length.
  56. */
  57. function binl_md5(x, len) {
  58. /* append padding */
  59. x[len >> 5] |= 0x80 << (len % 32);
  60. x[(((len + 64) >>> 9) << 4) + 14] = len;
  61. var i, olda, oldb, oldc, oldd,
  62. a = 1732584193,
  63. b = -271733879,
  64. c = -1732584194,
  65. d = 271733878;
  66. for (i = 0; i < x.length; i += 16) {
  67. olda = a;
  68. oldb = b;
  69. oldc = c;
  70. oldd = d;
  71. a = md5_ff(a, b, c, d, x[i], 7, -680876936);
  72. d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
  73. c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
  74. b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
  75. a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
  76. d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
  77. c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
  78. b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
  79. a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
  80. d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
  81. c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
  82. b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
  83. a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
  84. d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
  85. c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
  86. b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
  87. a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
  88. d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
  89. c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
  90. b = md5_gg(b, c, d, a, x[i], 20, -373897302);
  91. a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
  92. d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
  93. c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
  94. b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
  95. a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
  96. d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
  97. c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
  98. b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
  99. a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
  100. d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
  101. c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
  102. b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
  103. a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
  104. d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
  105. c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
  106. b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
  107. a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
  108. d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
  109. c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
  110. b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
  111. a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
  112. d = md5_hh(d, a, b, c, x[i], 11, -358537222);
  113. c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
  114. b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
  115. a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
  116. d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
  117. c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
  118. b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
  119. a = md5_ii(a, b, c, d, x[i], 6, -198630844);
  120. d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
  121. c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
  122. b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
  123. a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
  124. d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
  125. c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
  126. b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
  127. a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
  128. d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
  129. c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
  130. b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
  131. a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
  132. d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
  133. c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
  134. b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
  135. a = safe_add(a, olda);
  136. b = safe_add(b, oldb);
  137. c = safe_add(c, oldc);
  138. d = safe_add(d, oldd);
  139. }
  140. return [a, b, c, d];
  141. }
  142. /*
  143. * Convert an array of little-endian words to a string
  144. */
  145. function binl2rstr(input) {
  146. var i,
  147. output = '';
  148. for (i = 0; i < input.length * 32; i += 8) {
  149. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
  150. }
  151. return output;
  152. }
  153. /*
  154. * Convert a raw string to an array of little-endian words
  155. * Characters >255 have their high-byte silently ignored.
  156. */
  157. function rstr2binl(input) {
  158. var i,
  159. output = [];
  160. output[(input.length >> 2) - 1] = undefined;
  161. for (i = 0; i < output.length; i += 1) {
  162. output[i] = 0;
  163. }
  164. for (i = 0; i < input.length * 8; i += 8) {
  165. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
  166. }
  167. return output;
  168. }
  169. /*
  170. * Calculate the MD5 of a raw string
  171. */
  172. function rstr_md5(s) {
  173. return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
  174. }
  175. /*
  176. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  177. */
  178. function rstr_hmac_md5(key, data) {
  179. var i,
  180. bkey = rstr2binl(key),
  181. ipad = [],
  182. opad = [],
  183. hash;
  184. ipad[15] = opad[15] = undefined;
  185. if (bkey.length > 16) {
  186. bkey = binl_md5(bkey, key.length * 8);
  187. }
  188. for (i = 0; i < 16; i += 1) {
  189. ipad[i] = bkey[i] ^ 0x36363636;
  190. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  191. }
  192. hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  193. return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
  194. }
  195. /*
  196. * Convert a raw string to a hex string
  197. */
  198. function rstr2hex(input) {
  199. var hex_tab = '0123456789abcdef',
  200. output = '',
  201. x,
  202. i;
  203. for (i = 0; i < input.length; i += 1) {
  204. x = input.charCodeAt(i);
  205. output += hex_tab.charAt((x >>> 4) & 0x0F) +
  206. hex_tab.charAt(x & 0x0F);
  207. }
  208. return output;
  209. }
  210. /*
  211. * Encode a string as utf-8
  212. */
  213. function str2rstr_utf8(input) {
  214. return unescape(encodeURIComponent(input));
  215. }
  216. /*
  217. * Take string arguments and return either raw or hex encoded strings
  218. */
  219. function raw_md5(s) {
  220. return rstr_md5(str2rstr_utf8(s));
  221. }
  222. function hex_md5(s) {
  223. return rstr2hex(raw_md5(s));
  224. }
  225. function raw_hmac_md5(k, d) {
  226. return rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d));
  227. }
  228. function hex_hmac_md5(k, d) {
  229. return rstr2hex(raw_hmac_md5(k, d));
  230. }
  231. export function md5(string, key, raw) {
  232. if (!key) {
  233. if (!raw) {
  234. return hex_md5(string);
  235. }
  236. return raw_md5(string);
  237. }
  238. if (!raw) {
  239. return hex_hmac_md5(key, string);
  240. }
  241. return raw_hmac_md5(key, string);
  242. }
  243. // module.exports = {
  244. // i: md5
  245. // }