tinyhmacmd5 0.4.1 → 0.4.2
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 **1007 bytes**.
|
|
6
8
|
|
|
7
9
|
- **Input type**: `string` (UTF‑8), `Uint8Array` or `Uint8ClampedArray`
|
|
8
10
|
- **Output type**: hex `string` or bytes `Uint8Array`
|
|
@@ -172,19 +174,19 @@ pnpm benchmark
|
|
|
172
174
|
```
|
|
173
175
|
Data length: 104857600 chars (100MiB)
|
|
174
176
|
--------------------------------------------------
|
|
175
|
-
tinyhmacmd5 :
|
|
176
|
-
js-md5 :
|
|
177
|
-
blueimp-md5 :
|
|
178
|
-
crypto-js :
|
|
179
|
-
node:crypto :
|
|
177
|
+
tinyhmacmd5 : 550.92 ms
|
|
178
|
+
js-md5 : 128.96 ms
|
|
179
|
+
blueimp-md5 : 3837.83 ms
|
|
180
|
+
crypto-js : 1562.08 ms
|
|
181
|
+
node:crypto : 129.08 ms
|
|
180
182
|
--------------------------------------------------
|
|
181
183
|
✅ All pure JS implementations match node:crypto result.
|
|
182
184
|
|
|
183
185
|
--- Pure 513MiB Test (No prior small tests) ---
|
|
184
|
-
tinyhmacmd5 HMAC-MD5 timer: 2.
|
|
185
|
-
node:crypto HMAC-MD5 timer:
|
|
186
|
-
tinyhmacmd5 MD5 timer: 2.
|
|
187
|
-
node:crypto MD5 timer: 550.
|
|
186
|
+
tinyhmacmd5 HMAC-MD5 timer: 2.706s
|
|
187
|
+
node:crypto HMAC-MD5 timer: 547.808ms
|
|
188
|
+
tinyhmacmd5 MD5 timer: 2.693s
|
|
189
|
+
node:crypto MD5 timer: 550.144ms
|
|
188
190
|
```
|
|
189
191
|
|
|
190
192
|
CPU: i5-10600KF
|
|
@@ -210,7 +212,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
|
|
|
210
212
|
|
|
211
213
|
I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
|
|
212
214
|
|
|
213
|
-
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (
|
|
215
|
+
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (1007 bytes) while also improving reliability.
|
|
214
216
|
|
|
215
217
|
Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
|
|
216
218
|
|
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,i=e.of(a,~w,~a,w),s=new e(i))=>{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;)i[u&=3]=((w=i[u]+n[o]+(a=i[3&++u],w=i[3&++u],l=i[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;)s[--u]=i[u]+=s[u]}return i},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),i=a?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;a?i[w]=h:i=(h>>4&&"")+h.toString(16)+i)h=u[--w>>2]>>8*w&255;return i}}
|
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",
|
|
4
|
+
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 1007 bytes.",
|
|
5
5
|
"author": "bddjr",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|