tinyhmacmd5 0.2.3 → 0.3.0-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,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 **982 bytes**.
5
+ A tiny and reliable HMAC-MD5 implementation for JavaScript: [`browser.min.js`](browser.min.js) is only **963 bytes**.
6
6
 
7
7
  - **Input type**: `string` (UTF‑8), `Uint8Array` or `Uint8ClampedArray`
8
8
  - **Output type**: hex `string` or bytes `Uint8Array`
@@ -10,14 +10,10 @@ A tiny and reliable HMAC-MD5 implementation for JavaScript: [`browser.min.js`](b
10
10
  - **TypeScript‑ready**: [`main.d.ts`](main.d.ts)
11
11
  - **0 dependencies**
12
12
 
13
- **Live demo**: https://bddjr.github.io/tinyhmacmd5/
14
-
15
13
  `tinyhmacmd5` does not simply aim for the smallest possible size.
16
14
  Its goal is to balance code size and runtime performance, so some performance-oriented implementations are intentionally retained.
17
15
  As a result, the final size is not the smallest theoretically achievable.
18
16
 
19
- If you need a smaller implementation that targets ECMAScript 2026, see the [`es2026`](https://github.com/bddjr/tinyhmacmd5/tree/es2026) branch.
20
-
21
17
  > [!WARNING]
22
18
  > MD5 is cryptographically broken and unsafe for security-sensitive applications.
23
19
  > Do not rely on it for password hashing, digital signatures, or certificate verification.
@@ -27,7 +23,7 @@ If you need a smaller implementation that targets ECMAScript 2026, see the [`es2
27
23
  ### npm
28
24
 
29
25
  ```
30
- npm i tinyhmacmd5
26
+ npm i tinyhmacmd5@es2026
31
27
  ```
32
28
 
33
29
  ```js
@@ -43,7 +39,7 @@ You can also use other package managers (e.g. `pnpm` or `yarn`) in place of `npm
43
39
  See https://www.jsdelivr.com/package/npm/tinyhmacmd5
44
40
 
45
41
  ```html
46
- <script src="https://cdn.jsdelivr.net/npm/tinyhmacmd5"></script>
42
+ <script src="https://cdn.jsdelivr.net/npm/tinyhmacmd5@es2026"></script>
47
43
  ```
48
44
 
49
45
  It will define the `md5` function using `var`.
@@ -51,7 +47,7 @@ It will define the `md5` function using `var`.
51
47
  ### UNPKG
52
48
 
53
49
  ```html
54
- <script src="https://unpkg.com/tinyhmacmd5"></script>
50
+ <script src="https://unpkg.com/tinyhmacmd5@es2026"></script>
55
51
  ```
56
52
 
57
53
  It will define the `md5` function using `var`.
@@ -142,27 +138,54 @@ md5(data)
142
138
 
143
139
  ## Runtime Environment
144
140
 
145
- Environments that support [Exponentiation (`**`)](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Exponentiation) and [`TextEncoder`](https://developer.mozilla.org/docs/Web/API/TextEncoder) :
141
+ Environments that support [`Uint8Array.prototype.toHex()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toHex) :
146
142
 
147
143
  - Desktop
148
- - Chrome ≥ 52 (2016-07-20)
149
- - Edge ≥ 79 (2020-01-15)
150
- - Firefox ≥ 52 (2017-03-07)
151
- - Opera ≥ 39 (2016-08-02)
152
- - Safari ≥ 10.1 (2017-03-27)
144
+ - Chrome ≥ 140 (2025-09-02)
145
+ - Edge ≥ 140 (2025-09-05)
146
+ - Firefox ≥ 133 (2024-11-26)
147
+ - Opera ≥ 124 (2025-11-13)
148
+ - Safari ≥ 18.2 (2024-12-11)
153
149
  - Mobile
154
- - Chrome Android ≥ 52 (2016-07-27)
155
- - Firefox for Android ≥ 52 (2017-03-07)
156
- - Opera Android ≥ 41 (2016-10-25)
157
- - Safari on iOS ≥ 10.3 (2017-03-27)
158
- - Samsung Browser ≥ 6 (2017-08-23)
159
- - WebView Android ≥ 51 (2016-06-08)
160
- - WebView on iOS ≥ 10.3 (2017-03-27)
150
+ - Chrome Android ≥ 140 (2025-09-02)
151
+ - Firefox for Android ≥ 133 (2024-11-26)
152
+ - Opera Android ≥ 92 (2025-10-08)
153
+ - Safari on iOS ≥ 18.2 (2024-12-11)
154
+ - Samsung Browser ×
155
+ - WebView Android ≥ 140 (2025-09-02)
156
+ - WebView on iOS ≥ 18.2 (2024-12-11)
161
157
 
162
158
  Not recommended for use in environments that support `node:crypto`, as `node:crypto` already provides HMAC-MD5.
163
159
 
164
160
  ---
165
161
 
162
+ ## Benchmark
163
+
164
+ ```
165
+ pnpm benchmark
166
+ ```
167
+
168
+ ```
169
+ Generating 100MiB string data (this might take a moment)...
170
+ Data length: 104857600 chars (100MiB)
171
+ --------------------------------------------------
172
+ tinyhmacmd5 : 718.27 ms
173
+ js-md5 : 141.43 ms
174
+ blueimp-md5 : 3934.98 ms
175
+ crypto-js : 1732.55 ms
176
+ native crypto : 136.60 ms
177
+ --------------------------------------------------
178
+ ✅ All pure JS implementations match native crypto result.
179
+
180
+ --- Pure 513MiB Test (No prior small tests) ---
181
+ tinyhmacmd5 HMAC-MD5 timer: 3.465s
182
+ node:crypto HMAC-MD5 timer: 572.686ms
183
+ tinyhmacmd5 MD5 timer: 3.451s
184
+ node:crypto MD5 timer: 571.58ms
185
+ ```
186
+
187
+ ---
188
+
166
189
  ## Why I Made This Project
167
190
 
168
191
  I initially used HMAC-MD5 just to call a certain website's API. That site signs the request body with `crypto-js`'s HMAC-MD5 to make reverse engineering harder.
@@ -179,7 +202,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
179
202
 
180
203
  I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
181
204
 
182
- I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (982 bytes) while also improving reliability.
205
+ I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (963 bytes) while also improving reliability.
183
206
 
184
207
  Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
185
208
 
package/browser.min.js CHANGED
@@ -1 +1 @@
1
- /*! npmjs.com/tinyhmacmd5 */{let r=[],n=0,t=Math,e=t.floor,o=r=>16*e((r+72)/64),f=(n,t,f=0,a=o(t),l=1732584193,c=271733878,i,d,g=[l,~c,~l,c],h=[...g])=>{for(n[a-1]=0|t/2**29,n[e(t/4)]|=128<<(n[a-2]=t<<3);f<n.length;f+=16){for(a=0;a<64;g[d&=3]=0|((c=0|g[d]+r[a]+(l=g[3&++d],c=g[3&++d],i=g[3&++d],t?t>4?t>8?c^(l|~i):l^c^i:l&i|c&~i:l&c|~l&i)+(0|n[f+(a*(29521>>t)+(1296>>t)&15)]))<<(i="',16%).4$+07&*/5".charCodeAt(3&a++|t))|c>>>64-i)+l)t=a>>4<<2;for(;d;)h[--d]=g[d]=0|h[d]+g[d]}return g},a=(r,n,t=("string"==typeof r?r=(new TextEncoder).encode(r):r).length,e=new Int32Array(o(4*n+t)),f=0)=>{for(;f<t;)e[n++]=r[f++]|r[f++]<<8|r[f++]<<16|r[f++]<<24;return[e,t]};for(var md5=(r,n,t)=>{var e=16,o=null!=n,[l,c]=a(r,o*e),i=t?new Uint8Array(e):"";if(o){let[r,t]=a(n,0),o=[];for(t>64&&(r=f(r,t)),t=e;t;)o[--t]=1785358954^(l[t]=909522486^r[t]);l=o.concat(f(l,64+c)),c=80}for(l=f(l,c);e;t?i[e]=c:i=(c>>4&&"")+c.toString(16)+i)c=l[--e>>2]>>8*e&255;return i};n<64;)r[n]=0|2**32*t.abs(t.sin(++n))}
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=>16*e((r+72)/64),f=(r,o,f=0,a,l=1732584193,w=271733878,d,h,i=r.length,s=n.of(l,~w,~l,w),u=new n(s))=>{for(r[i-1]=o/2**29,r[e(o/4)]|=128<<(r[i-2]=o<<3);f<i;f+=16){for(o=0;o<16;o+=4)for(a=0;a<16;)s[h&=3]=((w=s[h]+t[4*o|a]+(l=s[3&++h],w=s[3&++h],d=s[3&++h],o?o>4?o>8?w^(l|~d):l^w^d:l&d|w&~d:l&w|~l&d)+r[f+(a*(29521>>o)+(1296>>o)&15)])<<(d="',16%).4$+07&*/5".charCodeAt(3&a++|o))|w>>>64-d)+l;for(;h;)u[--h]=s[h]+=u[h]}return s},a=(r,e,t=("string"==typeof r?r=(new TextEncoder).encode(r):r).length,f=new n(o(4*e+t)),a=0)=>{for(;a<t;)f[e++]=r[a++]|r[a++]<<8|r[a++]<<16|r[a++]<<24;return[f,t]};var md5=(r,e,t)=>{var o=16,l=null!=e,[w,d]=a(r,l*o),h=new Uint8Array(o);if(l){let[r,t]=a(e,0),l=new n(32);for(t>64&&(r=f(r,t)),t=o;t;)l[--t]=1785358954^(w[t]=909522486^r[t]);l.set(f(w,64+d),16),w=l,d=80}for(w=f(w,d);o;)h[--o]=w[o>>2]>>8*o;return t?h:h.toHex()}}
package/main.js CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  // Adapted from https://github.com/blueimp/JavaScript-MD5
4
4
 
5
- /** @type {number[]} MD5 constants cached in memory */
6
- let K = []
7
-
8
- let i = 0
9
-
10
5
  let $Math = Math
11
6
 
12
7
  let floor = $Math.floor
13
8
 
9
+ let $Int32Array = Int32Array
10
+
11
+ /** MD5 constants cached in memory */
12
+ let K = new $Int32Array(64).map((v, i) => 2 ** 32 * $Math.abs($Math.sin(i + 1)))
13
+
14
14
  /** @param {number} byteLen */
15
15
  // `byteLen` may be >= 2**32, so cannot use `>>>` as a replacement for `floor`
16
16
  let wordsLen = (byteLen) => floor((byteLen + 72) / 64) * 16
@@ -18,64 +18,64 @@ let wordsLen = (byteLen) => floor((byteLen + 72) / 64) * 16
18
18
  /**
19
19
  * Calculate the MD5 of an array of little-endian words, and a byte length.
20
20
  *
21
- * @param {Int32Array | number[]} x Array of little-endian words
21
+ * @param {Int32Array<ArrayBuffer>} x little-endian words
22
22
  * @param {number} l Byte length
23
23
  *
24
+ * @param {number} [j]
24
25
  * @param {number} [t2]
25
26
  * @param {number} [oi]
26
27
  *
27
- * @returns {number[]} MD5 Array
28
+ * @returns {Int32Array<ArrayBuffer>} MD5 Array
28
29
  */
29
30
  let wordsMD5 = (
30
31
  x,
31
32
  l,
32
33
  // var:
33
34
  i = 0,
34
- j = wordsLen(l),
35
+ j,
35
36
  t0 = 1732584193,
36
37
  t1 = 271733878,
37
38
  t2,
38
39
  oi,
39
- output = [t0, ~t1, ~t0, t1],
40
- oldOutput = [...output],
40
+ xLen = x.length,
41
+ output = $Int32Array.of(t0, ~t1, ~t0, t1),
42
+ oldOutput = new $Int32Array(output),
41
43
  ) => {
42
44
  // append padding
43
45
  // `l` may be >= 2**32, so cannot use `>>>` as a replacement for `floor`
44
- x[j - 1] = 0 | l / 2 ** 29;
45
- x[floor(l / 4)] |= 0x80 << (x[j - 2] = l << 3);
46
-
47
- for (; i < x.length; i += 16) {
48
- for (j = 0; j < 64;
49
- output[oi &= 3] = 0 | (
50
- (
51
- t1 = 0 | (
52
- output[oi] +
53
- K[j] +
54
- (
55
- t0 = output[++oi & 3],
56
- t1 = output[++oi & 3],
57
- t2 = output[++oi & 3],
58
- l
59
- ? l > 4
60
- ? l > 8
61
- ? t1 ^ (t0 | ~t2) // Round 4: I
62
- : t0 ^ t1 ^ t2 // Round 3: H
63
- : t0 & t2 | t1 & ~t2 // Round 2: G
64
- : t0 & t1 | ~t0 & t2 // Round 1: F
65
- ) +
66
- (
67
- 0 | x[i + (j * (0x7351 >> l) + (0x0510 >> l) & 15)]
46
+ x[xLen - 1] = l / 2 ** 29;
47
+ x[floor(l / 4)] |= 0x80 << (x[xLen - 2] = l << 3);
48
+
49
+ for (; i < xLen; i += 16) {
50
+ for (l = 0; l < 16; l += 4) {
51
+ for (j = 0; j < 16;) {
52
+ output[oi &= 3] = (
53
+ (
54
+ t1 = (
55
+ output[oi] +
56
+ K[l * 4 | j] +
57
+ (
58
+ t0 = output[++oi & 3],
59
+ t1 = output[++oi & 3],
60
+ t2 = output[++oi & 3],
61
+ l
62
+ ? l > 4
63
+ ? l > 8
64
+ ? t1 ^ (t0 | ~t2) // Round 4: I
65
+ : t0 ^ t1 ^ t2 // Round 3: H
66
+ : t0 & t2 | t1 & ~t2 // Round 2: G
67
+ : t0 & t1 | ~t0 & t2 // Round 1: F
68
+ ) +
69
+ x[i + (j * (0x7351 >> l) + (0x0510 >> l) & 15)]
68
70
  )
69
- )
70
- ) << (
71
- t2 = "',16%).4$+07&*/5".charCodeAt(j++ & 3 | l)
72
- ) | t1 >>> 64 - t2
73
- ) + t0
74
- ) {
75
- l = j >> 4 << 2
71
+ ) << (
72
+ t2 = "',16%).4$+07&*/5".charCodeAt(j++ & 3 | l)
73
+ ) | t1 >>> 64 - t2
74
+ ) + t0
75
+ }
76
76
  }
77
77
  for (; oi;) {
78
- oldOutput[--oi] = output[oi] = 0 | oldOutput[oi] + output[oi]
78
+ oldOutput[--oi] = output[oi] += oldOutput[oi]
79
79
  }
80
80
  }
81
81
  return output
@@ -87,7 +87,7 @@ let wordsMD5 = (
87
87
  * @param {*} input
88
88
  * @param {number} j pad int32 length
89
89
  *
90
- * @returns {[Int32Array<ArrayBuffer> | number[], number]}
90
+ * @returns {[Int32Array<ArrayBuffer>, number]}
91
91
  */
92
92
  let inputToWords = (
93
93
  input,
@@ -100,7 +100,7 @@ let inputToWords = (
100
100
  ).length,
101
101
  // Using `Array` to process inputs over 512 MiB could throw a `RangeError`,
102
102
  // so I replaced it with `Int32Array`.
103
- output = new Int32Array(wordsLen(j * 4 + byteLen)),
103
+ output = new $Int32Array(wordsLen(j * 4 + byteLen)),
104
104
  i = 0,
105
105
  ) => {
106
106
  for (; i < byteLen;) {
@@ -129,14 +129,12 @@ var md5 = (data, key, raw) => {
129
129
  var i = 16
130
130
  , hasKey = key != null
131
131
  , [bdata, temp] = inputToWords(data, /**@type {*}*/(hasKey) * i)
132
-
133
- /** @type {*} */
134
- var out = raw ? new Uint8Array(i) : ''
132
+ , out = new Uint8Array(i)
135
133
 
136
134
  if (hasKey) {
137
135
  // HMAC
138
136
  let [bkey, j] = inputToWords(key, 0)
139
- , opad = []
137
+ , opad = new $Int32Array(32)
140
138
  if (j > 64) {
141
139
  bkey = wordsMD5(bkey, j)
142
140
  }
@@ -144,26 +142,19 @@ var md5 = (data, key, raw) => {
144
142
  // (0x36363636 ^ 0x5c5c5c5c) == 0x6a6a6a6a
145
143
  opad[--j] = 0x6a6a6a6a ^ (bdata[j] = 0x36363636 ^ bkey[j])
146
144
  }
147
- bdata = opad.concat(wordsMD5(bdata, 64 + temp))
145
+ opad.set(wordsMD5(bdata, 64 + temp), 16)
146
+ bdata = opad
148
147
  temp = 80
149
148
  }
150
149
 
151
150
  bdata = wordsMD5(bdata, temp)
152
151
 
153
152
  // words to bytes or hex
154
- for (; i;
155
- raw
156
- ? out[i] = temp
157
- : out = (temp >> 4 && '') + temp.toString(16) + out
158
- ) {
159
- temp = bdata[--i >> 2] >> i * 8 & 0xff
153
+ for (; i;) {
154
+ out[--i] = bdata[i >> 2] >> i * 8
160
155
  }
161
156
 
162
- return out
163
- }
164
-
165
- for (; i < 64;) {
166
- K[i] = 0 | 2 ** 32 * $Math.abs($Math.sin(++i))
157
+ return raw ? out : out.toHex()
167
158
  }
168
159
 
169
160
  export default md5
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tinyhmacmd5",
3
- "version": "0.2.3",
4
- "description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 982 bytes.",
3
+ "version": "0.3.0-es2026",
4
+ "description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only 963 bytes.",
5
5
  "author": "bddjr",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -54,16 +54,20 @@
54
54
  "url": "git+https://github.com/bddjr/tinyhmacmd5.git"
55
55
  },
56
56
  "devDependencies": {
57
+ "blueimp-md5": "^2.19.0",
58
+ "crypto-js": "^4.2.0",
57
59
  "es-check": "^9.6.4",
60
+ "js-md5": "^0.9.2",
58
61
  "terser": "^5.49.0",
59
62
  "tinyhmacmd5": "link:",
60
63
  "typescript": "^7.0.2"
61
64
  },
62
65
  "scripts": {
63
- "build": "tsc && node scripts/build.mjs && es-check es2016 --module --checkFeatures main.js browser.min.js",
66
+ "build": "tsc && node scripts/build.mjs && es-check es2026 --module --checkFeatures main.js browser.min.js",
64
67
  "build:unchanged": "node scripts/build-unchanged.mjs",
65
68
  "test": "pnpm build && pnpm test:only",
66
- "test:only": "node scripts/test.mjs",
67
- "test:unchanged": "pnpm build:unchanged && pnpm test:only"
69
+ "test:only": "node scripts/test.mjs && node scripts/test-513MiB.mjs",
70
+ "test:unchanged": "pnpm build:unchanged && pnpm test:only",
71
+ "benchmark": "node scripts/benchmark.mjs && node scripts/test-513MiB.mjs"
68
72
  }
69
73
  }