tinyhmacmd5 0.3.3 → 0.4.0-es2026
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 +27 -31
- package/browser.min.js +1 -1
- package/main.js +32 -29
- package/package.json +3 -3
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 **989 bytes**.
|
|
6
6
|
|
|
7
7
|
- **Input type**: `string` (UTF‑8), `Uint8Array` or `Uint8ClampedArray`
|
|
8
8
|
- **Output type**: hex `string` or bytes `Uint8Array`
|
|
@@ -10,14 +10,10 @@ A tiny and reliable HMAC-MD5 implementation for JavaScript: [`browser.min.js`](b
|
|
|
10
10
|
- **TypeScript‑ready**: [`main.d.ts`](main.d.ts)
|
|
11
11
|
- **0 dependencies**
|
|
12
12
|
|
|
13
|
-
**Live demo**: https://bddjr.github.io/tinyhmacmd5/
|
|
14
|
-
|
|
15
13
|
`tinyhmacmd5` does not simply aim for the smallest possible size.
|
|
16
14
|
Its goal is to balance code size and runtime performance, so some performance-oriented implementations are intentionally retained.
|
|
17
15
|
As a result, the final size is not the smallest theoretically achievable.
|
|
18
16
|
|
|
19
|
-
If you need a smaller implementation that targets ECMAScript 2026, see the [`es2026`](https://github.com/bddjr/tinyhmacmd5/tree/es2026) branch.
|
|
20
|
-
|
|
21
17
|
> [!WARNING]
|
|
22
18
|
> MD5 is cryptographically broken and unsafe for security-sensitive applications.
|
|
23
19
|
> Do not rely on it for password hashing, digital signatures, or certificate verification.
|
|
@@ -27,7 +23,7 @@ If you need a smaller implementation that targets ECMAScript 2026, see the [`es2
|
|
|
27
23
|
### npm
|
|
28
24
|
|
|
29
25
|
```
|
|
30
|
-
npm i tinyhmacmd5
|
|
26
|
+
npm i tinyhmacmd5@es2026
|
|
31
27
|
```
|
|
32
28
|
|
|
33
29
|
```js
|
|
@@ -43,7 +39,7 @@ You can also use other package managers (e.g. `pnpm` or `yarn`) in place of `npm
|
|
|
43
39
|
See https://www.jsdelivr.com/package/npm/tinyhmacmd5
|
|
44
40
|
|
|
45
41
|
```html
|
|
46
|
-
<script src="https://cdn.jsdelivr.net/npm/tinyhmacmd5"></script>
|
|
42
|
+
<script src="https://cdn.jsdelivr.net/npm/tinyhmacmd5@es2026"></script>
|
|
47
43
|
```
|
|
48
44
|
|
|
49
45
|
It will define the `md5` function using `var`.
|
|
@@ -51,7 +47,7 @@ It will define the `md5` function using `var`.
|
|
|
51
47
|
### UNPKG
|
|
52
48
|
|
|
53
49
|
```html
|
|
54
|
-
<script src="https://unpkg.com/tinyhmacmd5"></script>
|
|
50
|
+
<script src="https://unpkg.com/tinyhmacmd5@es2026"></script>
|
|
55
51
|
```
|
|
56
52
|
|
|
57
53
|
It will define the `md5` function using `var`.
|
|
@@ -142,22 +138,22 @@ md5(data)
|
|
|
142
138
|
|
|
143
139
|
## Runtime Environment
|
|
144
140
|
|
|
145
|
-
Environments that support [
|
|
141
|
+
Environments that support [`Uint8Array.prototype.toHex()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toHex) :
|
|
146
142
|
|
|
147
143
|
- Desktop
|
|
148
|
-
- Chrome ≥
|
|
149
|
-
- Edge ≥
|
|
150
|
-
- Firefox ≥
|
|
151
|
-
- Opera ≥
|
|
152
|
-
- Safari ≥
|
|
144
|
+
- Chrome ≥ 140 (2025-09-02)
|
|
145
|
+
- Edge ≥ 140 (2025-09-05)
|
|
146
|
+
- Firefox ≥ 133 (2024-11-26)
|
|
147
|
+
- Opera ≥ 124 (2025-11-13)
|
|
148
|
+
- Safari ≥ 18.2 (2024-12-11)
|
|
153
149
|
- Mobile
|
|
154
|
-
- Chrome Android ≥
|
|
155
|
-
- Firefox for Android ≥
|
|
156
|
-
- Opera Android ≥
|
|
157
|
-
- Safari on iOS ≥
|
|
158
|
-
- Samsung Browser
|
|
159
|
-
- WebView Android ≥
|
|
160
|
-
- WebView on iOS ≥
|
|
150
|
+
- Chrome Android ≥ 140 (2025-09-02)
|
|
151
|
+
- Firefox for Android ≥ 133 (2024-11-26)
|
|
152
|
+
- Opera Android ≥ 92 (2025-10-08)
|
|
153
|
+
- Safari on iOS ≥ 18.2 (2024-12-11)
|
|
154
|
+
- Samsung Browser ×
|
|
155
|
+
- WebView Android ≥ 140 (2025-09-02)
|
|
156
|
+
- WebView on iOS ≥ 18.2 (2024-12-11)
|
|
161
157
|
|
|
162
158
|
Not recommended for use in environments that support `node:crypto`, as `node:crypto` already provides HMAC-MD5.
|
|
163
159
|
|
|
@@ -172,19 +168,19 @@ pnpm benchmark
|
|
|
172
168
|
```
|
|
173
169
|
Data length: 104857600 chars (100MiB)
|
|
174
170
|
--------------------------------------------------
|
|
175
|
-
tinyhmacmd5 :
|
|
176
|
-
js-md5 :
|
|
177
|
-
blueimp-md5 :
|
|
178
|
-
crypto-js :
|
|
179
|
-
node:crypto : 130.
|
|
171
|
+
tinyhmacmd5 : 569.00 ms
|
|
172
|
+
js-md5 : 130.30 ms
|
|
173
|
+
blueimp-md5 : 3851.57 ms
|
|
174
|
+
crypto-js : 1561.18 ms
|
|
175
|
+
node:crypto : 130.29 ms
|
|
180
176
|
--------------------------------------------------
|
|
181
177
|
✅ All pure JS implementations match node:crypto result.
|
|
182
178
|
|
|
183
179
|
--- Pure 513MiB Test (No prior small tests) ---
|
|
184
|
-
tinyhmacmd5 HMAC-MD5 timer:
|
|
185
|
-
node:crypto HMAC-MD5 timer: 552.
|
|
186
|
-
tinyhmacmd5 MD5 timer:
|
|
187
|
-
node:crypto MD5 timer:
|
|
180
|
+
tinyhmacmd5 HMAC-MD5 timer: 2.796s
|
|
181
|
+
node:crypto HMAC-MD5 timer: 552.718ms
|
|
182
|
+
tinyhmacmd5 MD5 timer: 2.775s
|
|
183
|
+
node:crypto MD5 timer: 551.291ms
|
|
188
184
|
```
|
|
189
185
|
|
|
190
186
|
CPU: i5-10600KF
|
|
@@ -210,7 +206,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
|
|
|
210
206
|
|
|
211
207
|
I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
|
|
212
208
|
|
|
213
|
-
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 (989 bytes) while also improving reliability.
|
|
214
210
|
|
|
215
211
|
Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
|
|
216
212
|
|
package/browser.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
/*! npmjs.com/tinyhmacmd5 */{let
|
|
1
|
+
/*! npmjs.com/tinyhmacmd5 */{let e=Math,r=Int32Array,n=Uint8Array,t=new r(64).map((r,n)=>2**32*e.abs(e.sin(n+1))),f=new n(t.buffer)[28]<2,o=(n,f,o=0,a,l=1732584193,w=271733878,u,i,s=n.length,c=r.of(l,~w,~l,w),d=new r(c))=>{for(n[s-1]=f/2**29,n[e.floor(f/4)]|=128<<(n[s-2]=f<<3);o<s;o+=16){for(f=0;f<16;f+=4)for(a=0;a<16;)c[i&=3]=((w=c[i]+t[4*f+a]+(l=c[3&++i],w=c[3&++i],u=c[3&++i],w^(f>4?f>8?l|~u:l^u:f?u&(l^w):~l&(w^u)))+n[o+(a*(29521>>f)+(1296>>f)&15)])<<(u="',16%).4$+07&*/5".charCodeAt(3&a++|f))|w>>>32-u)+l;for(;i;)d[--i]=c[i]+=d[i]}return c},a=(t,o,a=("string"==typeof t?t=(new TextEncoder).encode(t):t).length,l=new r(o+16*e.ceil((a+9)/64)),w=new n(l.buffer),u)=>{for(f?w.set(t,4*o):u=0;u<a;3&++u||o++)l[o]|=t[u]<<8*u;return[l,a]};var md5=(e,t,f)=>{var l=16,w=null!=t,[u,i]=a(e,w*l),s=new n(l);if(w){let[e,n]=a(t,0),f=new r(32);for(n>64&&(e=o(e,n)),n=l;n;)f[--n]=1785358954^(u[n]=909522486^e[n]);f.set(o(u,64+i),l),u=f,i=80}for(u=o(u,i);l;)s[--l]=u[l>>2]>>8*l;return f?s:s.toHex()}}
|
package/main.js
CHANGED
|
@@ -6,9 +6,14 @@ let $Math = Math
|
|
|
6
6
|
|
|
7
7
|
let $Int32Array = Int32Array
|
|
8
8
|
|
|
9
|
+
let $Uint8Array = Uint8Array
|
|
10
|
+
|
|
9
11
|
/** MD5 constants cached in memory */
|
|
10
12
|
let K = new $Int32Array(64).map((v, i) => 2 ** 32 * $Math.abs($Math.sin(i + 1)))
|
|
11
13
|
|
|
14
|
+
/** is little-endian */
|
|
15
|
+
let isLE = new $Uint8Array(K.buffer)[28] < 2
|
|
16
|
+
|
|
12
17
|
/**
|
|
13
18
|
* Calculate the MD5 of an array of little-endian words, and a byte length.
|
|
14
19
|
*
|
|
@@ -36,7 +41,8 @@ let wordsMD5 = (
|
|
|
36
41
|
oldOutput = new $Int32Array(output),
|
|
37
42
|
) => {
|
|
38
43
|
// append padding
|
|
39
|
-
// `l` may be >= 2**32, so cannot use `>>>` as a replacement for `floor
|
|
44
|
+
// `l` may be >= 2**32, so cannot use `>>>` as a replacement for `floor`.
|
|
45
|
+
// `l << 3` cannot be changed to `l * 8`, as it would trigger JIT deoptimization on large inputs.
|
|
40
46
|
x[xLen - 1] = l / 2 ** 29;
|
|
41
47
|
x[$Math.floor(l / 4)] |= 0x80 << (x[xLen - 2] = l << 3);
|
|
42
48
|
|
|
@@ -52,20 +58,20 @@ let wordsMD5 = (
|
|
|
52
58
|
t0 = output[++oi & 3],
|
|
53
59
|
t1 = output[++oi & 3],
|
|
54
60
|
t2 = output[++oi & 3],
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
l >
|
|
58
|
-
?
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
t1 ^ (
|
|
62
|
+
l > 4
|
|
63
|
+
? l > 8
|
|
64
|
+
? t0 | ~t2 // Round 4: I
|
|
65
|
+
: t0 ^ t2 // Round 3: H
|
|
66
|
+
: l
|
|
67
|
+
? t2 & (t0 ^ t1) // Round 2: G
|
|
68
|
+
: ~t0 & (t1 ^ t2) // Round 1: F
|
|
69
|
+
)
|
|
64
70
|
) +
|
|
65
71
|
x[i + (j * (0x7351 >> l) + (0x0510 >> l) & 15)]
|
|
66
72
|
)
|
|
67
73
|
) << (
|
|
68
|
-
t2 = "',16%).4$+07&*/5".charCodeAt(j++ & 3 | l)
|
|
74
|
+
t2 = "',16%).4$+07&*/5".charCodeAt(j++ & 3 | l)
|
|
69
75
|
) | t1 >>> 32 - t2
|
|
70
76
|
) + t0
|
|
71
77
|
}
|
|
@@ -83,6 +89,8 @@ let wordsMD5 = (
|
|
|
83
89
|
* @param {*} input
|
|
84
90
|
* @param {number} j pad int32 length (0 or 16)
|
|
85
91
|
*
|
|
92
|
+
* @param {number} [i]
|
|
93
|
+
*
|
|
86
94
|
* @returns {[Int32Array<ArrayBuffer>, number]}
|
|
87
95
|
*/
|
|
88
96
|
let inputToWords = (
|
|
@@ -97,15 +105,16 @@ let inputToWords = (
|
|
|
97
105
|
// Using `Array` to process inputs over 512 MiB could throw a `RangeError`,
|
|
98
106
|
// so I replaced it with `Int32Array`.
|
|
99
107
|
output = new $Int32Array(j + $Math.ceil((byteLen + 9) / 64) * 16),
|
|
100
|
-
|
|
108
|
+
outputBytes = new $Uint8Array(output.buffer),
|
|
109
|
+
i,
|
|
101
110
|
) => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
111
|
+
// (fast) little-endian
|
|
112
|
+
isLE
|
|
113
|
+
? outputBytes.set(input, j * 4)
|
|
114
|
+
: i = 0
|
|
115
|
+
// (slow) big-endian
|
|
116
|
+
for (; i < byteLen; ++i & 3 || j++) {
|
|
117
|
+
output[j] |= input[i] << 8 * i
|
|
109
118
|
}
|
|
110
119
|
return [output, byteLen]
|
|
111
120
|
}
|
|
@@ -125,9 +134,7 @@ var md5 = (data, key, raw) => {
|
|
|
125
134
|
var i = 16
|
|
126
135
|
, hasKey = key != null
|
|
127
136
|
, [bdata, temp] = inputToWords(data, /**@type {*}*/(hasKey) * i)
|
|
128
|
-
|
|
129
|
-
/** @type {*} */
|
|
130
|
-
var out = raw ? new Uint8Array(i) : ''
|
|
137
|
+
, out = new $Uint8Array(i)
|
|
131
138
|
|
|
132
139
|
if (hasKey) {
|
|
133
140
|
// HMAC
|
|
@@ -148,15 +155,11 @@ var md5 = (data, key, raw) => {
|
|
|
148
155
|
bdata = wordsMD5(bdata, temp)
|
|
149
156
|
|
|
150
157
|
// words to bytes or hex
|
|
151
|
-
for (; i;
|
|
152
|
-
|
|
153
|
-
? out[i] = temp
|
|
154
|
-
: out = (temp >> 4 && '') + temp.toString(16) + out
|
|
155
|
-
) {
|
|
156
|
-
temp = bdata[--i >> 2] >> i * 8 & 0xff
|
|
158
|
+
for (; i;) {
|
|
159
|
+
out[--i] = bdata[i >> 2] >> i * 8
|
|
157
160
|
}
|
|
158
161
|
|
|
159
|
-
return out
|
|
162
|
+
return raw ? out : out.toHex()
|
|
160
163
|
}
|
|
161
164
|
|
|
162
165
|
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.4.0-es2026",
|
|
4
|
+
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 989 bytes.",
|
|
5
5
|
"author": "bddjr",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"typescript": "^7.0.2"
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
|
-
"build": "tsc && node scripts/build.mjs && es-check
|
|
66
|
+
"build": "tsc && node scripts/build.mjs && es-check es2026 --module --checkFeatures main.js browser.min.js",
|
|
67
67
|
"build:unchanged": "node scripts/build-unchanged.mjs",
|
|
68
68
|
"test": "pnpm build && pnpm test:only",
|
|
69
69
|
"test:only": "node scripts/test.mjs && node scripts/test-513MiB.mjs",
|