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 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 **1014 bytes**.
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 : 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
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.789s
185
- node:crypto HMAC-MD5 timer: 552.479ms
186
- tinyhmacmd5 MD5 timer: 2.778s
187
- node:crypto MD5 timer: 550.663ms
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 (1014 bytes) while also improving reliability.
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=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}}
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 * $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",
4
- "description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 1014 bytes.",
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",