tinyhmacmd5 0.1.3 → 0.1.5

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
@@ -3,7 +3,7 @@ English | [中文](README-zh.md)
3
3
  # Tiny HMAC MD5
4
4
 
5
5
  A tiny, fully compliant HMAC-MD5 implementation for JavaScript:
6
- [`browser.min.js`](browser.min.js) is only **1051 bytes**.
6
+ [`browser.min.js`](browser.min.js) is only **1045 bytes**.
7
7
 
8
8
  - **Input type**: `string` (UTF‑8), `Uint8Array` or `Uint8ClampedArray`
9
9
  - **Output type**: hex `string` or raw `Uint8Array`
@@ -131,7 +131,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
131
131
 
132
132
  I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
133
133
 
134
- I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (1051 bytes) while also improving reliability.
134
+ I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (1045 bytes) while also improving reliability.
135
135
 
136
136
  Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
137
137
 
package/browser.min.js CHANGED
@@ -1,2 +1,2 @@
1
1
  /** @license https://bddjr.github.io/tinyhmacmd5/lic */
2
- {let r=1732584193,e=271733878,n=~r,t=~e,o=2**32,f=Math,a=f.floor,l=r=>16*a((r+8)/64)+16,i=[],u=(u,d,h=0,c=l(d),g,s,w,y,A=[r,t,n,e],v=[...A])=>{for(u[c-1]=0|8*d/o,u[c-2]=0|8*d,u[a(d/4)]|=128<<d%4*8;h<u.length;h+=16){for(y=c=0;c<64;)A[3&y]=0|((g=0|(g=A[3&++y],s=A[3&++y],w=A[3&++y],((d=c>>4<<2)?d>4?d>8?s^(g|~w):g^s^w:g&w|s&~w:g&s|~g&w)+(0|u[h+(c*(29521>>d)+(1296>>d)&15)])+(s="',16%).4$+07&*/5".charCodeAt(d+c%4),i[63-c++]??=0|o*f.abs(f.sin(c)))+A[y+1&3]))<<s|g>>>64-s)+A[y+2&3];for(y=4;y;)v[--y]=A[y]=0|v[y]+A[y]}return A},d=(r,e,n=("string"==typeof r?r=(new TextEncoder).encode(r):r).length,t=new Int32Array(l(4*e+n)),o=0)=>{for(;o<n;)t[e++]=r[o++]|r[o++]<<8|r[o++]<<16|r[o++]<<24;return[t,n]};var md5=(r,e,n)=>{var t=16,o=null!=e,[f,a]=d(r,o*t),l=new Uint8Array(t);if(o){let[r,n]=d(e,0),o=[];for(n>64&&(r=u(r,n));t;)o[--t]=1785358954^(f[t]=909522486^r[t]);t=16,(f=u(f,64+a)).unshift(...o),a=80}for(f=u(f,a);t;)l[--t]=f[t>>2]>>>t%4*8;return n?l:l.reduce((r,e)=>r+(e>>4&&"")+e.toString(16),"")}}
2
+ {let r=1732584193,e=271733878,n=~r,t=~e,o=2**32,f=Math,a=f.floor,l=r=>16*a((r+8)/64)+16,c=[],d=(d,i,u=0,g=l(i),h,s,w,y,A=[r,t,n,e],v=[...A])=>{for(d[g-1]=0|8*i/o,d[g-2]=0|8*i,d[a(i/4)]|=128<<i%4*8;u<d.length;u+=16){for(y=g=0;g<64;)A[3&y]=0|((h=0|(h=A[3&++y],s=A[3&++y],w=A[3&++y],((i=g>>4<<2)?i>4?i>8?s^(h|~w):h^s^w:h&w|s&~w:h&s|~h&w)+(0|d[u+(g*(29521>>i)+(1296>>i)&15)])+(s="',16%).4$+07&*/5".charCodeAt(i+g%4),c[63-g++]??=0|o*f.abs(f.sin(g)))+A[y+1&3]))<<s|h>>>64-s)+A[y+2&3];for(y=4;y;)v[--y]=A[y]=0|v[y]+A[y]}return A},i=(r,e,n=("string"==typeof r?r=(new TextEncoder).encode(r):r).length,t=new Int32Array(l(4*e+n)),o=0)=>{for(;o<n;)t[e++]=r[o++]|r[o++]<<8|r[o++]<<16|r[o++]<<24;return[t,n]};var md5=(r,e,n)=>{var t=16,o=null!=e,[f,a]=i(r,o*t),l=new Uint8Array(t);if(o){let[r,n]=i(e,0),o=[];for(n>64&&(r=d(r,n));t;)o[--t]=1785358954^(f[t]=909522486^r[t]);t=16,f=o.concat(d(f,64+a)),a=80}for(f=d(f,a);t;)l[--t]=f[t>>2]>>>t%4*8;return n?l:l.reduce((r,e)=>r+(e>>4&&"")+e.toString(16),"")}}
package/main.d.ts CHANGED
@@ -4,10 +4,10 @@
4
4
  * By default, returns the hash as a lowercase hexadecimal string.
