Skip to content

Commit de2529c

Browse files
committed
Use blueimp+jsdoc+prettier config for eslint.
Move eslint configs to package.json. Use package.json files property instead of .npmignore.
1 parent 64415da commit de2529c

File tree

9 files changed

+2072
-106
lines changed

9 files changed

+2072
-106
lines changed

.eslintignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

.eslintrc.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
.DS_Store
21
node_modules

.npmignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

js/demo/demo.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111

1212
/* global md5 */
1313

14-
;(function () {
14+
/* eslint-disable strict */
15+
16+
;(function() {
1517
'use strict'
1618

1719
var input = document.getElementById('input')
18-
document.getElementById('calculate').addEventListener(
19-
'click',
20-
function (event) {
20+
document
21+
.getElementById('calculate')
22+
.addEventListener('click', function(event) {
2123
event.preventDefault()
2224
document.getElementById('result').value = md5(input.value)
23-
}
24-
)
25+
})
2526
input.value = '日本'
26-
}())
27+
})()

js/md5.js

Lines changed: 182 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,122 @@
1919

2020
/* global define */
2121

22-
;(function ($) {
22+
/* eslint-disable strict */
23+
24+
;(function($) {
2325
'use strict'
2426

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) {
3036
var lsw = (x & 0xffff) + (y & 0xffff)
3137
var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
3238
return (msw << 16) | (lsw & 0xffff)
3339
}
3440

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) {
3949
return (num << cnt) | (num >>> (32 - cnt))
4050
}
4151

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) {
4664
return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b)
4765
}
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) {
4979
return md5cmn((b & c) | (~b & d), a, b, x, s, t)
5080
}
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) {
5294
return md5cmn((b & d) | (c & ~d), a, b, x, s, t)
5395
}
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) {
55109
return md5cmn(b ^ c ^ d, a, b, x, s, t)
56110
}
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) {
58124
return md5cmn(c ^ (b | ~d), a, b, x, s, t)
59125
}
60126

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) {
65135
/* 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
68138

69139
var i
70140
var olda
@@ -158,24 +228,30 @@
158228
return [a, b, c, d]
159229
}
160230

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) {
165238
var i
166239
var output = ''
167240
var length32 = input.length * 32
168241
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)
170243
}
171244
return output
172245
}
173246

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) {
179255
var i
180256
var output = []
181257
output[(input.length >> 2) - 1] = undefined
@@ -184,22 +260,29 @@
184260
}
185261
var length8 = input.length * 8
186262
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
188264
}
189265
return output
190266
}
191267

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) {
196275
return binl2rstr(binlMD5(rstr2binl(s), s.length * 8))
197276
}
198277

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) {
203286
var i
204287
var bkey = rstr2binl(key)
205288
var ipad = []
@@ -217,10 +300,13 @@
217300
return binl2rstr(binlMD5(opad.concat(hash), 512 + 128))
218301
}
219302

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) {
224310
var hexTab = '0123456789abcdef'
225311
var output = ''
226312
var x
@@ -232,30 +318,66 @@
232318
return output
233319
}
234320

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) {
239328
return unescape(encodeURIComponent(input))
240329
}
241330

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) {
246338
return rstrMD5(str2rstrUTF8(s))
247339
}
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) {
249347
return rstr2hex(rawMD5(s))
250348
}
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) {
252357
return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d))
253358
}
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) {
255367
return rstr2hex(rawHMACMD5(k, d))
256368
}
257369

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) {
259381
if (!key) {
260382
if (!raw) {
261383
return hexMD5(string)
@@ -269,7 +391,7 @@
269391
}
270392

271393
if (typeof define === 'function' && define.amd) {
272-
define(function () {
394+
define(function() {
273395
return md5
274396
})
275397
} else if (typeof module === 'object' && module.exports) {

0 commit comments

Comments
 (0)