tinyhmacmd5 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/browser.min.js +1 -1
- package/main.js +64 -191
- package/package.json +5 -4
package/README.md
CHANGED
package/browser.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** @license https://github.com/bddjr/tinyhmacmd5/blob/HEAD/LICENSE */
|
|
2
|
-
{let r="length",e=16,n=2**32,t=
|
|
2
|
+
{let r="length",e=16,n=2**32,t=1732584193,o=271733878,a=~t,f=~o,u=Math,i=u.floor,l=[738695,669989,770404,703814],d=Array(64),s=(s,v)=>{var y,c,g,h,A,w=0,b=i((v+64)/512)*e+14,m=[t,f,a,o],p=[...m],x=r=>m[3&++A];for(s[i(v/32)]|=128<<v%32,s[b]=0|v,s[b+1]=0|v/n;w<s[r];w+=e){for(A=b=0;b<64;A-=6)m[3&A]=(c=x(),g=x(),h=x(),0|((c=((y=b>>4)?y>1?y>2?g^(c|~h):c^g^h:c&h|g&~h:c&g|~c&h)+(0|s[w+(b*(29521>>4*y)+(1296>>4*y)&15)])+(g=l[y]>>b%4*5&31,d[b++]??=0|n*u.abs(u.sin(b)))+x())<<g|c>>>32-g)+x());for(A=4;A;)p[--A]=m[A]=0|p[A]+m[A]}return m},v=e=>{for(var n=e[r],t=[];n;)t[i(--n/4)]|=e[n]<<n%4*8;return t},y=r=>"string"==typeof r?(new TextEncoder).encode(r):r;var md5=(n,t,o)=>{var a,f=v(n=y(n)),u=8*n[r],i=e,l=new Uint8Array(e),d=r=>{for(f.unshift(...Array(e));i;)f[--i]=16843009*r^a[i];i=e};for(null!=t&&((a=v(t=y(t)))[r]>e&&(a=s(a,8*t[r])),d(54),f=s(f,512+u),d(92),u=640),f=s(f,u);i;)l[--i]=f[i>>2]>>>i%4*8;return o?l:l.reduce((r,n)=>r+(n>>4&&"")+n.toString(e),"")}}
|
package/main.js
CHANGED
|
@@ -8,102 +8,27 @@ let $length = "length"
|
|
|
8
8
|
/** @type {16} */
|
|
9
9
|
let $16 = 16
|
|
10
10
|
|
|
11
|
-
/** @type {number} */
|
|
12
11
|
let $2_32 = 2 ** 32
|
|
13
12
|
|
|
13
|
+
let A = 1732584193
|
|
14
|
+
, D = 271733878
|
|
15
|
+
, C = ~A
|
|
16
|
+
, B = ~D
|
|
17
|
+
|
|
14
18
|
let $Math = Math
|
|
15
19
|
|
|
16
20
|
let floor = $Math.floor
|
|
17
21
|
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
let safeAdd = (x, y) => 0 | (0 | x) + (0 | y)
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Bitwise rotate a 32-bit number to the left.
|
|
29
|
-
*
|
|
30
|
-
* @param {number} num 32-bit number
|
|
31
|
-
* @param {number} cnt Rotation count
|
|
32
|
-
* @returns {number} Rotated number
|
|
33
|
-
*/
|
|
34
|
-
let bitRotateLeft = (num, cnt) => (num << cnt) | (num >>> (32 - cnt))
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Basic operation the algorithm uses.
|
|
38
|
-
*
|
|
39
|
-
* @param {number} q q
|
|
40
|
-
* @param {number} a a
|
|
41
|
-
* @param {number} b b
|
|
42
|
-
* @param {number} x x
|
|
43
|
-
* @param {number} s s
|
|
44
|
-
* @param {number} t t
|
|
45
|
-
* @returns {number} Result
|
|
46
|
-
*/
|
|
47
|
-
let md5cmn = (q, a, b, x, s, t) => safeAdd(bitRotateLeft(safeAdd(a, q) + safeAdd(x, t), s), b)
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Basic operation the algorithm uses.
|
|
51
|
-
*
|
|
52
|
-
* @param {number} a a
|
|
53
|
-
* @param {number} b b
|
|
54
|
-
* @param {number} c c
|
|
55
|
-
* @param {number} d d
|
|
56
|
-
* @param {number} x x
|
|
57
|
-
* @param {number} s s
|
|
58
|
-
* @param {number} t t
|
|
59
|
-
* @returns {number} Result
|
|
60
|
-
*/
|
|
61
|
-
let md5ff = (a, b, c, d, x, s, t) => md5cmn((b & c) | (~b & d), a, b, x, s, t)
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Basic operation the algorithm uses.
|
|
65
|
-
*
|
|
66
|
-
* @param {number} a a
|
|
67
|
-
* @param {number} b b
|
|
68
|
-
* @param {number} c c
|
|
69
|
-
* @param {number} d d
|
|
70
|
-
* @param {number} x x
|
|
71
|
-
* @param {number} s s
|
|
72
|
-
* @param {number} t t
|
|
73
|
-
* @returns {number} Result
|
|
74
|
-
*/
|
|
75
|
-
let md5gg = (a, b, c, d, x, s, t) => md5cmn((b & d) | (c & ~d), a, b, x, s, t)
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Basic operation the algorithm uses.
|
|
79
|
-
*
|
|
80
|
-
* @param {number} a a
|
|
81
|
-
* @param {number} b b
|
|
82
|
-
* @param {number} c c
|
|
83
|
-
* @param {number} d d
|
|
84
|
-
* @param {number} x x
|
|
85
|
-
* @param {number} s s
|
|
86
|
-
* @param {number} t t
|
|
87
|
-
* @returns {number} Result
|
|
88
|
-
*/
|
|
89
|
-
let md5hh = (a, b, c, d, x, s, t) => md5cmn(b ^ c ^ d, a, b, x, s, t)
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Basic operation the algorithm uses.
|
|
93
|
-
*
|
|
94
|
-
* @param {number} a a
|
|
95
|
-
* @param {number} b b
|
|
96
|
-
* @param {number} c c
|
|
97
|
-
* @param {number} d d
|
|
98
|
-
* @param {number} x x
|
|
99
|
-
* @param {number} s s
|
|
100
|
-
* @param {number} t t
|
|
101
|
-
* @returns {number} Result
|
|
102
|
-
*/
|
|
103
|
-
let md5ii = (a, b, c, d, x, s, t) => md5cmn(c ^ (b | ~d), a, b, x, s, t)
|
|
22
|
+
/** @type {number[]} */
|
|
23
|
+
let S = [
|
|
24
|
+
738695, // 7, 12, 17, 22
|
|
25
|
+
669989, // 5, 9, 14, 20
|
|
26
|
+
770404, // 4, 11, 16, 23
|
|
27
|
+
703814, // 6, 10, 15, 21
|
|
28
|
+
]
|
|
104
29
|
|
|
105
30
|
/** @type {number[]} MD5 constants cached in memory */
|
|
106
|
-
let K =
|
|
31
|
+
let K = Array(64)
|
|
107
32
|
|
|
108
33
|
/**
|
|
109
34
|
* Calculate the MD5 of an array of little-endian words, and a bit length.
|
|
@@ -114,110 +39,58 @@ let K = []
|
|
|
114
39
|
*/
|
|
115
40
|
let binlMD5 = (x, bitLen) => {
|
|
116
41
|
var i = 0
|
|
117
|
-
,
|
|
118
|
-
,
|
|
119
|
-
,
|
|
120
|
-
,
|
|
121
|
-
,
|
|
122
|
-
|
|
123
|
-
, c = ~a
|
|
124
|
-
, b = ~d
|
|
125
|
-
, padLenIndex = floor((bitLen + 64) / 512) * $16 + 14
|
|
42
|
+
, j = floor((bitLen + 64) / 512) * $16 + 14
|
|
43
|
+
, l
|
|
44
|
+
, t0
|
|
45
|
+
, t1
|
|
46
|
+
, t2
|
|
47
|
+
|
|
126
48
|
/** @type {number} */
|
|
127
|
-
var
|
|
128
|
-
|
|
49
|
+
var oi
|
|
50
|
+
|
|
51
|
+
/** @type {[number, number, number, number]} */
|
|
52
|
+
var output = [A, B, C, D]
|
|
53
|
+
, oldOutput = [...output]
|
|
54
|
+
, o = (/**@type {*}*/ _) => output[++oi & 3]
|
|
129
55
|
|
|
130
56
|
// append padding
|
|
131
57
|
x[floor(bitLen / 32)] |= 0x80 << bitLen % 32;
|
|
132
|
-
x[
|
|
133
|
-
x[
|
|
134
|
-
|
|
135
|
-
for (; i < x[$length];
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
d = md5gg(d, a, b, c, x6, 9, k())
|
|
167
|
-
c = md5gg(c, d, a, b, x11, 14, k())
|
|
168
|
-
b = md5gg(b, c, d, a, x0, 20, k())
|
|
169
|
-
a = md5gg(a, b, c, d, x5, 5, k())
|
|
170
|
-
d = md5gg(d, a, b, c, x10, 9, k())
|
|
171
|
-
c = md5gg(c, d, a, b, x15, 14, k())
|
|
172
|
-
b = md5gg(b, c, d, a, x4, 20, k())
|
|
173
|
-
a = md5gg(a, b, c, d, x9, 5, k())
|
|
174
|
-
d = md5gg(d, a, b, c, x14, 9, k())
|
|
175
|
-
c = md5gg(c, d, a, b, x3, 14, k())
|
|
176
|
-
b = md5gg(b, c, d, a, x8, 20, k())
|
|
177
|
-
a = md5gg(a, b, c, d, x13, 5, k())
|
|
178
|
-
d = md5gg(d, a, b, c, x2, 9, k())
|
|
179
|
-
c = md5gg(c, d, a, b, x7, 14, k())
|
|
180
|
-
b = md5gg(b, c, d, a, x12, 20, k())
|
|
181
|
-
|
|
182
|
-
a = md5hh(a, b, c, d, x5, 4, k())
|
|
183
|
-
d = md5hh(d, a, b, c, x8, 11, k())
|
|
184
|
-
c = md5hh(c, d, a, b, x11, $16, k())
|
|
185
|
-
b = md5hh(b, c, d, a, x14, 23, k())
|
|
186
|
-
a = md5hh(a, b, c, d, x1, 4, k())
|
|
187
|
-
d = md5hh(d, a, b, c, x4, 11, k())
|
|
188
|
-
c = md5hh(c, d, a, b, x7, $16, k())
|
|
189
|
-
b = md5hh(b, c, d, a, x10, 23, k())
|
|
190
|
-
a = md5hh(a, b, c, d, x13, 4, k())
|
|
191
|
-
d = md5hh(d, a, b, c, x0, 11, k())
|
|
192
|
-
c = md5hh(c, d, a, b, x3, $16, k())
|
|
193
|
-
b = md5hh(b, c, d, a, x6, 23, k())
|
|
194
|
-
a = md5hh(a, b, c, d, x9, 4, k())
|
|
195
|
-
d = md5hh(d, a, b, c, x12, 11, k())
|
|
196
|
-
c = md5hh(c, d, a, b, x15, $16, k())
|
|
197
|
-
b = md5hh(b, c, d, a, x2, 23, k())
|
|
198
|
-
|
|
199
|
-
a = md5ii(a, b, c, d, x0, 6, k())
|
|
200
|
-
d = md5ii(d, a, b, c, x7, 10, k())
|
|
201
|
-
c = md5ii(c, d, a, b, x14, 15, k())
|
|
202
|
-
b = md5ii(b, c, d, a, x5, 21, k())
|
|
203
|
-
a = md5ii(a, b, c, d, x12, 6, k())
|
|
204
|
-
d = md5ii(d, a, b, c, x3, 10, k())
|
|
205
|
-
c = md5ii(c, d, a, b, x10, 15, k())
|
|
206
|
-
b = md5ii(b, c, d, a, x1, 21, k())
|
|
207
|
-
a = md5ii(a, b, c, d, x8, 6, k())
|
|
208
|
-
d = md5ii(d, a, b, c, x15, 10, k())
|
|
209
|
-
c = md5ii(c, d, a, b, x6, 15, k())
|
|
210
|
-
b = md5ii(b, c, d, a, x13, 21, k())
|
|
211
|
-
a = md5ii(a, b, c, d, x4, 6, k())
|
|
212
|
-
d = md5ii(d, a, b, c, x11, 10, k())
|
|
213
|
-
c = md5ii(c, d, a, b, x2, 15, k())
|
|
214
|
-
b = md5ii(b, c, d, a, x9, 21, k())
|
|
215
|
-
|
|
216
|
-
a = safeAdd(a, olda)
|
|
217
|
-
b = safeAdd(b, oldb)
|
|
218
|
-
c = safeAdd(c, oldc)
|
|
58
|
+
x[j] = 0 | bitLen;
|
|
59
|
+
x[j + 1] = 0 | bitLen / $2_32;
|
|
60
|
+
|
|
61
|
+
for (; i < x[$length]; i += $16) {
|
|
62
|
+
for (oi = j = 0; j < 64; oi -= 6) {
|
|
63
|
+
output[oi & 3] = (
|
|
64
|
+
t0 = (
|
|
65
|
+
(
|
|
66
|
+
t0 = o(),
|
|
67
|
+
t1 = o(),
|
|
68
|
+
t2 = o(),
|
|
69
|
+
(l = j >> 4)
|
|
70
|
+
? l > 1
|
|
71
|
+
? l > 2
|
|
72
|
+
? t1 ^ (t0 | ~t2) // 3
|
|
73
|
+
: t0 ^ t1 ^ t2 // 2
|
|
74
|
+
: t0 & t2 | t1 & ~t2 // 1
|
|
75
|
+
: t0 & t1 | ~t0 & t2 // 0
|
|
76
|
+
) +
|
|
77
|
+
(
|
|
78
|
+
0 | x[i + (j * (0x7351 >> l * 4) + (0x0510 >> l * 4) & 15)]
|
|
79
|
+
) +
|
|
80
|
+
(
|
|
81
|
+
t1 = S[l] >> j % 4 * 5 & 31,
|
|
82
|
+
K[j++] ??= 0 | $2_32 * $Math.abs($Math.sin(j))
|
|
83
|
+
) +
|
|
84
|
+
o()
|
|
85
|
+
),
|
|
86
|
+
0 | (t0 << t1 | t0 >>> 32 - t1) + o()
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
for (oi = 4; oi;) {
|
|
90
|
+
oldOutput[--oi] = output[oi] = 0 | oldOutput[oi] + output[oi]
|
|
91
|
+
}
|
|
219
92
|
}
|
|
220
|
-
return
|
|
93
|
+
return output
|
|
221
94
|
}
|
|
222
95
|
|
|
223
96
|
/**
|
|
@@ -279,8 +152,9 @@ var md5 = (data, key, raw) => {
|
|
|
279
152
|
/** @type {*} */
|
|
280
153
|
var bkey
|
|
281
154
|
, pad = (/**@type {number}*/ x) => {
|
|
155
|
+
bdata.unshift(...Array($16))
|
|
282
156
|
for (; i;) {
|
|
283
|
-
bdata
|
|
157
|
+
bdata[--i] = 0x01010101 * x ^ bkey[i]
|
|
284
158
|
}
|
|
285
159
|
i = $16
|
|
286
160
|
}
|
|
@@ -291,9 +165,9 @@ var md5 = (data, key, raw) => {
|
|
|
291
165
|
if (bkey[$length] > $16) {
|
|
292
166
|
bkey = binlMD5(bkey, key[$length] * 8)
|
|
293
167
|
}
|
|
294
|
-
pad(
|
|
168
|
+
pad(0x36)
|
|
295
169
|
bdata = binlMD5(bdata, 512 + bitLen)
|
|
296
|
-
pad(
|
|
170
|
+
pad(0x5c)
|
|
297
171
|
bitLen = 512 + 128
|
|
298
172
|
}
|
|
299
173
|
|
|
@@ -307,7 +181,6 @@ var md5 = (data, key, raw) => {
|
|
|
307
181
|
return raw
|
|
308
182
|
? out
|
|
309
183
|
: out.reduce((p, v) => p + (v >> 4 && '') + v.toString($16), '')
|
|
310
|
-
// : out.toHex()
|
|
311
184
|
}
|
|
312
185
|
|
|
313
186
|
export default md5
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tinyhmacmd5",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "A tiny JavaScript HMAC-MD5 implementation. The minified browser bundle is only
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "A tiny JavaScript HMAC-MD5 implementation. The minified browser bundle is only 1029 bytes.",
|
|
5
5
|
"author": "bddjr",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
@@ -29,11 +29,12 @@
|
|
|
29
29
|
"url": "git+https://github.com/bddjr/tinyhmacmd5.git"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
+
"es-check": "^9.6.4",
|
|
32
33
|
"terser": "^5.49.0",
|
|
33
34
|
"tinyhmacmd5": "link:"
|
|
34
35
|
},
|
|
35
36
|
"scripts": {
|
|
36
|
-
"build": "node scripts/build.mjs",
|
|
37
|
-
"test": "
|
|
37
|
+
"build": "node scripts/build.mjs && es-check es2021 --module --checkFeatures main.js browser.min.js",
|
|
38
|
+
"test": "pnpm build && node scripts/test.mjs"
|
|
38
39
|
}
|
|
39
40
|
}
|