tinyhmacmd5 0.2.3 → 0.3.0
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 +29 -2
- package/browser.min.js +1 -1
- package/main.js +47 -50
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@ English | [中文](README-zh.md)
|
|
|
2
2
|
|
|
3
3
|
# Tiny HMAC MD5
|
|
4
4
|
|
|
5
|
-
A tiny and reliable HMAC-MD5 implementation for JavaScript: [`browser.min.js`](browser.min.js) is only **
|
|
5
|
+
A tiny and reliable HMAC-MD5 implementation for JavaScript: [`browser.min.js`](browser.min.js) is only **995 bytes**.
|
|
6
6
|
|
|
7
7
|
- **Input type**: `string` (UTF‑8), `Uint8Array` or `Uint8ClampedArray`
|
|
8
8
|
- **Output type**: hex `string` or bytes `Uint8Array`
|
|
@@ -163,6 +163,33 @@ Not recommended for use in environments that support `node:crypto`, as `node:cry
|
|
|
163
163
|
|
|
164
164
|
---
|
|
165
165
|
|
|
166
|
+
## Benchmark
|
|
167
|
+
|
|
168
|
+
```
|
|
169
|
+
pnpm benchmark
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
Generating 100MiB string data (this might take a moment)...
|
|
174
|
+
Data length: 104857600 chars (100MiB)
|
|
175
|
+
--------------------------------------------------
|
|
176
|
+
tinyhmacmd5 : 718.27 ms
|
|
177
|
+
js-md5 : 141.43 ms
|
|
178
|
+
blueimp-md5 : 3934.98 ms
|
|
179
|
+
crypto-js : 1732.55 ms
|
|
180
|
+
native crypto : 136.60 ms
|
|
181
|
+
--------------------------------------------------
|
|
182
|
+
✅ All pure JS implementations match native crypto result.
|
|
183
|
+
|
|
184
|
+
--- Pure 513MiB Test (No prior small tests) ---
|
|
185
|
+
tinyhmacmd5 HMAC-MD5 timer: 3.465s
|
|
186
|
+
node:crypto HMAC-MD5 timer: 572.686ms
|
|
187
|
+
tinyhmacmd5 MD5 timer: 3.451s
|
|
188
|
+
node:crypto MD5 timer: 571.58ms
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
166
193
|
## Why I Made This Project
|
|
167
194
|
|
|
168
195
|
I initially used HMAC-MD5 just to call a certain website's API. That site signs the request body with `crypto-js`'s HMAC-MD5 to make reverse engineering harder.
|
|
@@ -179,7 +206,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
|
|
|
179
206
|
|
|
180
207
|
I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
|
|
181
208
|
|
|
182
|
-
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (
|
|
209
|
+
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (995 bytes) while also improving reliability.
|
|
183
210
|
|
|
184
211
|
Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
|
|
185
212
|
|
package/browser.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
/*! npmjs.com/tinyhmacmd5 */{let r=
|
|
1
|
+
/*! npmjs.com/tinyhmacmd5 */{let r=Math,e=r.floor,n=Int32Array,t=new n(64).map((e,n)=>2**32*r.abs(r.sin(n+1))),o=r=>16*e((r+72)/64),f=(r,o,f=0,a,l=1732584193,w=271733878,i,d,g=r.length,h=n.of(l,~w,~l,w),s=new n(h))=>{for(r[g-1]=o/2**29,r[e(o/4)]|=128<<(r[g-2]=o<<3);f<g;f+=16){for(o=0;o<16;o+=4)for(a=0;a<16;)h[d&=3]=((w=h[d]+t[4*o|a]+(l=h[3&++d],w=h[3&++d],i=h[3&++d],o?o>4?o>8?w^(l|~i):l^w^i:l&i|w&~i:l&w|~l&i)+r[f+(a*(29521>>o)+(1296>>o)&15)])<<(i="',16%).4$+07&*/5".charCodeAt(3&a++|o))|w>>>64-i)+l;for(;d;)s[--d]=h[d]+=s[d]}return h},a=(r,e,t=("string"==typeof r?r=(new TextEncoder).encode(r):r).length,f=new n(o(4*e+t)),a=0)=>{for(;a<t;)f[e++]=r[a++]|r[a++]<<8|r[a++]<<16|r[a++]<<24;return[f,t]};var md5=(r,e,t)=>{var o=16,l=null!=e,[w,i]=a(r,l*o),d=t?new Uint8Array(o):"";if(l){let[r,t]=a(e,0),l=new n(32);for(t>64&&(r=f(r,t)),t=o;t;)l[--t]=1785358954^(w[t]=909522486^r[t]);l.set(f(w,64+i),16),w=l,i=80}for(w=f(w,i);o;t?d[o]=i:d=(i>>4&&"")+i.toString(16)+d)i=w[--o>>2]>>8*o&255;return d}}
|
package/main.js
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
// Adapted from https://github.com/blueimp/JavaScript-MD5
|
|
4
4
|
|
|
5
|
-
/** @type {number[]} MD5 constants cached in memory */
|
|
6
|
-
let K = []
|
|
7
|
-
|
|
8
|
-
let i = 0
|
|
9
|
-
|
|
10
5
|
let $Math = Math
|
|
11
6
|
|
|
12
7
|
let floor = $Math.floor
|
|
13
8
|
|
|
9
|
+
let $Int32Array = Int32Array
|
|
10
|
+
|
|
11
|
+
/** MD5 constants cached in memory */
|
|
12
|
+
let K = new $Int32Array(64).map((v, i) => 2 ** 32 * $Math.abs($Math.sin(i + 1)))
|
|
13
|
+
|
|
14
14
|
/** @param {number} byteLen */
|
|
15
15
|
// `byteLen` may be >= 2**32, so cannot use `>>>` as a replacement for `floor`
|
|
16
16
|
let wordsLen = (byteLen) => floor((byteLen + 72) / 64) * 16
|
|
@@ -18,64 +18,64 @@ let wordsLen = (byteLen) => floor((byteLen + 72) / 64) * 16
|
|
|
18
18
|
/**
|
|
19
19
|
* Calculate the MD5 of an array of little-endian words, and a byte length.
|
|
20
20
|
*
|
|
21
|
-
* @param {Int32Array
|
|
21
|
+
* @param {Int32Array<ArrayBuffer>} x little-endian words
|
|
22
22
|
* @param {number} l Byte length
|
|
23
23
|
*
|
|
24
|
+
* @param {number} [j]
|
|
24
25
|
* @param {number} [t2]
|
|
25
26
|
* @param {number} [oi]
|
|
26
27
|
*
|
|
27
|
-
* @returns {
|
|
28
|
+
* @returns {Int32Array<ArrayBuffer>} MD5 Array
|
|
28
29
|
*/
|
|
29
30
|
let wordsMD5 = (
|
|
30
31
|
x,
|
|
31
32
|
l,
|
|
32
33
|
// var:
|
|
33
34
|
i = 0,
|
|
34
|
-
j
|
|
35
|
+
j,
|
|
35
36
|
t0 = 1732584193,
|
|
36
37
|
t1 = 271733878,
|
|
37
38
|
t2,
|
|
38
39
|
oi,
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
xLen = x.length,
|
|
41
|
+
output = $Int32Array.of(t0, ~t1, ~t0, t1),
|
|
42
|
+
oldOutput = new $Int32Array(output),
|
|
41
43
|
) => {
|
|
42
44
|
// append padding
|
|
43
45
|
// `l` may be >= 2**32, so cannot use `>>>` as a replacement for `floor`
|
|
44
|
-
x[
|
|
45
|
-
x[floor(l / 4)] |= 0x80 << (x[
|
|
46
|
-
|
|
47
|
-
for (; i <
|
|
48
|
-
for (
|
|
49
|
-
|
|
50
|
-
(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
? l >
|
|
61
|
-
?
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
46
|
+
x[xLen - 1] = l / 2 ** 29;
|
|
47
|
+
x[floor(l / 4)] |= 0x80 << (x[xLen - 2] = l << 3);
|
|
48
|
+
|
|
49
|
+
for (; i < xLen; i += 16) {
|
|
50
|
+
for (l = 0; l < 16; l += 4) {
|
|
51
|
+
for (j = 0; j < 16;) {
|
|
52
|
+
output[oi &= 3] = (
|
|
53
|
+
(
|
|
54
|
+
t1 = (
|
|
55
|
+
output[oi] +
|
|
56
|
+
K[l * 4 | j] +
|
|
57
|
+
(
|
|
58
|
+
t0 = output[++oi & 3],
|
|
59
|
+
t1 = output[++oi & 3],
|
|
60
|
+
t2 = output[++oi & 3],
|
|
61
|
+
l
|
|
62
|
+
? l > 4
|
|
63
|
+
? l > 8
|
|
64
|
+
? t1 ^ (t0 | ~t2) // Round 4: I
|
|
65
|
+
: t0 ^ t1 ^ t2 // Round 3: H
|
|
66
|
+
: t0 & t2 | t1 & ~t2 // Round 2: G
|
|
67
|
+
: t0 & t1 | ~t0 & t2 // Round 1: F
|
|
68
|
+
) +
|
|
69
|
+
x[i + (j * (0x7351 >> l) + (0x0510 >> l) & 15)]
|
|
68
70
|
)
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
) {
|
|
75
|
-
l = j >> 4 << 2
|
|
71
|
+
) << (
|
|
72
|
+
t2 = "',16%).4$+07&*/5".charCodeAt(j++ & 3 | l)
|
|
73
|
+
) | t1 >>> 64 - t2
|
|
74
|
+
) + t0
|
|
75
|
+
}
|
|
76
76
|
}
|
|
77
77
|
for (; oi;) {
|
|
78
|
-
oldOutput[--oi] = output[oi]
|
|
78
|
+
oldOutput[--oi] = output[oi] += oldOutput[oi]
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
return output
|
|
@@ -87,7 +87,7 @@ let wordsMD5 = (
|
|
|
87
87
|
* @param {*} input
|
|
88
88
|
* @param {number} j pad int32 length
|
|
89
89
|
*
|
|
90
|
-
* @returns {[Int32Array<ArrayBuffer
|
|
90
|
+
* @returns {[Int32Array<ArrayBuffer>, number]}
|
|
91
91
|
*/
|
|
92
92
|
let inputToWords = (
|
|
93
93
|
input,
|
|
@@ -100,7 +100,7 @@ let inputToWords = (
|
|
|
100
100
|
).length,
|
|
101
101
|
// Using `Array` to process inputs over 512 MiB could throw a `RangeError`,
|
|
102
102
|
// so I replaced it with `Int32Array`.
|
|
103
|
-
output = new Int32Array(wordsLen(j * 4 + byteLen)),
|
|
103
|
+
output = new $Int32Array(wordsLen(j * 4 + byteLen)),
|
|
104
104
|
i = 0,
|
|
105
105
|
) => {
|
|
106
106
|
for (; i < byteLen;) {
|
|
@@ -136,7 +136,7 @@ var md5 = (data, key, raw) => {
|
|
|
136
136
|
if (hasKey) {
|
|
137
137
|
// HMAC
|
|
138
138
|
let [bkey, j] = inputToWords(key, 0)
|
|
139
|
-
, opad =
|
|
139
|
+
, opad = new $Int32Array(32)
|
|
140
140
|
if (j > 64) {
|
|
141
141
|
bkey = wordsMD5(bkey, j)
|
|
142
142
|
}
|
|
@@ -144,7 +144,8 @@ var md5 = (data, key, raw) => {
|
|
|
144
144
|
// (0x36363636 ^ 0x5c5c5c5c) == 0x6a6a6a6a
|
|
145
145
|
opad[--j] = 0x6a6a6a6a ^ (bdata[j] = 0x36363636 ^ bkey[j])
|
|
146
146
|
}
|
|
147
|
-
|
|
147
|
+
opad.set(wordsMD5(bdata, 64 + temp), 16)
|
|
148
|
+
bdata = opad
|
|
148
149
|
temp = 80
|
|
149
150
|
}
|
|
150
151
|
|
|
@@ -162,8 +163,4 @@ var md5 = (data, key, raw) => {
|
|
|
162
163
|
return out
|
|
163
164
|
}
|
|
164
165
|
|
|
165
|
-
for (; i < 64;) {
|
|
166
|
-
K[i] = 0 | 2 ** 32 * $Math.abs($Math.sin(++i))
|
|
167
|
-
}
|
|
168
|
-
|
|
169
166
|
export default md5
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tinyhmacmd5",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 995 bytes.",
|
|
5
5
|
"author": "bddjr",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
@@ -54,7 +54,10 @@
|
|
|
54
54
|
"url": "git+https://github.com/bddjr/tinyhmacmd5.git"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
+
"blueimp-md5": "^2.19.0",
|
|
58
|
+
"crypto-js": "^4.2.0",
|
|
57
59
|
"es-check": "^9.6.4",
|
|
60
|
+
"js-md5": "^0.9.2",
|
|
58
61
|
"terser": "^5.49.0",
|
|
59
62
|
"tinyhmacmd5": "link:",
|
|
60
63
|
"typescript": "^7.0.2"
|
|
@@ -63,7 +66,8 @@
|
|
|
63
66
|
"build": "tsc && node scripts/build.mjs && es-check es2016 --module --checkFeatures main.js browser.min.js",
|
|
64
67
|
"build:unchanged": "node scripts/build-unchanged.mjs",
|
|
65
68
|
"test": "pnpm build && pnpm test:only",
|
|
66
|
-
"test:only": "node scripts/test.mjs",
|
|
67
|
-
"test:unchanged": "pnpm build:unchanged && pnpm test:only"
|
|
69
|
+
"test:only": "node scripts/test.mjs && node scripts/test-513MiB.mjs",
|
|
70
|
+
"test:unchanged": "pnpm build:unchanged && pnpm test:only",
|
|
71
|
+
"benchmark": "node scripts/benchmark.mjs && node scripts/test-513MiB.mjs"
|
|
68
72
|
}
|
|
69
73
|
}
|