tinyhmacmd5 0.4.1-es2026 → 0.4.2-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 +13 -11
- package/browser.min.js +1 -1
- package/main.js +7 -8
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,9 @@ English | [中文](README-zh.md)
|
|
|
2
2
|
|
|
3
3
|
# Tiny HMAC MD5
|
|
4
4
|
|
|
5
|
-
A tiny and reliable HMAC-MD5 implementation for JavaScript:
|
|
5
|
+
A tiny and reliable HMAC-MD5 implementation for JavaScript:
|
|
6
|
+
|
|
7
|
+
[`browser.min.js`](browser.min.js) is only **975 bytes**.
|
|
6
8
|
|
|
7
9
|
- **Input type**: `string` (UTF‑8), `Uint8Array` or `Uint8ClampedArray`
|
|
8
10
|
- **Output type**: hex `string` or bytes `Uint8Array`
|
|
@@ -168,19 +170,19 @@ pnpm benchmark
|
|
|
168
170
|
```
|
|
169
171
|
Data length: 104857600 chars (100MiB)
|
|
170
172
|
--------------------------------------------------
|
|
171
|
-
tinyhmacmd5 :
|
|
172
|
-
js-md5 :
|
|
173
|
-
blueimp-md5 :
|
|
174
|
-
crypto-js :
|
|
175
|
-
node:crypto :
|
|
173
|
+
tinyhmacmd5 : 550.92 ms
|
|
174
|
+
js-md5 : 128.96 ms
|
|
175
|
+
blueimp-md5 : 3837.83 ms
|
|
176
|
+
crypto-js : 1562.08 ms
|
|
177
|
+
node:crypto : 129.08 ms
|
|
176
178
|
--------------------------------------------------
|
|
177
179
|
✅ All pure JS implementations match node:crypto result.
|
|
178
180
|
|
|
179
181
|
--- Pure 513MiB Test (No prior small tests) ---
|
|
180
|
-
tinyhmacmd5 HMAC-MD5 timer: 2.
|
|
181
|
-
node:crypto HMAC-MD5 timer:
|
|
182
|
-
tinyhmacmd5 MD5 timer: 2.
|
|
183
|
-
node:crypto MD5 timer: 550.
|
|
182
|
+
tinyhmacmd5 HMAC-MD5 timer: 2.706s
|
|
183
|
+
node:crypto HMAC-MD5 timer: 547.808ms
|
|
184
|
+
tinyhmacmd5 MD5 timer: 2.693s
|
|
185
|
+
node:crypto MD5 timer: 550.144ms
|
|
184
186
|
```
|
|
185
187
|
|
|
186
188
|
CPU: i5-10600KF
|
|
@@ -206,7 +208,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
|
|
|
206
208
|
|
|
207
209
|
I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
|
|
208
210
|
|
|
209
|
-
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (
|
|
211
|
+
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (975 bytes) while also improving reliability.
|
|
210
212
|
|
|
211
213
|
Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
|
|
212
214
|
|
package/browser.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
/*! npmjs.com/tinyhmacmd5 */{let e=
|
|
1
|
+
/*! npmjs.com/tinyhmacmd5 */{let e=Int32Array,r=Uint8Array,n=new e(64).map((e,r)=>2**32*Math.abs(Math.sin(r+1))),t=1&new r(n.buffer)[3],f=(r,t,f=0,o,a=1732584193,w=271733878,l,u,h=r.length,s=e.of(a,~w,~a,w),d=new e(s))=>{for(r[h-1]=t/2**29,r[(t-t%4)/4]|=128<<(r[h-2]=t<<3);f<h;f+=16){for(t=o=0;t<16;t+=4)for(;o<4*t+16;)s[u&=3]=((w=s[u]+n[o]+(a=s[3&++u],w=s[3&++u],l=s[3&++u],w^(t>4?t>8?a|~l:a^l:t?l&(a^w):~a&(w^l)))+r[f+(o*(29521>>t)+(1296>>t)&15)])<<(l="',16%).4$+07&*/5".charCodeAt(3&o++|t))|w>>>32-l)+a;for(;u;)d[--u]=s[u]+=d[u]}return s},o=(n,f,o=("string"==typeof n?n=(new TextEncoder).encode(n):n).length,a=new e(f+18+(o-(o+8&63))/4),w=t&&new r(a.buffer,4*f).set(n))=>{for(;w<o;3&++w||f++)a[f]|=n[w]<<8*w;return[a,o]};var md5=(n,t,a)=>{var w=16,l=null!=t,[u,h]=o(n,l*w),s=new r(w);if(l){let[r,n]=o(t,0),a=new e(32);for(n>64&&(r=f(r,n)),n=w;n;)a[--n]=1785358954^(u[n]=909522486^r[n]);a.set(f(u,64+h),w),u=a,h=80}for(u=f(u,h);w;)s[--w]=u[w>>2]>>8*w;return a?s:s.toHex()}}
|
package/main.js
CHANGED
|
@@ -2,14 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
// Adapted from https://github.com/blueimp/JavaScript-MD5
|
|
4
4
|
|
|
5
|
-
let $Math = Math
|
|
6
|
-
|
|
7
5
|
let $Int32Array = Int32Array
|
|
8
6
|
|
|
9
7
|
let $Uint8Array = Uint8Array
|
|
10
8
|
|
|
11
9
|
/** MD5 constants cached in memory */
|
|
12
|
-
let K = new $Int32Array(64).map((v, i) => 2 ** 32 *
|
|
10
|
+
let K = new $Int32Array(64).map((v, i) => 2 ** 32 * Math.abs(Math.sin(i + 1)))
|
|
13
11
|
|
|
14
12
|
/** is little-endian */
|
|
15
13
|
let isLE = new $Uint8Array(K.buffer)[3] & 1
|
|
@@ -44,16 +42,16 @@ let wordsMD5 = (
|
|
|
44
42
|
// `l` may be >= 2**32, so cannot use `>>>` as a replacement for `floor`.
|
|
45
43
|
// `l << 3` cannot be changed to `l * 8`, as it would trigger JIT deoptimization on large inputs.
|
|
46
44
|
x[xLen - 1] = l / 2 ** 29;
|
|
47
|
-
x[
|
|
45
|
+
x[(l - l % 4) / 4] |= 0x80 << (x[xLen - 2] = l << 3);
|
|
48
46
|
|
|
49
47
|
for (; i < xLen; i += 16) {
|
|
50
|
-
for (l = 0; l < 16; l += 4) {
|
|
51
|
-
for (
|
|
48
|
+
for (l = j = 0; l < 16; l += 4) {
|
|
49
|
+
for (; j < 4 * l + 16;) {
|
|
52
50
|
output[oi &= 3] = (
|
|
53
51
|
(
|
|
54
52
|
t1 = (
|
|
55
53
|
output[oi] +
|
|
56
|
-
K[
|
|
54
|
+
K[j] +
|
|
57
55
|
(
|
|
58
56
|
t0 = output[++oi & 3],
|
|
59
57
|
t1 = output[++oi & 3],
|
|
@@ -98,13 +96,14 @@ let inputToWords = (
|
|
|
98
96
|
j,
|
|
99
97
|
// var:
|
|
100
98
|
byteLen = (
|
|
99
|
+
// Don't replace the type check below with something like `input.big`; it's unreliable.
|
|
101
100
|
typeof input == 'string'
|
|
102
101
|
? input = new TextEncoder().encode(input)
|
|
103
102
|
: input
|
|
104
103
|
).length,
|
|
105
104
|
// Using `Array` to process inputs over 512 MiB could throw a `RangeError`,
|
|
106
105
|
// so I replaced it with `Int32Array`.
|
|
107
|
-
output = new $Int32Array(j +
|
|
106
|
+
output = new $Int32Array(j + 18 + (byteLen - (byteLen + 8 & 63)) / 4),
|
|
108
107
|
// (fast) little-endian
|
|
109
108
|
i = isLE && new $Uint8Array(output.buffer, j * 4).set(input),
|
|
110
109
|
) => {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tinyhmacmd5",
|
|
3
|
-
"version": "0.4.
|
|
4
|
-
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only
|
|
3
|
+
"version": "0.4.2-es2026",
|
|
4
|
+
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 975 bytes.",
|
|
5
5
|
"author": "bddjr",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|