tinyhmacmd5 0.0.5 → 0.0.7
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 +3 -1
- package/browser.min.js +2 -2
- package/main.d.ts +2 -2
- package/main.js +80 -92
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
A tiny JavaScript HMAC-MD5 implementation.
|
|
4
4
|
|
|
5
|
-
The minified browser bundle ([`browser.min.js`](browser.min.js)) is only
|
|
5
|
+
The minified browser bundle ([`browser.min.js`](browser.min.js)) is only 965 bytes.
|
|
6
|
+
|
|
7
|
+
Preview: https://bddjr.github.io/tinyhmacmd5/
|
|
6
8
|
|
|
7
9
|
## Setup
|
|
8
10
|
|
package/browser.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/** @license https://github.
|
|
2
|
-
{let r=
|
|
1
|
+
/** @license https://bddjr.github.io/tinyhmacmd5/lic */
|
|
2
|
+
{let r=1732584193,e=271733878,t=~r,n=~e,o=[],f=(f,a,h=0,i,l,u,d,c,g=[r,n,t,e],s=[...g],y=r=>g[3&++c])=>{for(f[a+64>>>9<<4|14]=0|a,f[a>>>5]|=128<<a%32;h<f.length;h+=16){for(c=i=0;i<64;c-=6)g[3&c]=0|((l=0|(l=y(),u=y(),d=y(),((a=i>>4<<2)?a>4?a>8?u^(l|~d):l^u^d:l&d|u&~d:l&u|~l&d)+(0|f[h+(i*(29521>>a)+(1296>>a)&15)])+(u="',16%).4$+07&*/5".charCodeAt(a+i%4)-32,o[63-i++]??=0|2**32*Math.abs(Math.sin(i)))+y()))<<u|l>>>32-u)+y();for(c=4;c;)s[--c]=g[c]=0|s[c]+g[c]}return g},a=(r,e,t=("string"==typeof r?r=(new TextEncoder).encode(r):r).length,n=[],o=8*t)=>{for(;t;)n[e+(--t>>>2)]|=r[t]<<t%4*8;return[n,o]};var md5=(r,e,t)=>{var n=16,o=null!=e,[h,i]=a(r,o*n),l=new Uint8Array(n);if(o){let[r,t]=a(e,0),o=e=>{for(;n;)h[--n]=16843009*e^r[n];n=16};t>512&&(r=f(r,t)),o(54),(h=f(h,512+i)).unshift(...Array(16)),o(92),i=640}for(h=f(h,i);n;)l[--n]=h[n>>2]>>>n%4*8;return t?l:l.reduce((r,e)=>r+(e>>4&&"")+e.toString(16),"")}}
|
package/main.d.ts
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
* @param {boolean} [raw] If true, the hash is returned as raw bytes (Uint8Array); otherwise, as a hex string.
|
|
10
10
|
* @returns {string | Uint8Array<ArrayBuffer>} The MD5 (or HMAC‑MD5) digest, either as a hex string or a Uint8Array.
|
|
11
11
|
*/
|
|
12
|
-
var md5: {
|
|
12
|
+
declare var md5: {
|
|
13
13
|
(data: string | Uint8Array, key?: string | Uint8Array | null, raw?: false): string;
|
|
14
|
-
(data: string | Uint8Array, key
|
|
14
|
+
(data: string | Uint8Array, key: string | Uint8Array | null | undefined, raw: true): Uint8Array<ArrayBuffer>;
|
|
15
15
|
};
|
|
16
16
|
export default md5;
|
package/main.js
CHANGED
|
@@ -1,85 +1,73 @@
|
|
|
1
|
-
/** @license https://github.
|
|
1
|
+
/** @license https://bddjr.github.io/tinyhmacmd5/lic */
|
|
2
2
|
|
|
3
3
|
// Adapted from https://github.com/blueimp/JavaScript-MD5
|
|
4
4
|
|
|
5
|
-
/** @type {"length"} */
|
|
6
|
-
let $length = "length"
|
|
7
|
-
|
|
8
|
-
/** @type {16} */
|
|
9
|
-
let $16 = 16
|
|
10
|
-
|
|
11
|
-
let $2_32 = 2 ** 32
|
|
12
|
-
|
|
13
5
|
let A = 1732584193
|
|
14
6
|
, D = 271733878
|
|
15
7
|
, C = ~A
|
|
16
8
|
, B = ~D
|
|
17
9
|
|
|
18
|
-
let $Math = Math
|
|
19
|
-
|
|
20
|
-
let floor = $Math.floor
|
|
21
|
-
|
|
22
|
-
/** @type {number[]} */
|
|
23
|
-
let S = [
|
|
24
|
-
738695, // 7, 12, 17, 22
|
|
25
|
-
669989, // 5, 9, 14, 20
|
|
26
|
-
770404, // 4, 11, 16, 23
|
|
27
|
-
703814, // 6, 10, 15, 21
|
|
28
|
-
]
|
|
29
|
-
|
|
30
10
|
/** @type {number[]} MD5 constants cached in memory */
|
|
31
|
-
let K =
|
|
11
|
+
let K = []
|
|
32
12
|
|
|
33
13
|
/**
|
|
34
14
|
* Calculate the MD5 of an array of little-endian words, and a bit length.
|
|
35
15
|
*
|
|
36
16
|
* @param {number[]} x Array of little-endian words
|
|
37
|
-
* @param {number}
|
|
38
|
-
*
|
|
17
|
+
* @param {number} l Bit length
|
|
18
|
+
*
|
|
19
|
+
* @param {number} [j]
|
|
20
|
+
* @param {number} [t0]
|
|
21
|
+
* @param {number} [t1]
|
|
22
|
+
* @param {number} [t2]
|
|
23
|
+
* @param {number} [oi]
|
|
24
|
+
*
|
|
25
|
+
* @returns {number[]} MD5 Array
|
|
39
26
|
*/
|
|
40
|
-
let binlMD5 = (
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
27
|
+
let binlMD5 = (
|
|
28
|
+
x,
|
|
29
|
+
l,
|
|
30
|
+
// var:
|
|
31
|
+
i = 0,
|
|
32
|
+
j,
|
|
33
|
+
t0,
|
|
34
|
+
t1,
|
|
35
|
+
t2,
|
|
36
|
+
oi,
|
|
37
|
+
output = [A, B, C, D],
|
|
38
|
+
oldOutput = [...output],
|
|
39
|
+
o = (
|
|
40
|
+
/** @param {*} [_] */
|
|
41
|
+
_ => output[++oi & 3]
|
|
42
|
+
),
|
|
43
|
+
) => {
|
|
55
44
|
|
|
56
45
|
// append padding
|
|
57
|
-
x[
|
|
58
|
-
x[
|
|
59
|
-
x[j + 1] = 0 | bitLen / $2_32;
|
|
46
|
+
x[l + 64 >>> 9 << 4 | 14] = 0 | l;
|
|
47
|
+
x[l >>> 5] |= 0x80 << l % 32;
|
|
60
48
|
|
|
61
|
-
for (; i < x
|
|
49
|
+
for (; i < x.length; i += 16) {
|
|
62
50
|
for (oi = j = 0; j < 64; oi -= 6) {
|
|
63
51
|
output[oi & 3] = (
|
|
64
|
-
t0 = (
|
|
52
|
+
t0 = 0 | (
|
|
65
53
|
(
|
|
66
54
|
t0 = o(),
|
|
67
55
|
t1 = o(),
|
|
68
56
|
t2 = o(),
|
|
69
|
-
(l = j >> 4)
|
|
70
|
-
? l >
|
|
71
|
-
? l >
|
|
72
|
-
? t1 ^ (t0 | ~t2) //
|
|
73
|
-
: t0 ^ t1 ^ t2 //
|
|
74
|
-
: t0 & t2 | t1 & ~t2 //
|
|
75
|
-
: t0 & t1 | ~t0 & t2 //
|
|
57
|
+
(l = j >> 4 << 2)
|
|
58
|
+
? l > 4
|
|
59
|
+
? l > 8
|
|
60
|
+
? t1 ^ (t0 | ~t2) // Round 4: II
|
|
61
|
+
: t0 ^ t1 ^ t2 // Round 3: HH
|
|
62
|
+
: t0 & t2 | t1 & ~t2 // Round 2: GG
|
|
63
|
+
: t0 & t1 | ~t0 & t2 // Round 1: FF
|
|
76
64
|
) +
|
|
77
65
|
(
|
|
78
|
-
0 | x[i + (j * (0x7351 >> l
|
|
66
|
+
0 | x[i + (j * (0x7351 >> l) + (0x0510 >> l) & 15)]
|
|
79
67
|
) +
|
|
80
68
|
(
|
|
81
|
-
t1 =
|
|
82
|
-
K[j++] ??= 0 |
|
|
69
|
+
t1 = "',16%).4$+07&*/5".charCodeAt(l + j % 4) - 32,
|
|
70
|
+
K[63 - j++] ??= 0 | 2 ** 32 * Math.abs(Math.sin(j))
|
|
83
71
|
) +
|
|
84
72
|
o()
|
|
85
73
|
),
|
|
@@ -96,29 +84,31 @@ let binlMD5 = (x, bitLen) => {
|
|
|
96
84
|
/**
|
|
97
85
|
* Convert bytes to an array of little-endian words
|
|
98
86
|
*
|
|
99
|
-
* @param {Uint8Array} input
|
|
100
|
-
* @
|
|
87
|
+
* @param {string | Uint8Array} input
|
|
88
|
+
* @param {number} padLen
|
|
89
|
+
*
|
|
90
|
+
* @param {number[]} [output]
|
|
91
|
+
*
|
|
92
|
+
* @returns {[number[], number]}
|
|
101
93
|
*/
|
|
102
|
-
let
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
var
|
|
94
|
+
let inputToBinl = (
|
|
95
|
+
input,
|
|
96
|
+
padLen,
|
|
97
|
+
// var:
|
|
98
|
+
i = (
|
|
99
|
+
typeof input == 'string'
|
|
100
|
+
? input = new TextEncoder().encode(input)
|
|
101
|
+
: input
|
|
102
|
+
).length,
|
|
103
|
+
output = [],
|
|
104
|
+
bitLen = 8 * i,
|
|
105
|
+
) => {
|
|
106
106
|
for (; i;) {
|
|
107
|
-
output[
|
|
107
|
+
output[padLen + (--i >>> 2)] |= /**@type {Uint8Array}*/(input)[i] << i % 4 * 8
|
|
108
108
|
}
|
|
109
|
-
return output
|
|
109
|
+
return [output, bitLen]
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
/**
|
|
113
|
-
* @param {string | Uint8Array} input
|
|
114
|
-
* @returns {Uint8Array}
|
|
115
|
-
*/
|
|
116
|
-
let inputToBytes = (input) => (
|
|
117
|
-
typeof input == 'string'
|
|
118
|
-
? new TextEncoder().encode(input)
|
|
119
|
-
: input
|
|
120
|
-
)
|
|
121
|
-
|
|
122
112
|
/**
|
|
123
113
|
* @overload
|
|
124
114
|
* @param {string | Uint8Array} data
|
|
@@ -129,7 +119,7 @@ let inputToBytes = (input) => (
|
|
|
129
119
|
/**
|
|
130
120
|
* @overload
|
|
131
121
|
* @param {string | Uint8Array} data
|
|
132
|
-
* @param {string | Uint8Array | null}
|
|
122
|
+
* @param {string | Uint8Array | null | undefined} key
|
|
133
123
|
* @param {true} raw
|
|
134
124
|
* @returns {Uint8Array<ArrayBuffer>}
|
|
135
125
|
*/
|
|
@@ -145,30 +135,28 @@ let inputToBytes = (input) => (
|
|
|
145
135
|
* @returns {string | Uint8Array<ArrayBuffer>} The MD5 (or HMAC‑MD5) digest, either as a hex string or a Uint8Array.
|
|
146
136
|
*/
|
|
147
137
|
var md5 = (data, key, raw) => {
|
|
148
|
-
var
|
|
149
|
-
,
|
|
150
|
-
,
|
|
151
|
-
, out = new Uint8Array(
|
|
152
|
-
/** @type {*} */
|
|
153
|
-
var bkey
|
|
154
|
-
, pad = (/**@type {number}*/ x) => {
|
|
155
|
-
bdata.unshift(...Array($16))
|
|
156
|
-
for (; i;) {
|
|
157
|
-
bdata[--i] = 0x01010101 * x ^ bkey[i]
|
|
158
|
-
}
|
|
159
|
-
i = $16
|
|
160
|
-
}
|
|
138
|
+
var i = 16
|
|
139
|
+
, hasKey = key != null
|
|
140
|
+
, [bdata, bitLen] = inputToBinl(data, /**@type {*}*/(hasKey) * i)
|
|
141
|
+
, out = new Uint8Array(i)
|
|
161
142
|
|
|
162
|
-
if (
|
|
143
|
+
if (hasKey) {
|
|
163
144
|
// HMAC
|
|
164
|
-
bkey =
|
|
165
|
-
|
|
166
|
-
|
|
145
|
+
let [bkey, keyBitlen] = inputToBinl(key, 0)
|
|
146
|
+
, pad = (/**@type {number}*/ x) => {
|
|
147
|
+
for (; i;) {
|
|
148
|
+
bdata[--i] = 0x01010101 * x ^ bkey[i]
|
|
149
|
+
}
|
|
150
|
+
i = 16
|
|
151
|
+
}
|
|
152
|
+
if (keyBitlen > 512) {
|
|
153
|
+
bkey = binlMD5(bkey, keyBitlen)
|
|
167
154
|
}
|
|
168
155
|
pad(0x36)
|
|
169
156
|
bdata = binlMD5(bdata, 512 + bitLen)
|
|
157
|
+
bdata.unshift(...Array(16))
|
|
170
158
|
pad(0x5c)
|
|
171
|
-
bitLen =
|
|
159
|
+
bitLen = 640
|
|
172
160
|
}
|
|
173
161
|
|
|
174
162
|
bdata = binlMD5(bdata, bitLen)
|
|
@@ -180,7 +168,7 @@ var md5 = (data, key, raw) => {
|
|
|
180
168
|
|
|
181
169
|
return raw
|
|
182
170
|
? out
|
|
183
|
-
: out.reduce((p, v) => p + (v >> 4 && '') + v.toString(
|
|
171
|
+
: out.reduce((p, v) => p + (v >> 4 && '') + v.toString(16), '')
|
|
184
172
|
}
|
|
185
173
|
|
|
186
174
|
export default md5
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tinyhmacmd5",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "A tiny JavaScript HMAC-MD5 implementation. The minified browser bundle is only
|
|
3
|
+
"version": "0.0.7",
|
|
4
|
+
"description": "A tiny JavaScript HMAC-MD5 implementation. The minified browser bundle is only 965 bytes.",
|
|
5
5
|
"author": "bddjr",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"main.d.ts",
|
|
14
14
|
"browser.min.js"
|
|
15
15
|
],
|
|
16
|
+
"homepage": "https://bddjr.github.io/tinyhmacmd5/",
|
|
16
17
|
"keywords": [
|
|
17
18
|
"md5",
|
|
18
19
|
"hmac",
|
|
@@ -31,10 +32,11 @@
|
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"es-check": "^9.6.4",
|
|
33
34
|
"terser": "^5.49.0",
|
|
34
|
-
"tinyhmacmd5": "link:"
|
|
35
|
+
"tinyhmacmd5": "link:",
|
|
36
|
+
"typescript": "^7.0.2"
|
|
35
37
|
},
|
|
36
38
|
"scripts": {
|
|
37
|
-
"build": "node scripts/build.mjs && es-check es2021 --module --checkFeatures main.js browser.min.js",
|
|
39
|
+
"build": "tsc && node scripts/build.mjs && es-check es2021 --module --checkFeatures main.js browser.min.js",
|
|
38
40
|
"test": "pnpm build && node scripts/test.mjs"
|
|
39
41
|
}
|
|
40
42
|
}
|