tinyhmacmd5 0.0.1 → 0.0.2
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 +1 -1
- package/browser.min.js +1 -1
- package/main.js +103 -98
- package/package.json +2 -2
package/README.md
CHANGED
package/browser.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** @license https://github.com/bddjr/tinyhmacmd5/blob/HEAD/LICENSE */
|
|
2
|
-
{let r="length",e=
|
|
2
|
+
{let r="length",e=16,t=Math.floor,n=(r,e)=>0|(0|r)+(0|e),o=(r,e)=>r<<e|r>>>32-e,f=(r,e,t,f,l,a)=>n(o(n(n(e,r),n(f,a)),l),t),l=(r,e,t,n,o,l,a)=>f(e&t|~e&n,r,e,o,l,a),a=(r,e,t,n,o,l,a)=>f(e&n|t&~n,r,e,o,l,a),i=(r,e,t,n,o,l,a)=>f(e^t^n,r,e,o,l,a),u=(r,e,t,n,o,l,a)=>f(t^(e|~n),r,e,o,l,a),c=(o,f)=>{var c,d,v,g,h=0,s=1732584193,y=-271733879,w=-1732584194,A=271733878,m=t((f+64)/512)*e+14;for(o[t(f/32)]|=128<<f%32,o[m]=0|f,o[m+1]=0|f/4294967296;h<o[r];A=n(A,g)){let[r,t,f,m,p,x,E,M,S,T,U,b,j,k,q,z]=o.slice(h,h+=e);c=s,d=y,v=w,g=A,s=l(s,y,w,A,r,7,-680876936),A=l(A,s,y,w,t,12,-389564586),w=l(w,A,s,y,f,17,606105819),y=l(y,w,A,s,m,22,-1044525330),s=l(s,y,w,A,p,7,-176418897),A=l(A,s,y,w,x,12,1200080426),w=l(w,A,s,y,E,17,-1473231341),y=l(y,w,A,s,M,22,-45705983),s=l(s,y,w,A,S,7,1770035416),A=l(A,s,y,w,T,12,-1958414417),w=l(w,A,s,y,U,17,-42063),y=l(y,w,A,s,b,22,-1990404162),s=l(s,y,w,A,j,7,1804603682),A=l(A,s,y,w,k,12,-40341101),w=l(w,A,s,y,q,17,-1502002290),y=l(y,w,A,s,z,22,1236535329),s=a(s,y,w,A,t,5,-165796510),A=a(A,s,y,w,E,9,-1069501632),w=a(w,A,s,y,b,14,643717713),y=a(y,w,A,s,r,20,-373897302),s=a(s,y,w,A,x,5,-701558691),A=a(A,s,y,w,U,9,38016083),w=a(w,A,s,y,z,14,-660478335),y=a(y,w,A,s,p,20,-405537848),s=a(s,y,w,A,T,5,568446438),A=a(A,s,y,w,q,9,-1019803690),w=a(w,A,s,y,m,14,-187363961),y=a(y,w,A,s,S,20,1163531501),s=a(s,y,w,A,k,5,-1444681467),A=a(A,s,y,w,f,9,-51403784),w=a(w,A,s,y,M,14,1735328473),y=a(y,w,A,s,j,20,-1926607734),s=i(s,y,w,A,x,4,-378558),A=i(A,s,y,w,S,11,-2022574463),w=i(w,A,s,y,b,e,1839030562),y=i(y,w,A,s,q,23,-35309556),s=i(s,y,w,A,t,4,-1530992060),A=i(A,s,y,w,p,11,1272893353),w=i(w,A,s,y,M,e,-155497632),y=i(y,w,A,s,U,23,-1094730640),s=i(s,y,w,A,k,4,681279174),A=i(A,s,y,w,r,11,-358537222),w=i(w,A,s,y,m,e,-722521979),y=i(y,w,A,s,E,23,76029189),s=i(s,y,w,A,T,4,-640364487),A=i(A,s,y,w,j,11,-421815835),w=i(w,A,s,y,z,e,530742520),y=i(y,w,A,s,f,23,-995338651),s=u(s,y,w,A,r,6,-198630844),A=u(A,s,y,w,M,10,1126891415),w=u(w,A,s,y,q,15,-1416354905),y=u(y,w,A,s,x,21,-57434055),s=u(s,y,w,A,j,6,1700485571),A=u(A,s,y,w,m,10,-1894986606),w=u(w,A,s,y,U,15,-1051523),y=u(y,w,A,s,t,21,-2054922799),s=u(s,y,w,A,S,6,1873313359),A=u(A,s,y,w,z,10,-30611744),w=u(w,A,s,y,E,15,-1560198380),y=u(y,w,A,s,k,21,1309151649),s=u(s,y,w,A,p,6,-145523070),A=u(A,s,y,w,b,10,-1120210379),w=u(w,A,s,y,f,15,718787259),y=u(y,w,A,s,T,21,-343485551),s=n(s,c),y=n(y,d),w=n(w,v)}return[s,y,w,A]},d=e=>{for(var n=e[r],o=[];n;)o[t(--n/4)]|=e[n]<<n%4*8;return o},v=r=>"string"==typeof r?(new TextEncoder).encode(r):r;var md5=(t,n,o)=>{var f=d(t=v(t)),l=8*t[r],a=e,i=new Uint8Array(a);if(null!=n){let t=d(n=v(n)),o=e,a=Array(o),i=r=>f.unshift(...a);for(i(t[r]>e&&(t=c(t,8*n[r])));o;a[o]=1549556828^t[o])f[--o]=909522486^t[o];i(f=c(f,512+l)),l=640}for(f=c(f,l);a;)i[--a]=f[a>>2]>>>a%4*8;return o?i:i.reduce((r,t)=>r+(t>>4&&"")+t.toString(e),"")}}
|
package/main.js
CHANGED
|
@@ -5,12 +5,11 @@
|
|
|
5
5
|
/** @type {"length"} */
|
|
6
6
|
let $length = "length"
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/** @type {16} */
|
|
9
|
+
let $16 = 16
|
|
9
10
|
|
|
10
11
|
let floor = Math.floor
|
|
11
12
|
|
|
12
|
-
let textEncoder = new TextEncoder;
|
|
13
|
-
|
|
14
13
|
/**
|
|
15
14
|
* Add integers, wrapping at 2^32.
|
|
16
15
|
*
|
|
@@ -18,7 +17,7 @@ let textEncoder = new TextEncoder;
|
|
|
18
17
|
* @param {number} y Second integer
|
|
19
18
|
* @returns {number} Sum
|
|
20
19
|
*/
|
|
21
|
-
let safeAdd = (x, y) => (
|
|
20
|
+
let safeAdd = (x, y) => 0 | (0 | x) + (0 | y)
|
|
22
21
|
|
|
23
22
|
/**
|
|
24
23
|
* Bitwise rotate a 32-bit number to the left.
|
|
@@ -115,91 +114,95 @@ let binlMD5 = (x, bitLen) => {
|
|
|
115
114
|
, b = -271733879
|
|
116
115
|
, c = -1732584194
|
|
117
116
|
, d = 271733878
|
|
118
|
-
, padLenIndex = floor((bitLen + 64) / 512) * 16 + 14
|
|
117
|
+
, padLenIndex = floor((bitLen + 64) / 512) * $16 + 14
|
|
119
118
|
|
|
120
119
|
// append padding
|
|
121
|
-
x[floor(bitLen / 32)] |=
|
|
122
|
-
x[padLenIndex] =
|
|
123
|
-
x[padLenIndex + 1] =
|
|
120
|
+
x[floor(bitLen / 32)] |= 0x80 << bitLen % 32;
|
|
121
|
+
x[padLenIndex] = 0 | bitLen;
|
|
122
|
+
x[padLenIndex + 1] = 0 | bitLen / 0x100000000;
|
|
123
|
+
|
|
124
|
+
for (; i < x[$length]; d = safeAdd(d, oldd)) {
|
|
125
|
+
let [
|
|
126
|
+
x0, x1, x2, x3, x4, x5, x6, x7,
|
|
127
|
+
x8, x9, x10, x11, x12, x13, x14, x15
|
|
128
|
+
] = x.slice(i, i += $16)
|
|
124
129
|
|
|
125
|
-
for (; i < x[$length]; i += 16) {
|
|
126
130
|
olda = a
|
|
127
131
|
oldb = b
|
|
128
132
|
oldc = c
|
|
129
133
|
oldd = d
|
|
130
134
|
|
|
131
|
-
a = md5ff(a, b, c, d,
|
|
132
|
-
d = md5ff(d, a, b, c,
|
|
133
|
-
c = md5ff(c, d, a, b,
|
|
134
|
-
b = md5ff(b, c, d, a,
|
|
135
|
-
a = md5ff(a, b, c, d,
|
|
136
|
-
d = md5ff(d, a, b, c,
|
|
137
|
-
c = md5ff(c, d, a, b,
|
|
138
|
-
b = md5ff(b, c, d, a,
|
|
139
|
-
a = md5ff(a, b, c, d,
|
|
140
|
-
d = md5ff(d, a, b, c,
|
|
141
|
-
c = md5ff(c, d, a, b,
|
|
142
|
-
b = md5ff(b, c, d, a,
|
|
143
|
-
a = md5ff(a, b, c, d,
|
|
144
|
-
d = md5ff(d, a, b, c,
|
|
145
|
-
c = md5ff(c, d, a, b,
|
|
146
|
-
b = md5ff(b, c, d, a,
|
|
135
|
+
a = md5ff(a, b, c, d, x0, 7, -680876936)
|
|
136
|
+
d = md5ff(d, a, b, c, x1, 12, -389564586)
|
|
137
|
+
c = md5ff(c, d, a, b, x2, 17, 606105819)
|
|
138
|
+
b = md5ff(b, c, d, a, x3, 22, -1044525330)
|
|
139
|
+
a = md5ff(a, b, c, d, x4, 7, -176418897)
|
|
140
|
+
d = md5ff(d, a, b, c, x5, 12, 1200080426)
|
|
141
|
+
c = md5ff(c, d, a, b, x6, 17, -1473231341)
|
|
142
|
+
b = md5ff(b, c, d, a, x7, 22, -45705983)
|
|
143
|
+
a = md5ff(a, b, c, d, x8, 7, 1770035416)
|
|
144
|
+
d = md5ff(d, a, b, c, x9, 12, -1958414417)
|
|
145
|
+
c = md5ff(c, d, a, b, x10, 17, -42063)
|
|
146
|
+
b = md5ff(b, c, d, a, x11, 22, -1990404162)
|
|
147
|
+
a = md5ff(a, b, c, d, x12, 7, 1804603682)
|
|
148
|
+
d = md5ff(d, a, b, c, x13, 12, -40341101)
|
|
149
|
+
c = md5ff(c, d, a, b, x14, 17, -1502002290)
|
|
150
|
+
b = md5ff(b, c, d, a, x15, 22, 1236535329)
|
|
147
151
|
|
|
148
|
-
a = md5gg(a, b, c, d,
|
|
149
|
-
d = md5gg(d, a, b, c,
|
|
150
|
-
c = md5gg(c, d, a, b,
|
|
151
|
-
b = md5gg(b, c, d, a,
|
|
152
|
-
a = md5gg(a, b, c, d,
|
|
153
|
-
d = md5gg(d, a, b, c,
|
|
154
|
-
c = md5gg(c, d, a, b,
|
|
155
|
-
b = md5gg(b, c, d, a,
|
|
156
|
-
a = md5gg(a, b, c, d,
|
|
157
|
-
d = md5gg(d, a, b, c,
|
|
158
|
-
c = md5gg(c, d, a, b,
|
|
159
|
-
b = md5gg(b, c, d, a,
|
|
160
|
-
a = md5gg(a, b, c, d,
|
|
161
|
-
d = md5gg(d, a, b, c,
|
|
162
|
-
c = md5gg(c, d, a, b,
|
|
163
|
-
b = md5gg(b, c, d, a,
|
|
152
|
+
a = md5gg(a, b, c, d, x1, 5, -165796510)
|
|
153
|
+
d = md5gg(d, a, b, c, x6, 9, -1069501632)
|
|
154
|
+
c = md5gg(c, d, a, b, x11, 14, 643717713)
|
|
155
|
+
b = md5gg(b, c, d, a, x0, 20, -373897302)
|
|
156
|
+
a = md5gg(a, b, c, d, x5, 5, -701558691)
|
|
157
|
+
d = md5gg(d, a, b, c, x10, 9, 38016083)
|
|
158
|
+
c = md5gg(c, d, a, b, x15, 14, -660478335)
|
|
159
|
+
b = md5gg(b, c, d, a, x4, 20, -405537848)
|
|
160
|
+
a = md5gg(a, b, c, d, x9, 5, 568446438)
|
|
161
|
+
d = md5gg(d, a, b, c, x14, 9, -1019803690)
|
|
162
|
+
c = md5gg(c, d, a, b, x3, 14, -187363961)
|
|
163
|
+
b = md5gg(b, c, d, a, x8, 20, 1163531501)
|
|
164
|
+
a = md5gg(a, b, c, d, x13, 5, -1444681467)
|
|
165
|
+
d = md5gg(d, a, b, c, x2, 9, -51403784)
|
|
166
|
+
c = md5gg(c, d, a, b, x7, 14, 1735328473)
|
|
167
|
+
b = md5gg(b, c, d, a, x12, 20, -1926607734)
|
|
164
168
|
|
|
165
|
-
a = md5hh(a, b, c, d,
|
|
166
|
-
d = md5hh(d, a, b, c,
|
|
167
|
-
c = md5hh(c, d, a, b,
|
|
168
|
-
b = md5hh(b, c, d, a,
|
|
169
|
-
a = md5hh(a, b, c, d,
|
|
170
|
-
d = md5hh(d, a, b, c,
|
|
171
|
-
c = md5hh(c, d, a, b,
|
|
172
|
-
b = md5hh(b, c, d, a,
|
|
173
|
-
a = md5hh(a, b, c, d,
|
|
174
|
-
d = md5hh(d, a, b, c,
|
|
175
|
-
c = md5hh(c, d, a, b,
|
|
176
|
-
b = md5hh(b, c, d, a,
|
|
177
|
-
a = md5hh(a, b, c, d,
|
|
178
|
-
d = md5hh(d, a, b, c,
|
|
179
|
-
c = md5hh(c, d, a, b,
|
|
180
|
-
b = md5hh(b, c, d, a,
|
|
169
|
+
a = md5hh(a, b, c, d, x5, 4, -378558)
|
|
170
|
+
d = md5hh(d, a, b, c, x8, 11, -2022574463)
|
|
171
|
+
c = md5hh(c, d, a, b, x11, $16, 1839030562)
|
|
172
|
+
b = md5hh(b, c, d, a, x14, 23, -35309556)
|
|
173
|
+
a = md5hh(a, b, c, d, x1, 4, -1530992060)
|
|
174
|
+
d = md5hh(d, a, b, c, x4, 11, 1272893353)
|
|
175
|
+
c = md5hh(c, d, a, b, x7, $16, -155497632)
|
|
176
|
+
b = md5hh(b, c, d, a, x10, 23, -1094730640)
|
|
177
|
+
a = md5hh(a, b, c, d, x13, 4, 681279174)
|
|
178
|
+
d = md5hh(d, a, b, c, x0, 11, -358537222)
|
|
179
|
+
c = md5hh(c, d, a, b, x3, $16, -722521979)
|
|
180
|
+
b = md5hh(b, c, d, a, x6, 23, 76029189)
|
|
181
|
+
a = md5hh(a, b, c, d, x9, 4, -640364487)
|
|
182
|
+
d = md5hh(d, a, b, c, x12, 11, -421815835)
|
|
183
|
+
c = md5hh(c, d, a, b, x15, $16, 530742520)
|
|
184
|
+
b = md5hh(b, c, d, a, x2, 23, -995338651)
|
|
181
185
|
|
|
182
|
-
a = md5ii(a, b, c, d,
|
|
183
|
-
d = md5ii(d, a, b, c,
|
|
184
|
-
c = md5ii(c, d, a, b,
|
|
185
|
-
b = md5ii(b, c, d, a,
|
|
186
|
-
a = md5ii(a, b, c, d,
|
|
187
|
-
d = md5ii(d, a, b, c,
|
|
188
|
-
c = md5ii(c, d, a, b,
|
|
189
|
-
b = md5ii(b, c, d, a,
|
|
190
|
-
a = md5ii(a, b, c, d,
|
|
191
|
-
d = md5ii(d, a, b, c,
|
|
192
|
-
c = md5ii(c, d, a, b,
|
|
193
|
-
b = md5ii(b, c, d, a,
|
|
194
|
-
a = md5ii(a, b, c, d,
|
|
195
|
-
d = md5ii(d, a, b, c,
|
|
196
|
-
c = md5ii(c, d, a, b,
|
|
197
|
-
b = md5ii(b, c, d, a,
|
|
186
|
+
a = md5ii(a, b, c, d, x0, 6, -198630844)
|
|
187
|
+
d = md5ii(d, a, b, c, x7, 10, 1126891415)
|
|
188
|
+
c = md5ii(c, d, a, b, x14, 15, -1416354905)
|
|
189
|
+
b = md5ii(b, c, d, a, x5, 21, -57434055)
|
|
190
|
+
a = md5ii(a, b, c, d, x12, 6, 1700485571)
|
|
191
|
+
d = md5ii(d, a, b, c, x3, 10, -1894986606)
|
|
192
|
+
c = md5ii(c, d, a, b, x10, 15, -1051523)
|
|
193
|
+
b = md5ii(b, c, d, a, x1, 21, -2054922799)
|
|
194
|
+
a = md5ii(a, b, c, d, x8, 6, 1873313359)
|
|
195
|
+
d = md5ii(d, a, b, c, x15, 10, -30611744)
|
|
196
|
+
c = md5ii(c, d, a, b, x6, 15, -1560198380)
|
|
197
|
+
b = md5ii(b, c, d, a, x13, 21, 1309151649)
|
|
198
|
+
a = md5ii(a, b, c, d, x4, 6, -145523070)
|
|
199
|
+
d = md5ii(d, a, b, c, x11, 10, -1120210379)
|
|
200
|
+
c = md5ii(c, d, a, b, x2, 15, 718787259)
|
|
201
|
+
b = md5ii(b, c, d, a, x9, 21, -343485551)
|
|
198
202
|
|
|
199
203
|
a = safeAdd(a, olda)
|
|
200
204
|
b = safeAdd(b, oldb)
|
|
201
205
|
c = safeAdd(c, oldc)
|
|
202
|
-
d = safeAdd(d, oldd)
|
|
203
206
|
}
|
|
204
207
|
return [a, b, c, d]
|
|
205
208
|
}
|
|
@@ -212,13 +215,24 @@ let binlMD5 = (x, bitLen) => {
|
|
|
212
215
|
*/
|
|
213
216
|
let bytesToBinl = (input) => {
|
|
214
217
|
var i = input[$length]
|
|
215
|
-
|
|
218
|
+
/** @type {number[]} */
|
|
219
|
+
var output = []
|
|
216
220
|
for (; i;) {
|
|
217
|
-
output[floor(--i / 4)] |= input[i] <<
|
|
221
|
+
output[floor(--i / 4)] |= input[i] << i % 4 * 8
|
|
218
222
|
}
|
|
219
223
|
return output
|
|
220
224
|
}
|
|
221
225
|
|
|
226
|
+
/**
|
|
227
|
+
* @param {string | Uint8Array} input
|
|
228
|
+
* @returns {Uint8Array}
|
|
229
|
+
*/
|
|
230
|
+
let inputToBytes = (input) => (
|
|
231
|
+
typeof input == 'string'
|
|
232
|
+
? new TextEncoder().encode(input)
|
|
233
|
+
: input
|
|
234
|
+
)
|
|
235
|
+
|
|
222
236
|
/**
|
|
223
237
|
* @overload
|
|
224
238
|
* @param {string | Uint8Array} data
|
|
@@ -245,29 +259,22 @@ let bytesToBinl = (input) => {
|
|
|
245
259
|
* @returns {string | Uint8Array<ArrayBuffer>} The MD5 (or HMAC‑MD5) digest, either as a hex string or a Uint8Array.
|
|
246
260
|
*/
|
|
247
261
|
var md5 = (data, key, raw) => {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
,
|
|
252
|
-
, i = 16
|
|
253
|
-
, out = new Uint8Array(16)
|
|
262
|
+
var bdata = bytesToBinl(data = inputToBytes(data))
|
|
263
|
+
, bitLen = data[$length] * 8
|
|
264
|
+
, i = $16
|
|
265
|
+
, out = new Uint8Array(i)
|
|
254
266
|
|
|
255
267
|
if (key != null) {
|
|
256
268
|
// HMAC
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
,
|
|
260
|
-
,
|
|
261
|
-
|
|
262
|
-
if (bkey[$length] > 16) {
|
|
263
|
-
bkey = binlMD5(bkey, key[$length] * 8)
|
|
264
|
-
}
|
|
269
|
+
let bkey = bytesToBinl(key = inputToBytes(key))
|
|
270
|
+
, i = $16
|
|
271
|
+
, opad = Array(i)
|
|
272
|
+
, unshift = (/**@type{*}*/ _) => bdata.unshift(...opad)
|
|
273
|
+
unshift(bkey[$length] > $16 && (bkey = binlMD5(bkey, key[$length] * 8)))
|
|
265
274
|
for (; i; opad[i] = bkey[i] ^ 0x5c5c5c5c) {
|
|
266
|
-
|
|
275
|
+
bdata[--i] = bkey[i] ^ 0x36363636
|
|
267
276
|
}
|
|
268
|
-
|
|
269
|
-
bdata = binlMD5(bdata, 512 + bitLen)
|
|
270
|
-
bdata.unshift(...opad)
|
|
277
|
+
unshift(bdata = binlMD5(bdata, 512 + bitLen))
|
|
271
278
|
bitLen = 512 + 128
|
|
272
279
|
}
|
|
273
280
|
|
|
@@ -275,14 +282,12 @@ var md5 = (data, key, raw) => {
|
|
|
275
282
|
|
|
276
283
|
// binl to bytes
|
|
277
284
|
for (; i;) {
|
|
278
|
-
out[--i] = bdata[
|
|
285
|
+
out[--i] = bdata[i >> 2] >>> i % 4 * 8
|
|
279
286
|
}
|
|
280
287
|
|
|
281
288
|
return raw
|
|
282
289
|
? out
|
|
283
|
-
: out.
|
|
284
|
-
? out.toHex()
|
|
285
|
-
: _Array.from(out, byte => (0 + byte.toString(16)).slice(-2)).join('')
|
|
290
|
+
: out.reduce((p, v) => p + (v >> 4 && '') + v.toString($16), '')
|
|
286
291
|
}
|
|
287
292
|
|
|
288
293
|
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.2",
|
|
4
|
+
"description": "A tiny JavaScript HMAC-MD5 implementation. The minified browser bundle is only 2911 bytes.",
|
|
5
5
|
"author": "bddjr",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|