tinyhmacmd5 0.0.5 → 0.0.6

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 @@
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 1029 bytes.
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.com/bddjr/tinyhmacmd5/blob/HEAD/LICENSE */
2
- {let r="length",e=16,n=2**32,t=1732584193,o=271733878,a=~t,f=~o,u=Math,i=u.floor,l=[738695,669989,770404,703814],d=Array(64),s=(s,v)=>{var y,c,g,h,A,w=0,b=i((v+64)/512)*e+14,m=[t,f,a,o],p=[...m],x=r=>m[3&++A];for(s[i(v/32)]|=128<<v%32,s[b]=0|v,s[b+1]=0|v/n;w<s[r];w+=e){for(A=b=0;b<64;A-=6)m[3&A]=(c=x(),g=x(),h=x(),0|((c=((y=b>>4)?y>1?y>2?g^(c|~h):c^g^h:c&h|g&~h:c&g|~c&h)+(0|s[w+(b*(29521>>4*y)+(1296>>4*y)&15)])+(g=l[y]>>b%4*5&31,d[b++]??=0|n*u.abs(u.sin(b)))+x())<<g|c>>>32-g)+x());for(A=4;A;)p[--A]=m[A]=0|p[A]+m[A]}return m},v=e=>{for(var n=e[r],t=[];n;)t[i(--n/4)]|=e[n]<<n%4*8;return t},y=r=>"string"==typeof r?(new TextEncoder).encode(r):r;var md5=(n,t,o)=>{var a,f=v(n=y(n)),u=8*n[r],i=e,l=new Uint8Array(e),d=r=>{for(f.unshift(...Array(e));i;)f[--i]=16843009*r^a[i];i=e};for(null!=t&&((a=v(t=y(t)))[r]>e&&(a=s(a,8*t[r])),d(54),f=s(f,512+u),d(92),u=640),f=s(f,u);i;)l[--i]=f[i>>2]>>>i%4*8;return o?l:l.reduce((r,n)=>r+(n>>4&&"")+n.toString(e),"")}}
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.js CHANGED
@@ -1,85 +1,70 @@
1
- /** @license https://github.com/bddjr/tinyhmacmd5/blob/HEAD/LICENSE */
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 = Array(64)
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} bitLen Bit length
38
- * @returns {[number, number, number, number]} MD5 Array
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 = (x, bitLen) => {
41
- var i = 0
42
- , j = floor((bitLen + 64) / 512) * $16 + 14
43
- , l
44
- , t0
45
- , t1
46
- , t2
47
-
48
- /** @type {number} */
49
- var oi
50
-
51
- /** @type {[number, number, number, number]} */
52
- var output = [A, B, C, D]
53
- , oldOutput = [...output]
54
- , o = (/**@type {*}*/ _) => output[++oi & 3]
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 = (/**@type {*}*/ _) => output[++oi & 3],
40
+ ) => {
55
41
 
56
42
  // append padding
57
- x[floor(bitLen / 32)] |= 0x80 << bitLen % 32;
58
- x[j] = 0 | bitLen;
59
- x[j + 1] = 0 | bitLen / $2_32;
43
+ x[l + 64 >>> 9 << 4 | 14] = 0 | l;
44
+ x[l >>> 5] |= 0x80 << l % 32;
60
45
 
61
- for (; i < x[$length]; i += $16) {
46
+ for (; i < x.length; i += 16) {
62
47
  for (oi = j = 0; j < 64; oi -= 6) {
63
48
  output[oi & 3] = (
64
- t0 = (
49
+ t0 = 0 | (
65
50
  (
66
51
  t0 = o(),
67
52
  t1 = o(),
68
53
  t2 = o(),
69
- (l = j >> 4)
70
- ? l > 1
71
- ? l > 2
72
- ? t1 ^ (t0 | ~t2) // 3
73
- : t0 ^ t1 ^ t2 // 2
74
- : t0 & t2 | t1 & ~t2 // 1
75
- : t0 & t1 | ~t0 & t2 // 0
54
+ (l = j >> 4 << 2)
55
+ ? l > 4
56
+ ? l > 8
57
+ ? t1 ^ (t0 | ~t2) // Round 4: II
58
+ : t0 ^ t1 ^ t2 // Round 3: HH
59
+ : t0 & t2 | t1 & ~t2 // Round 2: GG
60
+ : t0 & t1 | ~t0 & t2 // Round 1: FF
76
61
  ) +
77
62
  (
78
- 0 | x[i + (j * (0x7351 >> l * 4) + (0x0510 >> l * 4) & 15)]
63
+ 0 | x[i + (j * (0x7351 >> l) + (0x0510 >> l) & 15)]
79
64
  ) +
80
65
  (
81
- t1 = S[l] >> j % 4 * 5 & 31,
82
- K[j++] ??= 0 | $2_32 * $Math.abs($Math.sin(j))
66
+ t1 = "',16%).4$+07&*/5".charCodeAt(l + j % 4) - 32,
67
+ K[63 - j++] ??= 0 | 2 ** 32 * Math.abs(Math.sin(j))
83
68
  ) +
84
69
  o()
85
70
  ),
@@ -96,29 +81,31 @@ let binlMD5 = (x, bitLen) => {
96
81
  /**
97
82
  * Convert bytes to an array of little-endian words
98
83
  *
99
- * @param {Uint8Array} input Raw bytes
100
- * @returns {number[]} Array of little-endian words
84
+ * @param {string | Uint8Array} input
85
+ * @param {number} padLen
86
+ *
87
+ * @param {number[]} [output]
88
+ *
89
+ * @returns {[number[], number]}
101
90
  */
102
- let bytesToBinl = (input) => {
103
- var i = input[$length]
104
- /** @type {number[]} */
105
- var output = []
91
+ let inputToBinl = (
92
+ input,
93
+ padLen,
94
+ // var:
95
+ i = (
96
+ typeof input == 'string'
97
+ ? input = new TextEncoder().encode(input)
98
+ : input
99
+ ).length,
100
+ output = [],
101
+ bitLen = 8 * i,
102
+ ) => {
106
103
  for (; i;) {
107
- output[floor(--i / 4)] |= input[i] << i % 4 * 8
104
+ output[padLen + (--i >>> 2)] |= /**@type {Uint8Array}*/(input)[i] << i % 4 * 8
108
105
  }
109
- return output
106
+ return [output, bitLen]
110
107
  }
111
108
 
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
109
  /**
123
110
  * @overload
124
111
  * @param {string | Uint8Array} data
@@ -145,30 +132,28 @@ let inputToBytes = (input) => (
145
132
  * @returns {string | Uint8Array<ArrayBuffer>} The MD5 (or HMAC‑MD5) digest, either as a hex string or a Uint8Array.
146
133
  */
147
134
  var md5 = (data, key, raw) => {
148
- var bdata = bytesToBinl(data = inputToBytes(data))
149
- , bitLen = data[$length] * 8
150
- , i = $16
151
- , out = new Uint8Array($16)
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
- }
135
+ var i = 16
136
+ , hasKey = key != null
137
+ , [bdata, bitLen] = inputToBinl(data, /**@type {*}*/(hasKey) * i)
138
+ , out = new Uint8Array(i)
161
139
 
162
- if (key != null) {
140
+ if (hasKey) {
163
141
  // HMAC
164
- bkey = bytesToBinl(key = inputToBytes(key))
165
- if (bkey[$length] > $16) {
166
- bkey = binlMD5(bkey, key[$length] * 8)
142
+ let [bkey, keyBitlen] = inputToBinl(key, 0)
143
+ , pad = (/**@type {number}*/ x) => {
144
+ for (; i;) {
145
+ bdata[--i] = 0x01010101 * x ^ bkey[i]
146
+ }
147
+ i = 16
148
+ }
149
+ if (keyBitlen > 512) {
150
+ bkey = binlMD5(bkey, keyBitlen)
167
151
  }
168
152
  pad(0x36)
169
153
  bdata = binlMD5(bdata, 512 + bitLen)
154
+ bdata.unshift(...Array(16))
170
155
  pad(0x5c)
171
- bitLen = 512 + 128
156
+ bitLen = 640
172
157
  }
173
158
 
174
159
  bdata = binlMD5(bdata, bitLen)
@@ -180,7 +165,7 @@ var md5 = (data, key, raw) => {
180
165
 
181
166
  return raw
182
167
  ? out
183
- : out.reduce((p, v) => p + (v >> 4 && '') + v.toString($16), '')
168
+ : out.reduce((p, v) => p + (v >> 4 && '') + v.toString(16), '')
184
169
  }
185
170
 
186
171
  export default md5
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tinyhmacmd5",
3
- "version": "0.0.5",
4
- "description": "A tiny JavaScript HMAC-MD5 implementation. The minified browser bundle is only 1029 bytes.",
3
+ "version": "0.0.6",
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",