报名地址

md5.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * JavaScript MD5
  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. * https://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. /* global define */
  20. /* eslint-disable strict */
  21. ;(function ($) {
  22. 'use strict'
  23. /**
  24. * Add integers, wrapping at 2^32.
  25. * This uses 16-bit operations internally to work around bugs in interpreters.
  26. *
  27. * @param {number} x First integer
  28. * @param {number} y Second integer
  29. * @returns {number} Sum
  30. */
  31. function safeAdd(x, y) {
  32. var lsw = (x & 0xffff) + (y & 0xffff)
  33. var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
  34. return (msw << 16) | (lsw & 0xffff)
  35. }
  36. /**
  37. * Bitwise rotate a 32-bit number to the left.
  38. *
  39. * @param {number} num 32-bit number
  40. * @param {number} cnt Rotation count
  41. * @returns {number} Rotated number
  42. */
  43. function bitRotateLeft(num, cnt) {
  44. return (num << cnt) | (num >>> (32 - cnt))
  45. }
  46. /**
  47. * Basic operation the algorithm uses.
  48. *
  49. * @param {number} q q
  50. * @param {number} a a
  51. * @param {number} b b
  52. * @param {number} x x
  53. * @param {number} s s
  54. * @param {number} t t
  55. * @returns {number} Result
  56. */
  57. function md5cmn(q, a, b, x, s, t) {
  58. return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b)
  59. }
  60. /**
  61. * Basic operation the algorithm uses.
  62. *
  63. * @param {number} a a
  64. * @param {number} b b
  65. * @param {number} c c
  66. * @param {number} d d
  67. * @param {number} x x
  68. * @param {number} s s
  69. * @param {number} t t
  70. * @returns {number} Result
  71. */
  72. function md5ff(a, b, c, d, x, s, t) {
  73. return md5cmn((b & c) | (~b & d), a, b, x, s, t)
  74. }
  75. /**
  76. * Basic operation the algorithm uses.
  77. *
  78. * @param {number} a a
  79. * @param {number} b b
  80. * @param {number} c c
  81. * @param {number} d d
  82. * @param {number} x x
  83. * @param {number} s s
  84. * @param {number} t t
  85. * @returns {number} Result
  86. */
  87. function md5gg(a, b, c, d, x, s, t) {
  88. return md5cmn((b & d) | (c & ~d), a, b, x, s, t)
  89. }
  90. /**
  91. * Basic operation the algorithm uses.
  92. *
  93. * @param {number} a a
  94. * @param {number} b b
  95. * @param {number} c c
  96. * @param {number} d d
  97. * @param {number} x x
  98. * @param {number} s s
  99. * @param {number} t t
  100. * @returns {number} Result
  101. */
  102. function md5hh(a, b, c, d, x, s, t) {
  103. return md5cmn(b ^ c ^ d, a, b, x, s, t)
  104. }
  105. /**
  106. * Basic operation the algorithm uses.
  107. *
  108. * @param {number} a a
  109. * @param {number} b b
  110. * @param {number} c c
  111. * @param {number} d d
  112. * @param {number} x x
  113. * @param {number} s s
  114. * @param {number} t t
  115. * @returns {number} Result
  116. */
  117. function md5ii(a, b, c, d, x, s, t) {
  118. return md5cmn(c ^ (b | ~d), a, b, x, s, t)
  119. }
  120. /**
  121. * Calculate the MD5 of an array of little-endian words, and a bit length.
  122. *
  123. * @param {Array} x Array of little-endian words
  124. * @param {number} len Bit length
  125. * @returns {Array<number>} MD5 Array
  126. */
  127. function binlMD5(x, len) {
  128. /* append padding */
  129. x[len >> 5] |= 0x80 << len % 32
  130. x[(((len + 64) >>> 9) << 4) + 14] = len
  131. var i
  132. var olda
  133. var oldb
  134. var oldc
  135. var oldd
  136. var a = 1732584193
  137. var b = -271733879
  138. var c = -1732584194
  139. var d = 271733878
  140. for (i = 0; i < x.length; i += 16) {
  141. olda = a
  142. oldb = b
  143. oldc = c
  144. oldd = d
  145. a = md5ff(a, b, c, d, x[i], 7, -680876936)
  146. d = md5ff(d, a, b, c, x[i + 1], 12, -389564586)
  147. c = md5ff(c, d, a, b, x[i + 2], 17, 606105819)
  148. b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330)
  149. a = md5ff(a, b, c, d, x[i + 4], 7, -176418897)
  150. d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426)
  151. c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341)
  152. b = md5ff(b, c, d, a, x[i + 7], 22, -45705983)
  153. a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416)
  154. d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417)
  155. c = md5ff(c, d, a, b, x[i + 10], 17, -42063)
  156. b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162)
  157. a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682)
  158. d = md5ff(d, a, b, c, x[i + 13], 12, -40341101)
  159. c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290)
  160. b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329)
  161. a = md5gg(a, b, c, d, x[i + 1], 5, -165796510)
  162. d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632)
  163. c = md5gg(c, d, a, b, x[i + 11], 14, 643717713)
  164. b = md5gg(b, c, d, a, x[i], 20, -373897302)
  165. a = md5gg(a, b, c, d, x[i + 5], 5, -701558691)
  166. d = md5gg(d, a, b, c, x[i + 10], 9, 38016083)
  167. c = md5gg(c, d, a, b, x[i + 15], 14, -660478335)
  168. b = md5gg(b, c, d, a, x[i + 4], 20, -405537848)
  169. a = md5gg(a, b, c, d, x[i + 9], 5, 568446438)
  170. d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690)
  171. c = md5gg(c, d, a, b, x[i + 3], 14, -187363961)
  172. b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501)
  173. a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467)
  174. d = md5gg(d, a, b, c, x[i + 2], 9, -51403784)
  175. c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473)
  176. b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734)
  177. a = md5hh(a, b, c, d, x[i + 5], 4, -378558)
  178. d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463)
  179. c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562)
  180. b = md5hh(b, c, d, a, x[i + 14], 23, -35309556)
  181. a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060)
  182. d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353)
  183. c = md5hh(c, d, a, b, x[i + 7], 16, -155497632)
  184. b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640)
  185. a = md5hh(a, b, c, d, x[i + 13], 4, 681279174)
  186. d = md5hh(d, a, b, c, x[i], 11, -358537222)
  187. c = md5hh(c, d, a, b, x[i + 3], 16, -722521979)
  188. b = md5hh(b, c, d, a, x[i + 6], 23, 76029189)
  189. a = md5hh(a, b, c, d, x[i + 9], 4, -640364487)
  190. d = md5hh(d, a, b, c, x[i + 12], 11, -421815835)
  191. c = md5hh(c, d, a, b, x[i + 15], 16, 530742520)
  192. b = md5hh(b, c, d, a, x[i + 2], 23, -995338651)
  193. a = md5ii(a, b, c, d, x[i], 6, -198630844)
  194. d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415)
  195. c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905)
  196. b = md5ii(b, c, d, a, x[i + 5], 21, -57434055)
  197. a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571)
  198. d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606)
  199. c = md5ii(c, d, a, b, x[i + 10], 15, -1051523)
  200. b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799)
  201. a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359)
  202. d = md5ii(d, a, b, c, x[i + 15], 10, -30611744)
  203. c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380)
  204. b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649)
  205. a = md5ii(a, b, c, d, x[i + 4], 6, -145523070)
  206. d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379)
  207. c = md5ii(c, d, a, b, x[i + 2], 15, 718787259)
  208. b = md5ii(b, c, d, a, x[i + 9], 21, -343485551)
  209. a = safeAdd(a, olda)
  210. b = safeAdd(b, oldb)
  211. c = safeAdd(c, oldc)
  212. d = safeAdd(d, oldd)
  213. }
  214. return [a, b, c, d]
  215. }
  216. /**
  217. * Convert an array of little-endian words to a string
  218. *
  219. * @param {Array<number>} input MD5 Array
  220. * @returns {string} MD5 string
  221. */
  222. function binl2rstr(input) {
  223. var i
  224. var output = ''
  225. var length32 = input.length * 32
  226. for (i = 0; i < length32; i += 8) {
  227. output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff)
  228. }
  229. return output
  230. }
  231. /**
  232. * Convert a raw string to an array of little-endian words
  233. * Characters >255 have their high-byte silently ignored.
  234. *
  235. * @param {string} input Raw input string
  236. * @returns {Array<number>} Array of little-endian words
  237. */
  238. function rstr2binl(input) {
  239. var i
  240. var output = []
  241. output[(input.length >> 2) - 1] = undefined
  242. for (i = 0; i < output.length; i += 1) {
  243. output[i] = 0
  244. }
  245. var length8 = input.length * 8
  246. for (i = 0; i < length8; i += 8) {
  247. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << i % 32
  248. }
  249. return output
  250. }
  251. /**
  252. * Calculate the MD5 of a raw string
  253. *
  254. * @param {string} s Input string
  255. * @returns {string} Raw MD5 string
  256. */
  257. function rstrMD5(s) {
  258. return binl2rstr(binlMD5(rstr2binl(s), s.length * 8))
  259. }
  260. /**
  261. * Calculates the HMAC-MD5 of a key and some data (raw strings)
  262. *
  263. * @param {string} key HMAC key
  264. * @param {string} data Raw input string
  265. * @returns {string} Raw MD5 string
  266. */
  267. function rstrHMACMD5(key, data) {
  268. var i
  269. var bkey = rstr2binl(key)
  270. var ipad = []
  271. var opad = []
  272. var hash
  273. ipad[15] = opad[15] = undefined
  274. if (bkey.length > 16) {
  275. bkey = binlMD5(bkey, key.length * 8)
  276. }
  277. for (i = 0; i < 16; i += 1) {
  278. ipad[i] = bkey[i] ^ 0x36363636
  279. opad[i] = bkey[i] ^ 0x5c5c5c5c
  280. }
  281. hash = binlMD5(ipad.concat(rstr2binl(data)), 512 + data.length * 8)
  282. return binl2rstr(binlMD5(opad.concat(hash), 512 + 128))
  283. }
  284. /**
  285. * Convert a raw string to a hex string
  286. *
  287. * @param {string} input Raw input string
  288. * @returns {string} Hex encoded string
  289. */
  290. function rstr2hex(input) {
  291. var hexTab = '0123456789abcdef'
  292. var output = ''
  293. var x
  294. var i
  295. for (i = 0; i < input.length; i += 1) {
  296. x = input.charCodeAt(i)
  297. output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f)
  298. }
  299. return output
  300. }
  301. /**
  302. * Encode a string as UTF-8
  303. *
  304. * @param {string} input Input string
  305. * @returns {string} UTF8 string
  306. */
  307. function str2rstrUTF8(input) {
  308. return unescape(encodeURIComponent(input))
  309. }
  310. /**
  311. * Encodes input string as raw MD5 string
  312. *
  313. * @param {string} s Input string
  314. * @returns {string} Raw MD5 string
  315. */
  316. function rawMD5(s) {
  317. return rstrMD5(str2rstrUTF8(s))
  318. }
  319. /**
  320. * Encodes input string as Hex encoded string
  321. *
  322. * @param {string} s Input string
  323. * @returns {string} Hex encoded string
  324. */
  325. function hexMD5(s) {
  326. return rstr2hex(rawMD5(s))
  327. }
  328. /**
  329. * Calculates the raw HMAC-MD5 for the given key and data
  330. *
  331. * @param {string} k HMAC key
  332. * @param {string} d Input string
  333. * @returns {string} Raw MD5 string
  334. */
  335. function rawHMACMD5(k, d) {
  336. return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d))
  337. }
  338. /**
  339. * Calculates the Hex encoded HMAC-MD5 for the given key and data
  340. *
  341. * @param {string} k HMAC key
  342. * @param {string} d Input string
  343. * @returns {string} Raw MD5 string
  344. */
  345. function hexHMACMD5(k, d) {
  346. return rstr2hex(rawHMACMD5(k, d))
  347. }
  348. /**
  349. * Calculates MD5 value for a given string.
  350. * If a key is provided, calculates the HMAC-MD5 value.
  351. * Returns a Hex encoded string unless the raw argument is given.
  352. *
  353. * @param {string} string Input string
  354. * @param {string} [key] HMAC key
  355. * @param {boolean} [raw] Raw output switch
  356. * @returns {string} MD5 output
  357. */
  358. function md5(string, key, raw) {
  359. if (!key) {
  360. if (!raw) {
  361. return hexMD5(string)
  362. }
  363. return rawMD5(string)
  364. }
  365. if (!raw) {
  366. return hexHMACMD5(key, string)
  367. }
  368. return rawHMACMD5(key, string)
  369. }
  370. if (typeof define === 'function' && define.amd) {
  371. define(function () {
  372. return md5
  373. })
  374. } else if (typeof module === 'object' && module.exports) {
  375. module.exports = md5
  376. } else {
  377. $.md5 = md5
  378. }
  379. })(this)