tinyhmacmd5 0.4.0 → 0.4.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 -11
- package/browser.min.js +1 -1
- package/main.js +4 -8
- 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 **1014 bytes**.
|
|
6
6
|
|
|
7
7
|
- **Input type**: `string` (UTF‑8), `Uint8Array` or `Uint8ClampedArray`
|
|
8
8
|
- **Output type**: hex `string` or bytes `Uint8Array`
|
|
@@ -172,19 +172,19 @@ pnpm benchmark
|
|
|
172
172
|
```
|
|
173
173
|
Data length: 104857600 chars (100MiB)
|
|
174
174
|
--------------------------------------------------
|
|
175
|
-
tinyhmacmd5 :
|
|
176
|
-
js-md5 : 130.
|
|
177
|
-
blueimp-md5 :
|
|
178
|
-
crypto-js :
|
|
179
|
-
node:crypto : 130.
|
|
175
|
+
tinyhmacmd5 : 568.29 ms
|
|
176
|
+
js-md5 : 130.97 ms
|
|
177
|
+
blueimp-md5 : 3835.78 ms
|
|
178
|
+
crypto-js : 1560.48 ms
|
|
179
|
+
node:crypto : 130.22 ms
|
|
180
180
|
--------------------------------------------------
|
|
181
181
|
✅ All pure JS implementations match node:crypto result.
|
|
182
182
|
|
|
183
183
|
--- Pure 513MiB Test (No prior small tests) ---
|
|
184
|
-
tinyhmacmd5 HMAC-MD5 timer: 2.
|
|
185
|
-
node:crypto HMAC-MD5 timer: 552.
|
|
186
|
-
tinyhmacmd5 MD5 timer: 2.
|
|
187
|
-
node:crypto MD5 timer:
|
|
184
|
+
tinyhmacmd5 HMAC-MD5 timer: 2.789s
|
|
185
|
+
node:crypto HMAC-MD5 timer: 552.479ms
|
|
186
|
+
tinyhmacmd5 MD5 timer: 2.778s
|
|
187
|
+
node:crypto MD5 timer: 550.663ms
|
|
188
188
|
```
|
|
189
189
|
|
|
190
190
|
CPU: i5-10600KF
|
|
@@ -210,7 +210,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
|
|
|
210
210
|
|
|
211
211
|
I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
|
|
212
212
|
|
|
213
|
-
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (
|
|
213
|
+
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (1014 bytes) while also improving reliability.
|
|
214
214
|
|
|
215
215
|
Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
|
|
216
216
|
|
package/browser.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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)[
|
|
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=1&new n(t.buffer)[3],o=(n,f,o=0,a,l=1732584193,w=271733878,i,u,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[u&=3]=((w=c[u]+t[4*f+a]+(l=c[3&++u],w=c[3&++u],i=c[3&++u],w^(f>4?f>8?l|~i:l^i:f?i&(l^w):~l&(w^i)))+n[o+(a*(29521>>f)+(1296>>f)&15)])<<(i="',16%).4$+07&*/5".charCodeAt(3&a++|f))|w>>>32-i)+l;for(;u;)d[--u]=c[u]+=d[u]}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=f&&new n(l.buffer,4*o).set(t))=>{for(;w<a;3&++w||o++)l[o]|=t[w]<<8*w;return[l,a]};var md5=(e,t,f)=>{var l=16,w=null!=t,[i,u]=a(e,w*l),s=f?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^(i[n]=909522486^e[n]);f.set(o(i,64+u),l),i=f,u=80}for(i=o(i,u);l;f?s[l]=u:s=(u>>4&&"")+u.toString(16)+s)u=i[--l>>2]>>8*l&255;return s}}
|
package/main.js
CHANGED
|
@@ -12,7 +12,7 @@ let $Uint8Array = Uint8Array
|
|
|
12
12
|
let K = new $Int32Array(64).map((v, i) => 2 ** 32 * $Math.abs($Math.sin(i + 1)))
|
|
13
13
|
|
|
14
14
|
/** is little-endian */
|
|
15
|
-
let isLE = new $Uint8Array(K.buffer)[
|
|
15
|
+
let isLE = new $Uint8Array(K.buffer)[3] & 1
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Calculate the MD5 of an array of little-endian words, and a byte length.
|
|
@@ -89,7 +89,7 @@ let wordsMD5 = (
|
|
|
89
89
|
* @param {*} input
|
|
90
90
|
* @param {number} j pad int32 length (0 or 16)
|
|
91
91
|
*
|
|
92
|
-
* @param {
|
|
92
|
+
* @param {*} [i]
|
|
93
93
|
*
|
|
94
94
|
* @returns {[Int32Array<ArrayBuffer>, number]}
|
|
95
95
|
*/
|
|
@@ -105,13 +105,9 @@ let inputToWords = (
|
|
|
105
105
|
// Using `Array` to process inputs over 512 MiB could throw a `RangeError`,
|
|
106
106
|
// so I replaced it with `Int32Array`.
|
|
107
107
|
output = new $Int32Array(j + $Math.ceil((byteLen + 9) / 64) * 16),
|
|
108
|
-
outputBytes = new $Uint8Array(output.buffer),
|
|
109
|
-
i,
|
|
110
|
-
) => {
|
|
111
108
|
// (fast) little-endian
|
|
112
|
-
isLE
|
|
113
|
-
|
|
114
|
-
: i = 0
|
|
109
|
+
i = isLE && new $Uint8Array(output.buffer, j * 4).set(input),
|
|
110
|
+
) => {
|
|
115
111
|
// (slow) big-endian
|
|
116
112
|
for (; i < byteLen; ++i & 3 || j++) {
|
|
117
113
|
output[j] |= input[i] << 8 * i
|
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.1",
|
|
4
|
+
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 1014 bytes.",
|
|
5
5
|
"author": "bddjr",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|