tinyhmacmd5 0.0.2 → 0.0.4
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 +4 -4
- package/browser.min.js +1 -1
- package/main.js +93 -172
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A tiny JavaScript HMAC-MD5 implementation.
|
|
4
4
|
|
|
5
|
-
The minified browser bundle ([`browser.min.js`](browser.min.js)) is only
|
|
5
|
+
The minified browser bundle ([`browser.min.js`](browser.min.js)) is only 1160 bytes.
|
|
6
6
|
|
|
7
7
|
## Setup
|
|
8
8
|
|
|
@@ -32,12 +32,12 @@ HMAC-MD5:
|
|
|
32
32
|
|
|
33
33
|
```js
|
|
34
34
|
// string to hex
|
|
35
|
-
// returns "
|
|
36
|
-
md5("Hello world!👋", "
|
|
35
|
+
// returns "c87fdc912df24da05a6e3fed927e9d89"
|
|
36
|
+
md5("Hello world!👋", "HMAC key 🔑")
|
|
37
37
|
|
|
38
38
|
// string to bytes
|
|
39
39
|
// returns Uint8Array<ArrayBuffer>
|
|
40
|
-
md5("Hello world!👋", "
|
|
40
|
+
md5("Hello world!👋", "HMAC key 🔑", true)
|
|
41
41
|
|
|
42
42
|
// bytes to hex
|
|
43
43
|
// returns "f881235eccf71ab49b937753a72ac673"
|
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,
|
|
2
|
+
{let r="length",e=16,n=2**32,t=Array,o=Math,a=o.floor,f=[(r,e,n)=>r&e|~r&n,(r,e,n)=>r&n|e&~n,(r,e,n)=>r^e^n,(r,e,n)=>e^(r|~n)],u=t(64),i=(i,l)=>{var d,s,v,c,g,h,y,w,A=0,b=1732584193,m=271733878,p=~b,x=~m,E=a((l+64)/512)*e+14,M=t(e),S=(r,e,t,a)=>0|((y=e+r+(y=E++*(7&3817>>3*h)+(7&328>>3*h)&15,h?M[y]:M[y]=0|i[A++])+(u[g++]??=0|n*o.abs(o.sin(g))))<<a|y>>>32-a)+t,T=(r,n,t,o)=>{for(w=f[h],E=0;E<e;)b=S(w(x,p,m),b,x,r),m=S(w(b,x,p),m,b,n),p=S(w(m,b,x),p,m,t),x=S(w(p,m,b),x,p,o);h++};for(i[a(l/32)]|=128<<l%32,i[E]=0|l,i[E+1]=0|l/n;A<i[r];m=0|m+c)d=b,s=x,v=p,c=m,g=h=0,T(7,12,17,22),T(5,9,14,20),T(4,11,e,23),T(6,10,15,21),b=0|b+d,x=0|x+s,p=0|p+v;return[b,x,p,m]},l=e=>{for(var n=e[r],t=[];n;)t[a(--n/4)]|=e[n]<<n%4*8;return t},d=r=>"string"==typeof r?(new TextEncoder).encode(r):r;var md5=(n,o,a)=>{var f,u=l(n=d(n)),s=8*n[r],v=e,c=new Uint8Array(e),g=r=>{for(u.unshift(...t(e));v;)u[--v]=r^f[v];v=e};for(null!=o&&((f=l(o=d(o)))[r]>e&&(f=i(f,8*o[r])),g(909522486),u=i(u,512+s),g(1549556828),s=640),u=i(u,s);v;)c[--v]=u[v>>2]>>>v%4*8;return a?c:c.reduce((r,n)=>r+(n>>4&&"")+n.toString(e),"")}}
|
package/main.js
CHANGED
|
@@ -8,94 +8,24 @@ let $length = "length"
|
|
|
8
8
|
/** @type {16} */
|
|
9
9
|
let $16 = 16
|
|
10
10
|
|
|
11
|
-
let
|
|
11
|
+
let $2_32 = 2 ** 32
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
* Add integers, wrapping at 2^32.
|
|
15
|
-
*
|
|
16
|
-
* @param {number} x First integer
|
|
17
|
-
* @param {number} y Second integer
|
|
18
|
-
* @returns {number} Sum
|
|
19
|
-
*/
|
|
20
|
-
let safeAdd = (x, y) => 0 | (0 | x) + (0 | y)
|
|
13
|
+
let $Array = Array
|
|
21
14
|
|
|
22
|
-
|
|
23
|
-
* Bitwise rotate a 32-bit number to the left.
|
|
24
|
-
*
|
|
25
|
-
* @param {number} num 32-bit number
|
|
26
|
-
* @param {number} cnt Rotation count
|
|
27
|
-
* @returns {number} Rotated number
|
|
28
|
-
*/
|
|
29
|
-
let bitRotateLeft = (num, cnt) => (num << cnt) | (num >>> (32 - cnt))
|
|
15
|
+
let $Math = Math
|
|
30
16
|
|
|
31
|
-
|
|
32
|
-
* Basic operation the algorithm uses.
|
|
33
|
-
*
|
|
34
|
-
* @param {number} q q
|
|
35
|
-
* @param {number} a a
|
|
36
|
-
* @param {number} b b
|
|
37
|
-
* @param {number} x x
|
|
38
|
-
* @param {number} s s
|
|
39
|
-
* @param {number} t t
|
|
40
|
-
* @returns {number} Result
|
|
41
|
-
*/
|
|
42
|
-
let md5cmn = (q, a, b, x, s, t) => safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b)
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Basic operation the algorithm uses.
|
|
46
|
-
*
|
|
47
|
-
* @param {number} a a
|
|
48
|
-
* @param {number} b b
|
|
49
|
-
* @param {number} c c
|
|
50
|
-
* @param {number} d d
|
|
51
|
-
* @param {number} x x
|
|
52
|
-
* @param {number} s s
|
|
53
|
-
* @param {number} t t
|
|
54
|
-
* @returns {number} Result
|
|
55
|
-
*/
|
|
56
|
-
let md5ff = (a, b, c, d, x, s, t) => md5cmn((b & c) | (~b & d), a, b, x, s, t)
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Basic operation the algorithm uses.
|
|
60
|
-
*
|
|
61
|
-
* @param {number} a a
|
|
62
|
-
* @param {number} b b
|
|
63
|
-
* @param {number} c c
|
|
64
|
-
* @param {number} d d
|
|
65
|
-
* @param {number} x x
|
|
66
|
-
* @param {number} s s
|
|
67
|
-
* @param {number} t t
|
|
68
|
-
* @returns {number} Result
|
|
69
|
-
*/
|
|
70
|
-
let md5gg = (a, b, c, d, x, s, t) => md5cmn((b & d) | (c & ~d), a, b, x, s, t)
|
|
17
|
+
let floor = $Math.floor
|
|
71
18
|
|
|
72
|
-
/**
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
* @param {number} x x
|
|
80
|
-
* @param {number} s s
|
|
81
|
-
* @param {number} t t
|
|
82
|
-
* @returns {number} Result
|
|
83
|
-
*/
|
|
84
|
-
let md5hh = (a, b, c, d, x, s, t) => md5cmn(b ^ c ^ d, a, b, x, s, t)
|
|
19
|
+
/** @type {((b:number, c:number, d:number) => number)[]} */
|
|
20
|
+
let ff = [
|
|
21
|
+
(b, c, d) => b & c | ~b & d,
|
|
22
|
+
(b, c, d) => b & d | c & ~d,
|
|
23
|
+
(b, c, d) => b ^ c ^ d,
|
|
24
|
+
(b, c, d) => c ^ (b | ~d)
|
|
25
|
+
]
|
|
85
26
|
|
|
86
|
-
/**
|
|
87
|
-
|
|
88
|
-
*
|
|
89
|
-
* @param {number} a a
|
|
90
|
-
* @param {number} b b
|
|
91
|
-
* @param {number} c c
|
|
92
|
-
* @param {number} d d
|
|
93
|
-
* @param {number} x x
|
|
94
|
-
* @param {number} s s
|
|
95
|
-
* @param {number} t t
|
|
96
|
-
* @returns {number} Result
|
|
97
|
-
*/
|
|
98
|
-
let md5ii = (a, b, c, d, x, s, t) => md5cmn(c ^ (b | ~d), a, b, x, s, t)
|
|
27
|
+
/** @type {number[]} MD5 constants cached in memory */
|
|
28
|
+
let K = $Array(64)
|
|
99
29
|
|
|
100
30
|
/**
|
|
101
31
|
* Calculate the MD5 of an array of little-endian words, and a bit length.
|
|
@@ -111,98 +41,82 @@ let binlMD5 = (x, bitLen) => {
|
|
|
111
41
|
, oldc
|
|
112
42
|
, oldd
|
|
113
43
|
, a = 1732584193
|
|
114
|
-
, b = -271733879
|
|
115
|
-
, c = -1732584194
|
|
116
44
|
, d = 271733878
|
|
117
|
-
,
|
|
45
|
+
, c = ~a
|
|
46
|
+
, b = ~d
|
|
47
|
+
, j = floor((bitLen + 64) / 512) * $16 + 14
|
|
48
|
+
|
|
49
|
+
/** @type {number} */
|
|
50
|
+
var k_i
|
|
51
|
+
|
|
52
|
+
/** @type {number} */
|
|
53
|
+
var ff_i
|
|
54
|
+
|
|
55
|
+
/** @type {number} */
|
|
56
|
+
var cmn_t
|
|
57
|
+
|
|
58
|
+
/** @type {number[]} */
|
|
59
|
+
var w = $Array($16)
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @param {number} q
|
|
63
|
+
* @param {number} a
|
|
64
|
+
* @param {number} b
|
|
65
|
+
* @param {number} s
|
|
66
|
+
* @returns {number}
|
|
67
|
+
*/
|
|
68
|
+
var cmn = (q, a, b, s) => (
|
|
69
|
+
cmn_t = (
|
|
70
|
+
a + q +
|
|
71
|
+
(
|
|
72
|
+
cmn_t = j++ * (7 & 0o7351 >> ff_i * 3) + (7 & 0o0510 >> ff_i * 3) & 15,
|
|
73
|
+
ff_i ? w[cmn_t] : w[cmn_t] = 0 | x[i++]
|
|
74
|
+
) +
|
|
75
|
+
(K[k_i++] ??= 0 | $2_32 * $Math.abs($Math.sin(k_i)))
|
|
76
|
+
),
|
|
77
|
+
0 | (cmn_t << s | cmn_t >>> 32 - s) + b
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
/** @type {typeof ff[0]} */
|
|
81
|
+
var F_f
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @param {number} s0
|
|
85
|
+
* @param {number} s1
|
|
86
|
+
* @param {number} s2
|
|
87
|
+
* @param {number} s3
|
|
88
|
+
*/
|
|
89
|
+
var F = (s0, s1, s2, s3) => {
|
|
90
|
+
for (F_f = ff[ff_i], j = 0; j < $16;) {
|
|
91
|
+
a = cmn(F_f(b, c, d), a, b, s0)
|
|
92
|
+
d = cmn(F_f(a, b, c), d, a, s1)
|
|
93
|
+
c = cmn(F_f(d, a, b), c, d, s2)
|
|
94
|
+
b = cmn(F_f(c, d, a), b, c, s3)
|
|
95
|
+
}
|
|
96
|
+
ff_i++
|
|
97
|
+
}
|
|
118
98
|
|
|
119
99
|
// append padding
|
|
120
100
|
x[floor(bitLen / 32)] |= 0x80 << bitLen % 32;
|
|
121
|
-
x[
|
|
122
|
-
x[
|
|
123
|
-
|
|
124
|
-
for (; i < x[$length]; d = safeAdd(d, oldd)) {
|
|
125
|
-
let [
|
|
126
|
-
x0, x1, x2, x3, x4, x5, x6, x7,
|
|
127
|
-
x8, x9, x10, x11, x12, x13, x14, x15
|
|
128
|
-
] = x.slice(i, i += $16)
|
|
101
|
+
x[j] = 0 | bitLen;
|
|
102
|
+
x[j + 1] = 0 | bitLen / $2_32;
|
|
129
103
|
|
|
104
|
+
for (; i < x[$length]; d = 0 | d + oldd) {
|
|
130
105
|
olda = a
|
|
131
106
|
oldb = b
|
|
132
107
|
oldc = c
|
|
133
108
|
oldd = d
|
|
134
109
|
|
|
135
|
-
|
|
136
|
-
d = md5ff(d, a, b, c, x1, 12, -389564586)
|
|
137
|
-
c = md5ff(c, d, a, b, x2, 17, 606105819)
|
|
138
|
-
b = md5ff(b, c, d, a, x3, 22, -1044525330)
|
|
139
|
-
a = md5ff(a, b, c, d, x4, 7, -176418897)
|
|
140
|
-
d = md5ff(d, a, b, c, x5, 12, 1200080426)
|
|
141
|
-
c = md5ff(c, d, a, b, x6, 17, -1473231341)
|
|
142
|
-
b = md5ff(b, c, d, a, x7, 22, -45705983)
|
|
143
|
-
a = md5ff(a, b, c, d, x8, 7, 1770035416)
|
|
144
|
-
d = md5ff(d, a, b, c, x9, 12, -1958414417)
|
|
145
|
-
c = md5ff(c, d, a, b, x10, 17, -42063)
|
|
146
|
-
b = md5ff(b, c, d, a, x11, 22, -1990404162)
|
|
147
|
-
a = md5ff(a, b, c, d, x12, 7, 1804603682)
|
|
148
|
-
d = md5ff(d, a, b, c, x13, 12, -40341101)
|
|
149
|
-
c = md5ff(c, d, a, b, x14, 17, -1502002290)
|
|
150
|
-
b = md5ff(b, c, d, a, x15, 22, 1236535329)
|
|
151
|
-
|
|
152
|
-
a = md5gg(a, b, c, d, x1, 5, -165796510)
|
|
153
|
-
d = md5gg(d, a, b, c, x6, 9, -1069501632)
|
|
154
|
-
c = md5gg(c, d, a, b, x11, 14, 643717713)
|
|
155
|
-
b = md5gg(b, c, d, a, x0, 20, -373897302)
|
|
156
|
-
a = md5gg(a, b, c, d, x5, 5, -701558691)
|
|
157
|
-
d = md5gg(d, a, b, c, x10, 9, 38016083)
|
|
158
|
-
c = md5gg(c, d, a, b, x15, 14, -660478335)
|
|
159
|
-
b = md5gg(b, c, d, a, x4, 20, -405537848)
|
|
160
|
-
a = md5gg(a, b, c, d, x9, 5, 568446438)
|
|
161
|
-
d = md5gg(d, a, b, c, x14, 9, -1019803690)
|
|
162
|
-
c = md5gg(c, d, a, b, x3, 14, -187363961)
|
|
163
|
-
b = md5gg(b, c, d, a, x8, 20, 1163531501)
|
|
164
|
-
a = md5gg(a, b, c, d, x13, 5, -1444681467)
|
|
165
|
-
d = md5gg(d, a, b, c, x2, 9, -51403784)
|
|
166
|
-
c = md5gg(c, d, a, b, x7, 14, 1735328473)
|
|
167
|
-
b = md5gg(b, c, d, a, x12, 20, -1926607734)
|
|
110
|
+
k_i = ff_i = 0
|
|
168
111
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
a = md5hh(a, b, c, d, x1, 4, -1530992060)
|
|
174
|
-
d = md5hh(d, a, b, c, x4, 11, 1272893353)
|
|
175
|
-
c = md5hh(c, d, a, b, x7, $16, -155497632)
|
|
176
|
-
b = md5hh(b, c, d, a, x10, 23, -1094730640)
|
|
177
|
-
a = md5hh(a, b, c, d, x13, 4, 681279174)
|
|
178
|
-
d = md5hh(d, a, b, c, x0, 11, -358537222)
|
|
179
|
-
c = md5hh(c, d, a, b, x3, $16, -722521979)
|
|
180
|
-
b = md5hh(b, c, d, a, x6, 23, 76029189)
|
|
181
|
-
a = md5hh(a, b, c, d, x9, 4, -640364487)
|
|
182
|
-
d = md5hh(d, a, b, c, x12, 11, -421815835)
|
|
183
|
-
c = md5hh(c, d, a, b, x15, $16, 530742520)
|
|
184
|
-
b = md5hh(b, c, d, a, x2, 23, -995338651)
|
|
112
|
+
F(7, 12, 17, 22)
|
|
113
|
+
F(5, 9, 14, 20)
|
|
114
|
+
F(4, 11, $16, 23)
|
|
115
|
+
F(6, 10, 15, 21)
|
|
185
116
|
|
|
186
|
-
a =
|
|
187
|
-
|
|
188
|
-
c =
|
|
189
|
-
b = md5ii(b, c, d, a, x5, 21, -57434055)
|
|
190
|
-
a = md5ii(a, b, c, d, x12, 6, 1700485571)
|
|
191
|
-
d = md5ii(d, a, b, c, x3, 10, -1894986606)
|
|
192
|
-
c = md5ii(c, d, a, b, x10, 15, -1051523)
|
|
193
|
-
b = md5ii(b, c, d, a, x1, 21, -2054922799)
|
|
194
|
-
a = md5ii(a, b, c, d, x8, 6, 1873313359)
|
|
195
|
-
d = md5ii(d, a, b, c, x15, 10, -30611744)
|
|
196
|
-
c = md5ii(c, d, a, b, x6, 15, -1560198380)
|
|
197
|
-
b = md5ii(b, c, d, a, x13, 21, 1309151649)
|
|
198
|
-
a = md5ii(a, b, c, d, x4, 6, -145523070)
|
|
199
|
-
d = md5ii(d, a, b, c, x11, 10, -1120210379)
|
|
200
|
-
c = md5ii(c, d, a, b, x2, 15, 718787259)
|
|
201
|
-
b = md5ii(b, c, d, a, x9, 21, -343485551)
|
|
202
|
-
|
|
203
|
-
a = safeAdd(a, olda)
|
|
204
|
-
b = safeAdd(b, oldb)
|
|
205
|
-
c = safeAdd(c, oldc)
|
|
117
|
+
a = 0 | a + olda
|
|
118
|
+
b = 0 | b + oldb
|
|
119
|
+
c = 0 | c + oldc
|
|
206
120
|
}
|
|
207
121
|
return [a, b, c, d]
|
|
208
122
|
}
|
|
@@ -262,19 +176,26 @@ var md5 = (data, key, raw) => {
|
|
|
262
176
|
var bdata = bytesToBinl(data = inputToBytes(data))
|
|
263
177
|
, bitLen = data[$length] * 8
|
|
264
178
|
, i = $16
|
|
265
|
-
, out = new Uint8Array(
|
|
179
|
+
, out = new Uint8Array($16)
|
|
180
|
+
/** @type {*} */
|
|
181
|
+
var bkey
|
|
182
|
+
, pad = (/**@type {number}*/ x) => {
|
|
183
|
+
bdata.unshift(...$Array($16))
|
|
184
|
+
for (; i;) {
|
|
185
|
+
bdata[--i] = x ^ bkey[i]
|
|
186
|
+
}
|
|
187
|
+
i = $16
|
|
188
|
+
}
|
|
266
189
|
|
|
267
190
|
if (key != null) {
|
|
268
191
|
// HMAC
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
,
|
|
272
|
-
, unshift = (/**@type{*}*/ _) => bdata.unshift(...opad)
|
|
273
|
-
unshift(bkey[$length] > $16 && (bkey = binlMD5(bkey, key[$length] * 8)))
|
|
274
|
-
for (; i; opad[i] = bkey[i] ^ 0x5c5c5c5c) {
|
|
275
|
-
bdata[--i] = bkey[i] ^ 0x36363636
|
|
192
|
+
bkey = bytesToBinl(key = inputToBytes(key))
|
|
193
|
+
if (bkey[$length] > $16) {
|
|
194
|
+
bkey = binlMD5(bkey, key[$length] * 8)
|
|
276
195
|
}
|
|
277
|
-
|
|
196
|
+
pad(0x36363636)
|
|
197
|
+
bdata = binlMD5(bdata, 512 + bitLen)
|
|
198
|
+
pad(0x5c5c5c5c)
|
|
278
199
|
bitLen = 512 + 128
|
|
279
200
|
}
|
|
280
201
|
|
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.4",
|
|
4
|
+
"description": "A tiny JavaScript HMAC-MD5 implementation. The minified browser bundle is only 1160 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
|
}
|