tinyhmacmd5 0.2.2-es2026 → 0.2.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 +20 -19
- package/browser.min.js +1 -1
- package/main.js +24 -20
- package/package.json +3 -3
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 **
|
|
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`
|
|
@@ -10,11 +10,13 @@ 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
|
+
|
|
13
15
|
`tinyhmacmd5` does not simply aim for the smallest possible size.
|
|
14
16
|
Its goal is to balance code size and runtime performance, so some performance-oriented implementations are intentionally retained.
|
|
15
17
|
As a result, the final size is not the smallest theoretically achievable.
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
If you need a smaller implementation that targets ECMAScript 2026, see the [`es2026`](https://github.com/bddjr/tinyhmacmd5/tree/es2026) branch.
|
|
18
20
|
|
|
19
21
|
> [!WARNING]
|
|
20
22
|
> MD5 is cryptographically broken and unsafe for security-sensitive applications.
|
|
@@ -24,7 +26,6 @@ As a result, the final size is not the smallest theoretically achievable.
|
|
|
24
26
|
|
|
25
27
|
### npm
|
|
26
28
|
|
|
27
|
-
|
|
28
29
|
```
|
|
29
30
|
npm i tinyhmacmd5
|
|
30
31
|
```
|
|
@@ -42,7 +43,7 @@ You can also use other package managers (e.g. `pnpm` or `yarn`) in place of `npm
|
|
|
42
43
|
See https://www.jsdelivr.com/package/npm/tinyhmacmd5
|
|
43
44
|
|
|
44
45
|
```html
|
|
45
|
-
<script src="https://cdn.jsdelivr.net/npm/tinyhmacmd5
|
|
46
|
+
<script src="https://cdn.jsdelivr.net/npm/tinyhmacmd5"></script>
|
|
46
47
|
```
|
|
47
48
|
|
|
48
49
|
It will define the `md5` function using `var`.
|
|
@@ -50,7 +51,7 @@ It will define the `md5` function using `var`.
|
|
|
50
51
|
### UNPKG
|
|
51
52
|
|
|
52
53
|
```html
|
|
53
|
-
<script src="https://unpkg.com/tinyhmacmd5
|
|
54
|
+
<script src="https://unpkg.com/tinyhmacmd5"></script>
|
|
54
55
|
```
|
|
55
56
|
|
|
56
57
|
It will define the `md5` function using `var`.
|
|
@@ -141,22 +142,22 @@ md5(data)
|
|
|
141
142
|
|
|
142
143
|
## Runtime Environment
|
|
143
144
|
|
|
144
|
-
Environments that support [
|
|
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) :
|
|
145
146
|
|
|
146
147
|
- Desktop
|
|
147
|
-
- Chrome ≥
|
|
148
|
-
- Edge ≥
|
|
149
|
-
- Firefox ≥
|
|
150
|
-
- Opera ≥
|
|
151
|
-
- Safari ≥
|
|
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)
|
|
152
153
|
- Mobile
|
|
153
|
-
- Chrome Android ≥
|
|
154
|
-
- Firefox for Android ≥
|
|
155
|
-
- Opera Android ≥
|
|
156
|
-
- Safari on iOS ≥
|
|
157
|
-
- Samsung Browser
|
|
158
|
-
- WebView Android ≥
|
|
159
|
-
- WebView on iOS ≥
|
|
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)
|
|
160
161
|
|
|
161
162
|
Not recommended for use in environments that support `node:crypto`, as `node:crypto` already provides HMAC-MD5.
|
|
162
163
|
|
|
@@ -178,7 +179,7 @@ During the adaptation, I discovered that `blueimp-md5` did not correctly handle
|
|
|
178
179
|
|
|
179
180
|
I also found that using `Array` to process inputs over 512 MiB could throw a `RangeError`, so I replaced it with `Int32Array`.
|
|
180
181
|
|
|
181
|
-
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (
|
|
182
|
+
I demonstrated in practice that HMAC-MD5 can be implemented in an extremely small footprint (982 bytes) while also improving reliability.
|
|
182
183
|
|
|
183
184
|
Maybe not many people care about saving just a few KB, but `tinyhmacmd5` exists precisely to "explore the unknown".
|
|
184
185
|
|
package/browser.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
/*! npmjs.com/tinyhmacmd5 */{let r=[],
|
|
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))}
|
package/main.js
CHANGED
|
@@ -44,8 +44,8 @@ let wordsMD5 = (
|
|
|
44
44
|
x[j - 1] = 0 | l / 2 ** 29;
|
|
45
45
|
x[floor(l / 4)] |= 0x80 << (x[j - 2] = l << 3);
|
|
46
46
|
|
|
47
|
-
for (; i < x.length;) {
|
|
48
|
-
for (j = 0; j < 64;
|
|
47
|
+
for (; i < x.length; i += 16) {
|
|
48
|
+
for (j = 0; j < 64;
|
|
49
49
|
output[oi &= 3] = 0 | (
|
|
50
50
|
(
|
|
51
51
|
t1 = 0 | (
|
|
@@ -55,7 +55,7 @@ let wordsMD5 = (
|
|
|
55
55
|
t0 = output[++oi & 3],
|
|
56
56
|
t1 = output[++oi & 3],
|
|
57
57
|
t2 = output[++oi & 3],
|
|
58
|
-
|
|
58
|
+
l
|
|
59
59
|
? l > 4
|
|
60
60
|
? l > 8
|
|
61
61
|
? t1 ^ (t0 | ~t2) // Round 4: I
|
|
@@ -71,8 +71,10 @@ let wordsMD5 = (
|
|
|
71
71
|
t2 = "',16%).4$+07&*/5".charCodeAt(j++ & 3 | l)
|
|
72
72
|
) | t1 >>> 64 - t2
|
|
73
73
|
) + t0
|
|
74
|
+
) {
|
|
75
|
+
l = j >> 4 << 2
|
|
74
76
|
}
|
|
75
|
-
for (; oi;
|
|
77
|
+
for (; oi;) {
|
|
76
78
|
oldOutput[--oi] = output[oi] = 0 | oldOutput[oi] + output[oi]
|
|
77
79
|
}
|
|
78
80
|
}
|
|
@@ -126,36 +128,38 @@ let inputToWords = (
|
|
|
126
128
|
var md5 = (data, key, raw) => {
|
|
127
129
|
var i = 16
|
|
128
130
|
, hasKey = key != null
|
|
129
|
-
, [bdata,
|
|
131
|
+
, [bdata, temp] = inputToWords(data, /**@type {*}*/(hasKey) * i)
|
|
130
132
|
|
|
131
133
|
/** @type {*} */
|
|
132
|
-
var
|
|
133
|
-
|
|
134
|
-
var out = new Uint8Array(i)
|
|
134
|
+
var out = raw ? new Uint8Array(i) : ''
|
|
135
135
|
|
|
136
136
|
if (hasKey) {
|
|
137
137
|
// HMAC
|
|
138
|
-
let [bkey,
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
let [bkey, j] = inputToWords(key, 0)
|
|
139
|
+
, opad = []
|
|
140
|
+
if (j > 64) {
|
|
141
|
+
bkey = wordsMD5(bkey, j)
|
|
141
142
|
}
|
|
142
|
-
for (
|
|
143
|
+
for (j = i; j;) {
|
|
143
144
|
// (0x36363636 ^ 0x5c5c5c5c) == 0x6a6a6a6a
|
|
144
|
-
|
|
145
|
+
opad[--j] = 0x6a6a6a6a ^ (bdata[j] = 0x36363636 ^ bkey[j])
|
|
145
146
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
dataByteLen = 80
|
|
147
|
+
bdata = opad.concat(wordsMD5(bdata, 64 + temp))
|
|
148
|
+
temp = 80
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
bdata = wordsMD5(bdata,
|
|
151
|
+
bdata = wordsMD5(bdata, temp)
|
|
152
152
|
|
|
153
153
|
// words to bytes or hex
|
|
154
|
-
for (; i;
|
|
155
|
-
|
|
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
|
|
156
160
|
}
|
|
157
161
|
|
|
158
|
-
return
|
|
162
|
+
return out
|
|
159
163
|
}
|
|
160
164
|
|
|
161
165
|
for (; i < 64;) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tinyhmacmd5",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "A tiny and reliable HMAC-MD5 implementation for JavaScript: browser.min.js is only
|
|
3
|
+
"version": "0.2.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",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"typescript": "^7.0.2"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
|
-
"build": "tsc && node scripts/build.mjs && es-check
|
|
63
|
+
"build": "tsc && node scripts/build.mjs && es-check es2016 --module --checkFeatures main.js browser.min.js",
|
|
64
64
|
"build:unchanged": "node scripts/build-unchanged.mjs",
|
|
65
65
|
"test": "pnpm build && pnpm test:only",
|
|
66
66
|
"test:only": "node scripts/test.mjs",
|