5
5
  * If `raw` is true, returns a Uint8Array.
6
6
  *
7
- * @param {string | Uint8Array | Uint8ClampedArray} data The input data to hash. Strings are UTF‑8 encoded.
8
- * @param {string | Uint8Array | Uint8ClampedArray | null} [key] Optional HMAC key. When given, HMAC‑MD5 is calculated instead of plain MD5.
9
- * @param {boolean} [raw] If true, the hash is returned as raw bytes (Uint8Array); otherwise, as a hex string.
10
- * @returns {string | Uint8Array<ArrayBuffer>} The MD5 (or HMAC‑MD5) digest, either as a hex string or a Uint8Array.
7
+ * @param data The input data to hash. Strings are UTF‑8 encoded.
8
+ * @param key Optional HMAC key. When given, HMAC‑MD5 is calculated instead of plain MD5.
9
+ * @param raw If true, the hash is returned as raw bytes (Uint8Array); otherwise, as a hex string.
10
+ * @returns The MD5 (or HMAC‑MD5) digest, either as a hex string or a Uint8Array.
11
11
  */
12
12
  declare var md5: {
13
13
  (
@@ -20,5 +20,10 @@ declare var md5: {
20
20
  key: string | Uint8Array | Uint8ClampedArray | null | undefined,
21
21
  raw: true
22
22
  ): Uint8Array<ArrayBuffer>;
23
+ (
24
+ data: string | Uint8Array | Uint8ClampedArray,
25
+ key: string | Uint8Array | Uint8ClampedArray | null | undefined,
26
+ raw: boolean
27
+ ): string | Uint8Array<ArrayBuffer>;
23
28
  };
24
29
  export default md5;
package/main.js CHANGED
@@ -20,7 +20,7 @@ let toBinlLen = (byteLen) => floor((byteLen + 8) / 64) * 16 + 16
20
20
  let K = []
21
21
 
22
22
  /**
23
- * Calculate the MD5 of an array of little-endian words, and a bit length.
23
+ * Calculate the MD5 of an array of little-endian words, and a byte length.
24
24
  *
25
25
  * @param {Int32Array | number[]} x Array of little-endian words
26
26
  * @param {number} l Byte length
@@ -124,20 +124,6 @@ let inputToBinl = (
124
124
  return [output, byteLen]
125
125
  }
126
126
 
127
- /**
128
- * @overload
129
- * @param {string | Uint8Array | Uint8ClampedArray} data
130
- * @param {string | Uint8Array | Uint8ClampedArray | null} [key]
131
- * @param {false} [raw]
132
- * @returns {string}
133
- */
134
- /**
135
- * @overload
136
- * @param {string | Uint8Array | Uint8ClampedArray} data
137
- * @param {string | Uint8Array | Uint8ClampedArray | null | undefined} key
138
- * @param {true} raw
139
- * @returns {Uint8Array<ArrayBuffer>}
140
- */
141
127
  /**
142
128
  * Computes the MD5 hash of the input data.
143
129
  * If a key is provided, computes HMAC-MD5.
@@ -167,8 +153,7 @@ var md5 = (data, key, raw) => {
167
153
  opad[--i] = 0x6a6a6a6a ^ (bdata[i] = 0x36363636 ^ bkey[i])
168
154
  }
169
155
  i = 16
170
- bdata = binlMD5(bdata, 64 + dataByteLen)
171
- bdata.unshift(...opad)
156
+ bdata = opad.concat(binlMD5(bdata, 64 + dataByteLen))
172
157
  dataByteLen = 80
173
158
  }
174
159
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tinyhmacmd5",
3
- "version": "0.1.3",
4
- "description": "A tiny, fully compliant HMAC-MD5 implementation for JavaScript: browser.min.js is only 1051 bytes.",
3
+ "version": "0.1.5",
4
+ "description": "A tiny, fully compliant HMAC-MD5 implementation for JavaScript: browser.min.js is only 1045 bytes.",
5
5
  "author": "bddjr",
6
6
  "license": "MIT",
7
7
  "type": "module",