tinyhmacmd5 0.3.2 → 0.3.3

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 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 **984 bytes**.
5
+ A tiny and reliable HMAC-MD5 implementation for JavaScript: [`browser.min.js`](browser.min.js) is only **982 bytes**.
6
6
 
7
7
  - **Input type**: `string` (UTF‑8), `Uint8Array` or `Uint8ClampedArray`
8
8
  - **Output type**: hex `string` or bytes `Uint8Array`
@@ -172,21 +172,26 @@ pnpm benchmark
172
172
  ```
173
173
  Data length: 104857600 chars (100MiB)
174
174
  --------------------------------------------------
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
175
+ tinyhmacmd5 : 640.00 ms
176
+ js-md5 : 131.10 ms
177
+ blueimp-md5 : 3825.62 ms
178
+ crypto-js : 1572.49 ms
179
+ node:crypto : 130.07 ms
180
180
  --------------------------------------------------
181
- ✅ All pure JS implementations match native crypto result.
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: 3.463s
185
- node:crypto HMAC-MD5 timer: 572.541ms
186
- tinyhmacmd5 MD5 timer: 3.450s
187
- node:crypto MD5 timer: 569.816ms
184
+ tinyhmacmd5 HMAC-MD5 timer: 3.184s
185
+ node:crypto HMAC-MD5 timer: 552.938ms
186
+ tinyhmacmd5 MD5 timer: 3.175s
187
+ node:crypto MD5 timer: 553.263ms
188
188
  ```
189
189
 
190
+ CPU: i5-10600KF
191
+ DRAM: 64GiB DDR4 3333MT/s
192
+ OS: Windows 11 Pro for Workstations 25H2 26200.8737
193
+ Node.js: v26.8.1
194
+
190
195
  ---
191
196
 
192
197
  ## Why I Made This Project
@@ -205,7 +210,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
205
210
 
206
211
  I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
207
212
 
208
- I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (984 bytes) while also improving reliability.
213
+ I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (982 bytes) while also improving reliability.
209
214
 
210
215
  Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
211
216
 
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=(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),a),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}}
1
+ /*! npmjs.com/tinyhmacmd5 */{let r=Math,e=Int32Array,n=new e(64).map((e,n)=>2**32*r.abs(r.sin(n+1))),t=(t,o,f=0,a,l=1732584193,i=271733878,w,c,d=t.length,g=e.of(l,~i,~l,i),h=new e(g))=>{for(t[d-1]=o/2**29,t[r.floor(o/4)]|=128<<(t[d-2]=o<<3);f<d;f+=16){for(o=0;o<16;o+=4)for(a=0;a<16;)g[c&=3]=((i=g[c]+n[4*o+a]+(l=g[3&++c],i=g[3&++c],w=g[3&++c],o?i^(o>4?o>8?l|~w:l^w:w&(l^i)):l&i|~l&w)+t[f+(a*(29521>>o)+(1296>>o)&15)])<<(w="',16%).4$+07&*/5".charCodeAt(3&a++|o)-32)|i>>>32-w)+l;for(;c;)h[--c]=g[c]+=h[c]}return g},o=(n,t,o=("string"==typeof n?n=(new TextEncoder).encode(n):n).length,f=new e(t+16*r.ceil((o+9)/64)),a=0)=>{for(;a<o;)f[t++]=n[a++]|n[a++]<<8|n[a++]<<16|n[a++]<<24;return[f,o]};var md5=(r,n,f)=>{var a=16,l=null!=n,[i,w]=o(r,l*a),c=f?new Uint8Array(a):"";if(l){let[r,f]=o(n,0),l=new e(32);for(f>64&&(r=t(r,f)),f=a;f;)l[--f]=1785358954^(i[f]=909522486^r[f]);l.set(t(i,64+w),a),i=l,w=80}for(i=t(i,w);a;f?c[a]=w:c=(w>>4&&"")+w.toString(16)+c)w=i[--a>>2]>>8*a&255;return c}}
package/main.js CHANGED
@@ -4,8 +4,6 @@
4
4
 
5
5
  let $Math = Math
6
6
 
7
- let floor = $Math.floor
8
-
9
7
  let $Int32Array = Int32Array
10
8
 
11
9
  /** MD5 constants cached in memory */
@@ -40,7 +38,7 @@ let wordsMD5 = (
40
38
  // append padding
41
39
  // `l` may be >= 2**32, so cannot use `>>>` as a replacement for `floor`
42
40
  x[xLen - 1] = l / 2 ** 29;
43
- x[floor(l / 4)] |= 0x80 << (x[xLen - 2] = l << 3);
41
+ x[$Math.floor(l / 4)] |= 0x80 << (x[xLen - 2] = l << 3);
44
42
 
45
43
  for (; i < xLen; i += 16) {
46
44
  for (l = 0; l < 16; l += 4) {
@@ -49,24 +47,26 @@ let wordsMD5 = (
49
47
  (
50
48
  t1 = (
51
49
  output[oi] +
52
- K[l * 4 | j] +
50
+ K[l * 4 + j] +
53
51
  (
54
52
  t0 = output[++oi & 3],
55
53
  t1 = output[++oi & 3],
56
54
  t2 = output[++oi & 3],
57
55
  l
58
- ? l > 4
59
- ? l > 8
60
- ? t1 ^ (t0 | ~t2) // Round 4: I
61
- : t0 ^ t1 ^ t2 // Round 3: H
62
- : t0 & t2 | t1 & ~t2 // Round 2: G
63
- : t0 & t1 | ~t0 & t2 // Round 1: F
56
+ ? t1 ^ (
57
+ l > 4
58
+ ? l > 8
59
+ ? t0 | ~t2 // Round 4: I
60
+ : t0 ^ t2 // Round 3: H
61
+ : t2 & (t0 ^ t1) // Round 2: G
62
+ )
63
+ : t0 & t1 | ~t0 & t2 // Round 1: F
64
64
  ) +
65
65
  x[i + (j * (0x7351 >> l) + (0x0510 >> l) & 15)]
66
66
  )
67
67
  ) << (
68
- t2 = "',16%).4$+07&*/5".charCodeAt(j++ & 3 | l)
69
- ) | t1 >>> 64 - t2
68
+ t2 = "',16%).4$+07&*/5".charCodeAt(j++ & 3 | l) - 32
69
+ ) | t1 >>> 32 - t2
70
70
  ) + t0
71
71
  }
72
72
  }
@@ -81,7 +81,7 @@ let wordsMD5 = (
81
81
  * Convert bytes to an array of little-endian words
82
82
  *
83
83
  * @param {*} input
84
- * @param {number} j pad int32 length
84
+ * @param {number} j pad int32 length (0 or 16)
85
85
  *
86
86
  * @returns {[Int32Array<ArrayBuffer>, number]}
87
87
  */
@@ -96,7 +96,7 @@ let inputToWords = (
96
96
  ).length,
97
97
  // Using `Array` to process inputs over 512 MiB could throw a `RangeError`,
98
98
  // so I replaced it with `Int32Array`.
99
- output = new $Int32Array(floor((j * 4 + byteLen + 72) / 64) * 16),
99
+ output = new $Int32Array(j + $Math.ceil((byteLen + 9) / 64) * 16),
100
100
  i = 0,
101
101
  ) => {
102
102
  for (; i < byteLen;) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tinyhmacmd5",
3
- "version": "0.3.2",
4
- "description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 984 bytes.",
3
+ "version": "0.3.3",
4
+ "description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 982 bytes.",
5
5
  "author": "bddjr",
6
6
  "license": "MIT",
7
7
  "type": "module",