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 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: [`browser.min.js`](browser.min.js) is only **982 bytes**.
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 : 568.29 ms
172
- js-md5 : 130.97 ms
173
- blueimp-md5 : 3835.78 ms
174
- crypto-js : 1560.48 ms
175
- node:crypto : 130.22 ms
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.789s
181
- node:crypto HMAC-MD5 timer: 552.479ms
182
- tinyhmacmd5 MD5 timer: 2.778s
183
- node:crypto MD5 timer: 550.663ms
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 (982 bytes) while also improving reliability.
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=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,u,i,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[i&=3]=((w=c[i]+t[4*f+a]+(l=c[3&++i],w=c[3&++i],u=c[3&++i],w^(f>4?f>8?l|~u:l^u:f?u&(l^w):~l&(w^u)))+n[o+(a*(29521>>f)+(1296>>f)&15)])<<(u="',16%).4$+07&*/5".charCodeAt(3&a++|f))|w>>>32-u)+l;for(;i;)d[--i]=c[i]+=d[i]}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,[u,i]=a(e,w*l),s=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^(u[n]=909522486^e[n]);f.set(o(u,64+i),l),u=f,i=80}for(u=o(u,i);l;)s[--l]=u[l>>2]>>8*l;return f?s:s.toHex()}}
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 * $Math.abs($Math.sin(i + 1)))
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[$Math.floor(l / 4)] |= 0x80 << (x[xLen - 2] = l << 3);
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 (j = 0; j < 16;) {
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[l * 4 + j] +
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 + $Math.ceil((byteLen + 9) / 64) * 16),
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.1-es2026",
4
- "description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 982 bytes.",
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",