tinyhmacmd5 0.3.0 → 0.3.1
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 +11 -12
- package/browser.min.js +1 -1
- package/main.js +1 -5
- package/package.json +2 -2
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 **985 bytes**.
|
|
6
6
|
|
|
7
7
|
- **Input type**: `string` (UTF‑8), `Uint8Array` or `Uint8ClampedArray`
|
|
8
8
|
- **Output type**: hex `string` or bytes `Uint8Array`
|
|
@@ -170,22 +170,21 @@ pnpm benchmark
|
|
|
170
170
|
```
|
|
171
171
|
|
|
172
172
|
```
|
|
173
|
-
Generating 100MiB string data (this might take a moment)...
|
|
174
173
|
Data length: 104857600 chars (100MiB)
|
|
175
174
|
--------------------------------------------------
|
|
176
|
-
tinyhmacmd5 :
|
|
177
|
-
js-md5 :
|
|
178
|
-
blueimp-md5 :
|
|
179
|
-
crypto-js :
|
|
180
|
-
native crypto :
|
|
175
|
+
tinyhmacmd5 : 724.91 ms
|
|
176
|
+
js-md5 : 138.45 ms
|
|
177
|
+
blueimp-md5 : 4014.42 ms
|
|
178
|
+
crypto-js : 1637.30 ms
|
|
179
|
+
native crypto : 135.11 ms
|
|
181
180
|
--------------------------------------------------
|
|
182
181
|
✅ All pure JS implementations match native crypto result.
|
|
183
182
|
|
|
184
183
|
--- Pure 513MiB Test (No prior small tests) ---
|
|
185
|
-
tinyhmacmd5 HMAC-MD5 timer: 3.
|
|
186
|
-
node:crypto HMAC-MD5 timer: 572.
|
|
187
|
-
tinyhmacmd5 MD5 timer: 3.
|
|
188
|
-
node:crypto MD5 timer:
|
|
184
|
+
tinyhmacmd5 HMAC-MD5 timer: 3.463s
|
|
185
|
+
node:crypto HMAC-MD5 timer: 572.541ms
|
|
186
|
+
tinyhmacmd5 MD5 timer: 3.450s
|
|
187
|
+
node:crypto MD5 timer: 569.816ms
|
|
189
188
|
```
|
|
190
189
|
|
|
191
190
|
---
|
|
@@ -206,7 +205,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
|
|
|
206
205
|
|
|
207
206
|
I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
|
|
208
207
|
|
|
209
|
-
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (
|
|
208
|
+
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (985 bytes) while also improving reliability.
|
|
210
209
|
|
|
211
210
|
Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
|
|
212
211
|
|
package/browser.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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=
|
|
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,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},f=(r,t,o=("string"==typeof r?r=(new TextEncoder).encode(r):r).length,f=new n(16*e((4*t+o+72)/64)),a=0)=>{for(;a<o;)f[t++]=r[a++]|r[a++]<<8|r[a++]<<16|r[a++]<<24;return[f,o]};var md5=(r,e,t)=>{var a=16,l=null!=e,[w,i]=f(r,l*a),d=t?new Uint8Array(a):"";if(l){let[r,t]=f(e,0),l=new n(32);for(t>64&&(r=o(r,t)),t=a;t;)l[--t]=1785358954^(w[t]=909522486^r[t]);l.set(o(w,64+i),16),w=l,i=80}for(w=o(w,i);a;t?d[a]=i:d=(i>>4&&"")+i.toString(16)+d)i=w[--a>>2]>>8*a&255;return d}}
|
package/main.js
CHANGED
|
@@ -11,10 +11,6 @@ let $Int32Array = Int32Array
|
|
|
11
11
|
/** MD5 constants cached in memory */
|
|
12
12
|
let K = new $Int32Array(64).map((v, i) => 2 ** 32 * $Math.abs($Math.sin(i + 1)))
|
|
13
13
|
|
|
14
|
-
/** @param {number} byteLen */
|
|
15
|
-
// `byteLen` may be >= 2**32, so cannot use `>>>` as a replacement for `floor`
|
|
16
|
-
let wordsLen = (byteLen) => floor((byteLen + 72) / 64) * 16
|
|
17
|
-
|
|
18
14
|
/**
|
|
19
15
|
* Calculate the MD5 of an array of little-endian words, and a byte length.
|
|
20
16
|
*
|
|
@@ -100,7 +96,7 @@ let inputToWords = (
|
|
|
100
96
|
).length,
|
|
101
97
|
// Using `Array` to process inputs over 512 MiB could throw a `RangeError`,
|
|
102
98
|
// so I replaced it with `Int32Array`.
|
|
103
|
-
output = new $Int32Array(
|
|
99
|
+
output = new $Int32Array(floor((j * 4 + byteLen + 72) / 64) * 16),
|
|
104
100
|
i = 0,
|
|
105
101
|
) => {
|
|
106
102
|
for (; i < byteLen;) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tinyhmacmd5",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 985 bytes.",
|
|
5
5
|
"author": "bddjr",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|