|
19 | 19 |
|
20 | 20 | /* global define */ |
21 | 21 |
|
22 | | -;(function ($) { |
| 22 | +/* eslint-disable strict */ |
| 23 | + |
| 24 | +;(function($) { |
23 | 25 | 'use strict' |
24 | 26 |
|
25 | | - /* |
26 | | - * Add integers, wrapping at 2^32. This uses 16-bit operations internally |
27 | | - * to work around bugs in some JS interpreters. |
28 | | - */ |
29 | | - function safeAdd (x, y) { |
| 27 | + /** |
| 28 | + * Add integers, wrapping at 2^32. |
| 29 | + * This uses 16-bit operations internally to work around bugs in interpreters. |
| 30 | + * |
| 31 | + * @param {number} x First integer |
| 32 | + * @param {number} y Second integer |
| 33 | + * @returns {number} Sum |
| 34 | + */ |
| 35 | + function safeAdd(x, y) { |
30 | 36 | var lsw = (x & 0xffff) + (y & 0xffff) |
31 | 37 | var msw = (x >> 16) + (y >> 16) + (lsw >> 16) |
32 | 38 | return (msw << 16) | (lsw & 0xffff) |
33 | 39 | } |
34 | 40 |
|
35 | | - /* |
36 | | - * Bitwise rotate a 32-bit number to the left. |
37 | | - */ |
38 | | - function bitRotateLeft (num, cnt) { |
| 41 | + /** |
| 42 | + * Bitwise rotate a 32-bit number to the left. |
| 43 | + * |
| 44 | + * @param {number} num 32-bit number |
| 45 | + * @param {number} cnt Rotation count |
| 46 | + * @returns {number} Rotated number |
| 47 | + */ |
| 48 | + function bitRotateLeft(num, cnt) { |
39 | 49 | return (num << cnt) | (num >>> (32 - cnt)) |
40 | 50 | } |
41 | 51 |
|
42 | | - /* |
43 | | - * These functions implement the four basic operations the algorithm uses. |
44 | | - */ |
45 | | - function md5cmn (q, a, b, x, s, t) { |
| 52 | + /** |
| 53 | + * Basic operation the algorithm uses. |
| 54 | + * |
| 55 | + * @param {number} q q |
| 56 | + * @param {number} a a |
| 57 | + * @param {number} b b |
| 58 | + * @param {number} x x |
| 59 | + * @param {number} s s |
| 60 | + * @param {number} t t |
| 61 | + * @returns {number} Result |
| 62 | + */ |
| 63 | + function md5cmn(q, a, b, x, s, t) { |
46 | 64 | return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b) |
47 | 65 | } |
48 | | - function md5ff (a, b, c, d, x, s, t) { |
| 66 | + /** |
| 67 | + * Basic operation the algorithm uses. |
| 68 | + * |
| 69 | + * @param {number} a a |
| 70 | + * @param {number} b b |
| 71 | + * @param {number} c c |
| 72 | + * @param {number} d d |
| 73 | + * @param {number} x x |
| 74 | + * @param {number} s s |
| 75 | + * @param {number} t t |
| 76 | + * @returns {number} Result |
| 77 | + */ |
| 78 | + function md5ff(a, b, c, d, x, s, t) { |
49 | 79 | return md5cmn((b & c) | (~b & d), a, b, x, s, t) |
50 | 80 | } |
51 | | - function md5gg (a, b, c, d, x, s, t) { |
| 81 | + /** |
| 82 | + * Basic operation the algorithm uses. |
| 83 | + * |
| 84 | + * @param {number} a a |
| 85 | + * @param {number} b b |
| 86 | + * @param {number} c c |
| 87 | + * @param {number} d d |
| 88 | + * @param {number} x x |
| 89 | + * @param {number} s s |
| 90 | + * @param {number} t t |
| 91 | + * @returns {number} Result |
| 92 | + */ |
| 93 | + function md5gg(a, b, c, d, x, s, t) { |
52 | 94 | return md5cmn((b & d) | (c & ~d), a, b, x, s, t) |
53 | 95 | } |
54 | | - function md5hh (a, b, c, d, x, s, t) { |
| 96 | + /** |
| 97 | + * Basic operation the algorithm uses. |
| 98 | + * |
| 99 | + * @param {number} a a |
| 100 | + * @param {number} b b |
| 101 | + * @param {number} c c |
| 102 | + * @param {number} d d |
| 103 | + * @param {number} x x |
| 104 | + * @param {number} s s |
| 105 | + * @param {number} t t |
| 106 | + * @returns {number} Result |
| 107 | + */ |
| 108 | + function md5hh(a, b, c, d, x, s, t) { |
55 | 109 | return md5cmn(b ^ c ^ d, a, b, x, s, t) |
56 | 110 | } |
57 | | - function md5ii (a, b, c, d, x, s, t) { |
| 111 | + /** |
| 112 | + * Basic operation the algorithm uses. |
| 113 | + * |
| 114 | + * @param {number} a a |
| 115 | + * @param {number} b b |
| 116 | + * @param {number} c c |
| 117 | + * @param {number} d d |
| 118 | + * @param {number} x x |
| 119 | + * @param {number} s s |
| 120 | + * @param {number} t t |
| 121 | + * @returns {number} Result |
| 122 | + */ |
| 123 | + function md5ii(a, b, c, d, x, s, t) { |
58 | 124 | return md5cmn(c ^ (b | ~d), a, b, x, s, t) |
59 | 125 | } |
60 | 126 |
|
61 | | - /* |
62 | | - * Calculate the MD5 of an array of little-endian words, and a bit length. |
63 | | - */ |
64 | | - function binlMD5 (x, len) { |
| 127 | + /** |
| 128 | + * Calculate the MD5 of an array of little-endian words, and a bit length. |
| 129 | + * |
| 130 | + * @param {Array} x Array of little-endian words |
| 131 | + * @param {number} len Bit length |
| 132 | + * @returns {Array<number>} MD5 Array |
| 133 | + */ |
| 134 | + function binlMD5(x, len) { |
65 | 135 | /* append padding */ |
66 | | - x[len >> 5] |= 0x80 << (len % 32) |
67 | | - x[((len + 64) >>> 9 << 4) + 14] = len |
| 136 | + x[len >> 5] |= 0x80 << len % 32 |
| 137 | + x[(((len + 64) >>> 9) << 4) + 14] = len |
68 | 138 |
|
69 | 139 | var i |
70 | 140 | var olda |
|
158 | 228 | return [a, b, c, d] |
159 | 229 | } |
160 | 230 |
|
161 | | - /* |
162 | | - * Convert an array of little-endian words to a string |
163 | | - */ |
164 | | - function binl2rstr (input) { |
| 231 | + /** |
| 232 | + * Convert an array of little-endian words to a string |
| 233 | + * |
| 234 | + * @param {Array<number>} input MD5 Array |
| 235 | + * @returns {string} MD5 string |
| 236 | + */ |
| 237 | + function binl2rstr(input) { |
165 | 238 | var i |
166 | 239 | var output = '' |
167 | 240 | var length32 = input.length * 32 |
168 | 241 | for (i = 0; i < length32; i += 8) { |
169 | | - output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xff) |
| 242 | + output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff) |
170 | 243 | } |
171 | 244 | return output |
172 | 245 | } |
173 | 246 |
|
174 | | - /* |
175 | | - * Convert a raw string to an array of little-endian words |
176 | | - * Characters >255 have their high-byte silently ignored. |
177 | | - */ |
178 | | - function rstr2binl (input) { |
| 247 | + /** |
| 248 | + * Convert a raw string to an array of little-endian words |
| 249 | + * Characters >255 have their high-byte silently ignored. |
| 250 | + * |
| 251 | + * @param {string} input Raw input string |
| 252 | + * @returns {Array<number>} Array of little-endian words |
| 253 | + */ |
| 254 | + function rstr2binl(input) { |
179 | 255 | var i |
180 | 256 | var output = [] |
181 | 257 | output[(input.length >> 2) - 1] = undefined |
|
184 | 260 | } |
185 | 261 | var length8 = input.length * 8 |
186 | 262 | for (i = 0; i < length8; i += 8) { |
187 | | - output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << (i % 32) |
| 263 | + output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << i % 32 |
188 | 264 | } |
189 | 265 | return output |
190 | 266 | } |
191 | 267 |
|
192 | | - /* |
193 | | - * Calculate the MD5 of a raw string |
194 | | - */ |
195 | | - function rstrMD5 (s) { |
| 268 | + /** |
| 269 | + * Calculate the MD5 of a raw string |
| 270 | + * |
| 271 | + * @param {string} s Input string |
| 272 | + * @returns {string} Raw MD5 string |
| 273 | + */ |
| 274 | + function rstrMD5(s) { |
196 | 275 | return binl2rstr(binlMD5(rstr2binl(s), s.length * 8)) |
197 | 276 | } |
198 | 277 |
|
199 | | - /* |
200 | | - * Calculate the HMAC-MD5, of a key and some data (raw strings) |
201 | | - */ |
202 | | - function rstrHMACMD5 (key, data) { |
| 278 | + /** |
| 279 | + * Calculates the HMAC-MD5 of a key and some data (raw strings) |
| 280 | + * |
| 281 | + * @param {string} key HMAC key |
| 282 | + * @param {string} data Raw input string |
| 283 | + * @returns {string} Raw MD5 string |
| 284 | + */ |
| 285 | + function rstrHMACMD5(key, data) { |
203 | 286 | var i |
204 | 287 | var bkey = rstr2binl(key) |
205 | 288 | var ipad = [] |
|
217 | 300 | return binl2rstr(binlMD5(opad.concat(hash), 512 + 128)) |
218 | 301 | } |
219 | 302 |
|
220 | | - /* |
221 | | - * Convert a raw string to a hex string |
222 | | - */ |
223 | | - function rstr2hex (input) { |
| 303 | + /** |
| 304 | + * Convert a raw string to a hex string |
| 305 | + * |
| 306 | + * @param {string} input Raw input string |
| 307 | + * @returns {string} Hex encoded string |
| 308 | + */ |
| 309 | + function rstr2hex(input) { |
224 | 310 | var hexTab = '0123456789abcdef' |
225 | 311 | var output = '' |
226 | 312 | var x |
|
232 | 318 | return output |
233 | 319 | } |
234 | 320 |
|
235 | | - /* |
236 | | - * Encode a string as utf-8 |
237 | | - */ |
238 | | - function str2rstrUTF8 (input) { |
| 321 | + /** |
| 322 | + * Encode a string as UTF-8 |
| 323 | + * |
| 324 | + * @param {string} input Input string |
| 325 | + * @returns {string} UTF8 string |
| 326 | + */ |
| 327 | + function str2rstrUTF8(input) { |
239 | 328 | return unescape(encodeURIComponent(input)) |
240 | 329 | } |
241 | 330 |
|
242 | | - /* |
243 | | - * Take string arguments and return either raw or hex encoded strings |
244 | | - */ |
245 | | - function rawMD5 (s) { |
| 331 | + /** |
| 332 | + * Encodes input string as raw MD5 string |
| 333 | + * |
| 334 | + * @param {string} s Input string |
| 335 | + * @returns {string} Raw MD5 string |
| 336 | + */ |
| 337 | + function rawMD5(s) { |
246 | 338 | return rstrMD5(str2rstrUTF8(s)) |
247 | 339 | } |
248 | | - function hexMD5 (s) { |
| 340 | + /** |
| 341 | + * Encodes input string as Hex encoded string |
| 342 | + * |
| 343 | + * @param {string} s Input string |
| 344 | + * @returns {string} Hex encoded string |
| 345 | + */ |
| 346 | + function hexMD5(s) { |
249 | 347 | return rstr2hex(rawMD5(s)) |
250 | 348 | } |
251 | | - function rawHMACMD5 (k, d) { |
| 349 | + /** |
| 350 | + * Calculates the raw HMAC-MD5 for the given key and data |
| 351 | + * |
| 352 | + * @param {string} k HMAC key |
| 353 | + * @param {string} d Input string |
| 354 | + * @returns {string} Raw MD5 string |
| 355 | + */ |
| 356 | + function rawHMACMD5(k, d) { |
252 | 357 | return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d)) |
253 | 358 | } |
254 | | - function hexHMACMD5 (k, d) { |
| 359 | + /** |
| 360 | + * Calculates the Hex encoded HMAC-MD5 for the given key and data |
| 361 | + * |
| 362 | + * @param {string} k HMAC key |
| 363 | + * @param {string} d Input string |
| 364 | + * @returns {string} Raw MD5 string |
| 365 | + */ |
| 366 | + function hexHMACMD5(k, d) { |
255 | 367 | return rstr2hex(rawHMACMD5(k, d)) |
256 | 368 | } |
257 | 369 |
|
258 | | - function md5 (string, key, raw) { |
| 370 | + /** |
| 371 | + * Calculates MD5 value for a given string. |
| 372 | + * If a key is provided, calculates the HMAC-MD5 value. |
| 373 | + * Returns a Hex encoded string unless the raw argument is given. |
| 374 | + * |
| 375 | + * @param {string} string Input string |
| 376 | + * @param {string} [key] HMAC key |
| 377 | + * @param {boolean} raw Raw oytput switch |
| 378 | + * @returns {string} MD5 output |
| 379 | + */ |
| 380 | + function md5(string, key, raw) { |
259 | 381 | if (!key) { |
260 | 382 | if (!raw) { |
261 | 383 | return hexMD5(string) |
|
269 | 391 | } |
270 | 392 |
|
271 | 393 | if (typeof define === 'function' && define.amd) { |
272 | | - define(function () { |
| 394 | + define(function() { |
273 | 395 | return md5 |
274 | 396 | }) |
275 | 397 | } else if (typeof module === 'object' && module.exports) { |
|
0 commit comments