@stryke/crypto 0.5.44 → 0.6.0
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/CHANGELOG.md +7 -0
- package/dist/cloudflare.cjs +960 -0
- package/dist/cloudflare.d.cts +251 -0
- package/dist/cloudflare.d.cts.map +1 -0
- package/dist/cloudflare.d.mts +251 -0
- package/dist/cloudflare.d.mts.map +1 -0
- package/dist/cloudflare.mjs +942 -0
- package/dist/cloudflare.mjs.map +1 -0
- package/dist/index.cjs +16 -12
- package/dist/index.d.cts +2 -4
- package/dist/index.d.mts +2 -4
- package/dist/index.mjs +2 -4
- package/dist/neutral-BISXAuIW.cjs +868 -0
- package/dist/{encryption.d.mts → neutral-BJmyEdJq.d.mts} +77 -2
- package/dist/neutral-BJmyEdJq.d.mts.map +1 -0
- package/dist/neutral-BtZoJSl-.mjs +779 -0
- package/dist/neutral-BtZoJSl-.mjs.map +1 -0
- package/dist/{encryption.d.cts → neutral-CW6EXiYo.d.cts} +77 -2
- package/dist/neutral-CW6EXiYo.d.cts.map +1 -0
- package/dist/neutral.cjs +16 -16
- package/dist/neutral.d.cts +2 -4
- package/dist/neutral.d.mts +2 -4
- package/dist/neutral.mjs +2 -4
- package/dist/node.cjs +663 -0
- package/dist/node.d.cts +117 -0
- package/dist/node.d.cts.map +1 -0
- package/dist/node.d.mts +117 -0
- package/dist/node.d.mts.map +1 -0
- package/dist/node.mjs +654 -0
- package/dist/node.mjs.map +1 -0
- package/package.json +55 -20
- package/dist/base-64.cjs +0 -440
- package/dist/base-64.d.cts +0 -40
- package/dist/base-64.d.cts.map +0 -1
- package/dist/base-64.d.mts +0 -40
- package/dist/base-64.d.mts.map +0 -1
- package/dist/base-64.mjs +0 -438
- package/dist/base-64.mjs.map +0 -1
- package/dist/convert/src/array-buffer-to-string.cjs +0 -19
- package/dist/convert/src/array-buffer-to-string.mjs +0 -19
- package/dist/convert/src/array-buffer-to-string.mjs.map +0 -1
- package/dist/convert/src/neutral.cjs +0 -7
- package/dist/convert/src/neutral.mjs +0 -9
- package/dist/convert/src/parse-type-definition.cjs +0 -1
- package/dist/convert/src/parse-type-definition.mjs +0 -3
- package/dist/convert/src/string-to-uint8-array.cjs +0 -24
- package/dist/convert/src/string-to-uint8-array.mjs +0 -23
- package/dist/convert/src/string-to-uint8-array.mjs.map +0 -1
- package/dist/convert/src/string-to-utf8-array.cjs +0 -15
- package/dist/convert/src/string-to-utf8-array.mjs +0 -15
- package/dist/convert/src/string-to-utf8-array.mjs.map +0 -1
- package/dist/convert/src/uint8-array-to-stream.cjs +0 -22
- package/dist/convert/src/uint8-array-to-stream.mjs +0 -22
- package/dist/convert/src/uint8-array-to-stream.mjs.map +0 -1
- package/dist/convert/src/uint8-array-to-string.cjs +0 -15
- package/dist/convert/src/uint8-array-to-string.mjs +0 -16
- package/dist/convert/src/uint8-array-to-string.mjs.map +0 -1
- package/dist/convert/src/utf8-array-to-string.cjs +0 -15
- package/dist/convert/src/utf8-array-to-string.mjs +0 -15
- package/dist/convert/src/utf8-array-to-string.mjs.map +0 -1
- package/dist/encryption.cjs +0 -134
- package/dist/encryption.d.cts.map +0 -1
- package/dist/encryption.d.mts.map +0 -1
- package/dist/encryption.mjs +0 -129
- package/dist/encryption.mjs.map +0 -1
- package/dist/encryption.node.cjs +0 -63
- package/dist/encryption.node.d.cts +0 -42
- package/dist/encryption.node.d.cts.map +0 -1
- package/dist/encryption.node.d.mts +0 -42
- package/dist/encryption.node.d.mts.map +0 -1
- package/dist/encryption.node.mjs +0 -62
- package/dist/encryption.node.mjs.map +0 -1
- package/dist/hex.cjs +0 -62
- package/dist/hex.d.cts +0 -18
- package/dist/hex.d.cts.map +0 -1
- package/dist/hex.d.mts +0 -18
- package/dist/hex.d.mts.map +0 -1
- package/dist/hex.mjs +0 -61
- package/dist/hex.mjs.map +0 -1
- package/dist/type-checks/src/index.cjs +0 -5
- package/dist/type-checks/src/index.mjs +0 -7
- package/dist/type-checks/src/is-buffer.cjs +0 -12
- package/dist/type-checks/src/is-buffer.mjs +0 -12
- package/dist/type-checks/src/is-buffer.mjs.map +0 -1
- package/dist/type-checks/src/is-collection.cjs +0 -1
- package/dist/type-checks/src/is-collection.mjs +0 -3
- package/dist/type-checks/src/is-string.cjs +0 -12
- package/dist/type-checks/src/is-string.mjs +0 -12
- package/dist/type-checks/src/is-string.mjs.map +0 -1
- package/dist/type-checks/src/is-undefined.cjs +0 -8
- package/dist/type-checks/src/is-undefined.mjs +0 -8
- package/dist/type-checks/src/is-undefined.mjs.map +0 -1
- package/dist/type-checks/src/type-detect.cjs +0 -15
- package/dist/type-checks/src/type-detect.mjs +0 -17
- package/dist/type-checks/src/type-detect.mjs.map +0 -1
|
@@ -0,0 +1,868 @@
|
|
|
1
|
+
|
|
2
|
+
//#region ../convert/src/array-buffer-to-string.ts
|
|
3
|
+
/**
|
|
4
|
+
* Convert an ArrayBuffer or Uint8Array to a string
|
|
5
|
+
*
|
|
6
|
+
* @param buffer - The ArrayBuffer or Uint8Array to convert
|
|
7
|
+
* @returns The converted string
|
|
8
|
+
*/
|
|
9
|
+
function arrayBufferToString(buffer) {
|
|
10
|
+
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
11
|
+
const len = bytes.byteLength;
|
|
12
|
+
if (len < 65535) return String.fromCharCode.apply(null, bytes);
|
|
13
|
+
let binary = "";
|
|
14
|
+
for (let i = 0; i < len; i++) binary += String.fromCharCode(bytes[i]);
|
|
15
|
+
return binary;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region ../type-checks/src/is-undefined.ts
|
|
20
|
+
const isUndefined = (value) => {
|
|
21
|
+
return value === void 0;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region ../type-checks/src/is-buffer.ts
|
|
26
|
+
const isBufferExists = typeof Buffer !== "undefined";
|
|
27
|
+
/**
|
|
28
|
+
* Check if the provided value's type is `Buffer`
|
|
29
|
+
*/
|
|
30
|
+
const isBuffer = isBufferExists ? Buffer.isBuffer.bind(Buffer) : function isBuffer$1(value) {
|
|
31
|
+
return false;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region ../type-checks/src/type-detect.ts
|
|
36
|
+
const globalObject = ((Obj) => {
|
|
37
|
+
if (typeof globalThis === "object") return globalThis;
|
|
38
|
+
Object.defineProperty(Obj, "typeDetectGlobalObject", {
|
|
39
|
+
get() {
|
|
40
|
+
return this;
|
|
41
|
+
},
|
|
42
|
+
configurable: true
|
|
43
|
+
});
|
|
44
|
+
return globalThis;
|
|
45
|
+
})(Object.prototype);
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region ../type-checks/src/is-string.ts
|
|
49
|
+
const isString = (value) => {
|
|
50
|
+
try {
|
|
51
|
+
return typeof value === "string";
|
|
52
|
+
} catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region ../convert/src/string-to-uint8-array.ts
|
|
59
|
+
/**
|
|
60
|
+
* Convert a string to Uint8Array
|
|
61
|
+
*
|
|
62
|
+
* @param text - The text to convert
|
|
63
|
+
* @returns The converted Uint8Array
|
|
64
|
+
*/
|
|
65
|
+
function stringToUint8Array(text) {
|
|
66
|
+
return Uint8Array.from([...encodeURIComponent(text)].map((letter) => letter.codePointAt(0)));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Convert a base64 string to a Uint8Array
|
|
70
|
+
*
|
|
71
|
+
* @param data - The base64 string to convert
|
|
72
|
+
* @returns The converted Uint8Array
|
|
73
|
+
*/
|
|
74
|
+
function base64StringToUint8Array(data) {
|
|
75
|
+
return stringToUint8Array(atob(data));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region ../convert/src/string-to-utf8-array.ts
|
|
80
|
+
const encoder = new TextEncoder();
|
|
81
|
+
/**
|
|
82
|
+
* Convert a string to a utf-8 array
|
|
83
|
+
*
|
|
84
|
+
* @param input - The string to convert
|
|
85
|
+
* @returns The converted utf-8 array
|
|
86
|
+
*/
|
|
87
|
+
function stringToUtf8Array(input) {
|
|
88
|
+
return encoder.encode(input);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region ../convert/src/uint8-array-to-stream.ts
|
|
93
|
+
/**
|
|
94
|
+
* Concatenate an array of Uint8Array chunks into a single Uint8Array
|
|
95
|
+
*
|
|
96
|
+
* @param chunks - Array of Uint8Array chunks to concatenate
|
|
97
|
+
* @returns The concatenated Uint8Array
|
|
98
|
+
*/
|
|
99
|
+
function concatUint8Array(chunks) {
|
|
100
|
+
let total = 0;
|
|
101
|
+
for (const chunk of chunks) total += chunk.length;
|
|
102
|
+
const result = new Uint8Array(total);
|
|
103
|
+
let offset = 0;
|
|
104
|
+
for (const chunk of chunks) {
|
|
105
|
+
result.set(chunk, offset);
|
|
106
|
+
offset += chunk.length;
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region ../convert/src/uint8-array-to-string.ts
|
|
113
|
+
/**
|
|
114
|
+
* Convert a Uint8Array to a base64 string
|
|
115
|
+
*
|
|
116
|
+
* @param buffer - The Uint8Array to convert
|
|
117
|
+
* @returns The converted base64 string
|
|
118
|
+
*/
|
|
119
|
+
function uint8ArrayToString(buffer) {
|
|
120
|
+
return btoa(arrayBufferToString(buffer));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region ../convert/src/utf8-array-to-string.ts
|
|
125
|
+
const decoder = new TextDecoder();
|
|
126
|
+
/**
|
|
127
|
+
* Convert a utf-8 array to string
|
|
128
|
+
*
|
|
129
|
+
* @param input - Utf-8 Array
|
|
130
|
+
* @returns The converted string
|
|
131
|
+
*/
|
|
132
|
+
function utf8ArrayToString(input) {
|
|
133
|
+
return decoder.decode(input);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/base-64.ts
|
|
138
|
+
const ENCODE_MAP = new Uint8Array([
|
|
139
|
+
65,
|
|
140
|
+
66,
|
|
141
|
+
67,
|
|
142
|
+
68,
|
|
143
|
+
69,
|
|
144
|
+
70,
|
|
145
|
+
71,
|
|
146
|
+
72,
|
|
147
|
+
73,
|
|
148
|
+
74,
|
|
149
|
+
75,
|
|
150
|
+
76,
|
|
151
|
+
77,
|
|
152
|
+
78,
|
|
153
|
+
79,
|
|
154
|
+
80,
|
|
155
|
+
81,
|
|
156
|
+
82,
|
|
157
|
+
83,
|
|
158
|
+
84,
|
|
159
|
+
85,
|
|
160
|
+
86,
|
|
161
|
+
87,
|
|
162
|
+
88,
|
|
163
|
+
89,
|
|
164
|
+
90,
|
|
165
|
+
97,
|
|
166
|
+
98,
|
|
167
|
+
99,
|
|
168
|
+
100,
|
|
169
|
+
101,
|
|
170
|
+
102,
|
|
171
|
+
103,
|
|
172
|
+
104,
|
|
173
|
+
105,
|
|
174
|
+
106,
|
|
175
|
+
107,
|
|
176
|
+
108,
|
|
177
|
+
109,
|
|
178
|
+
110,
|
|
179
|
+
111,
|
|
180
|
+
112,
|
|
181
|
+
113,
|
|
182
|
+
114,
|
|
183
|
+
115,
|
|
184
|
+
116,
|
|
185
|
+
117,
|
|
186
|
+
118,
|
|
187
|
+
119,
|
|
188
|
+
120,
|
|
189
|
+
121,
|
|
190
|
+
122,
|
|
191
|
+
48,
|
|
192
|
+
49,
|
|
193
|
+
50,
|
|
194
|
+
51,
|
|
195
|
+
52,
|
|
196
|
+
53,
|
|
197
|
+
54,
|
|
198
|
+
55,
|
|
199
|
+
56,
|
|
200
|
+
57,
|
|
201
|
+
43,
|
|
202
|
+
47
|
|
203
|
+
]);
|
|
204
|
+
const ENCODE_PAD = 61;
|
|
205
|
+
const DECODE_MAP$1 = new Uint8Array([
|
|
206
|
+
100,
|
|
207
|
+
100,
|
|
208
|
+
100,
|
|
209
|
+
100,
|
|
210
|
+
100,
|
|
211
|
+
100,
|
|
212
|
+
100,
|
|
213
|
+
100,
|
|
214
|
+
100,
|
|
215
|
+
100,
|
|
216
|
+
100,
|
|
217
|
+
100,
|
|
218
|
+
100,
|
|
219
|
+
100,
|
|
220
|
+
100,
|
|
221
|
+
100,
|
|
222
|
+
100,
|
|
223
|
+
100,
|
|
224
|
+
100,
|
|
225
|
+
100,
|
|
226
|
+
100,
|
|
227
|
+
100,
|
|
228
|
+
100,
|
|
229
|
+
100,
|
|
230
|
+
100,
|
|
231
|
+
100,
|
|
232
|
+
100,
|
|
233
|
+
100,
|
|
234
|
+
100,
|
|
235
|
+
100,
|
|
236
|
+
100,
|
|
237
|
+
100,
|
|
238
|
+
100,
|
|
239
|
+
100,
|
|
240
|
+
100,
|
|
241
|
+
100,
|
|
242
|
+
100,
|
|
243
|
+
100,
|
|
244
|
+
100,
|
|
245
|
+
100,
|
|
246
|
+
100,
|
|
247
|
+
100,
|
|
248
|
+
100,
|
|
249
|
+
62,
|
|
250
|
+
100,
|
|
251
|
+
100,
|
|
252
|
+
100,
|
|
253
|
+
63,
|
|
254
|
+
52,
|
|
255
|
+
53,
|
|
256
|
+
54,
|
|
257
|
+
55,
|
|
258
|
+
56,
|
|
259
|
+
57,
|
|
260
|
+
58,
|
|
261
|
+
59,
|
|
262
|
+
60,
|
|
263
|
+
61,
|
|
264
|
+
100,
|
|
265
|
+
100,
|
|
266
|
+
100,
|
|
267
|
+
100,
|
|
268
|
+
100,
|
|
269
|
+
100,
|
|
270
|
+
100,
|
|
271
|
+
0,
|
|
272
|
+
1,
|
|
273
|
+
2,
|
|
274
|
+
3,
|
|
275
|
+
4,
|
|
276
|
+
5,
|
|
277
|
+
6,
|
|
278
|
+
7,
|
|
279
|
+
8,
|
|
280
|
+
9,
|
|
281
|
+
10,
|
|
282
|
+
11,
|
|
283
|
+
12,
|
|
284
|
+
13,
|
|
285
|
+
14,
|
|
286
|
+
15,
|
|
287
|
+
16,
|
|
288
|
+
17,
|
|
289
|
+
18,
|
|
290
|
+
19,
|
|
291
|
+
20,
|
|
292
|
+
21,
|
|
293
|
+
22,
|
|
294
|
+
23,
|
|
295
|
+
24,
|
|
296
|
+
25,
|
|
297
|
+
100,
|
|
298
|
+
100,
|
|
299
|
+
100,
|
|
300
|
+
100,
|
|
301
|
+
100,
|
|
302
|
+
100,
|
|
303
|
+
26,
|
|
304
|
+
27,
|
|
305
|
+
28,
|
|
306
|
+
29,
|
|
307
|
+
30,
|
|
308
|
+
31,
|
|
309
|
+
32,
|
|
310
|
+
33,
|
|
311
|
+
34,
|
|
312
|
+
35,
|
|
313
|
+
36,
|
|
314
|
+
37,
|
|
315
|
+
38,
|
|
316
|
+
39,
|
|
317
|
+
40,
|
|
318
|
+
41,
|
|
319
|
+
42,
|
|
320
|
+
43,
|
|
321
|
+
44,
|
|
322
|
+
45,
|
|
323
|
+
46,
|
|
324
|
+
47,
|
|
325
|
+
48,
|
|
326
|
+
49,
|
|
327
|
+
50,
|
|
328
|
+
51,
|
|
329
|
+
100,
|
|
330
|
+
100,
|
|
331
|
+
100,
|
|
332
|
+
100,
|
|
333
|
+
100,
|
|
334
|
+
100,
|
|
335
|
+
100,
|
|
336
|
+
100,
|
|
337
|
+
100,
|
|
338
|
+
100,
|
|
339
|
+
100,
|
|
340
|
+
100,
|
|
341
|
+
100,
|
|
342
|
+
100,
|
|
343
|
+
100,
|
|
344
|
+
100,
|
|
345
|
+
100,
|
|
346
|
+
100,
|
|
347
|
+
100,
|
|
348
|
+
100,
|
|
349
|
+
100,
|
|
350
|
+
100,
|
|
351
|
+
100,
|
|
352
|
+
100,
|
|
353
|
+
100,
|
|
354
|
+
100,
|
|
355
|
+
100,
|
|
356
|
+
100,
|
|
357
|
+
100,
|
|
358
|
+
100,
|
|
359
|
+
100,
|
|
360
|
+
100,
|
|
361
|
+
100,
|
|
362
|
+
100,
|
|
363
|
+
100,
|
|
364
|
+
100,
|
|
365
|
+
100,
|
|
366
|
+
100,
|
|
367
|
+
100,
|
|
368
|
+
100,
|
|
369
|
+
100,
|
|
370
|
+
100,
|
|
371
|
+
100,
|
|
372
|
+
100,
|
|
373
|
+
100,
|
|
374
|
+
100,
|
|
375
|
+
100,
|
|
376
|
+
100,
|
|
377
|
+
100,
|
|
378
|
+
100,
|
|
379
|
+
100,
|
|
380
|
+
100,
|
|
381
|
+
100,
|
|
382
|
+
100,
|
|
383
|
+
100,
|
|
384
|
+
100,
|
|
385
|
+
100,
|
|
386
|
+
100,
|
|
387
|
+
100,
|
|
388
|
+
100,
|
|
389
|
+
100,
|
|
390
|
+
100,
|
|
391
|
+
100,
|
|
392
|
+
100,
|
|
393
|
+
100,
|
|
394
|
+
100,
|
|
395
|
+
100,
|
|
396
|
+
100,
|
|
397
|
+
100,
|
|
398
|
+
100,
|
|
399
|
+
100,
|
|
400
|
+
100,
|
|
401
|
+
100,
|
|
402
|
+
100,
|
|
403
|
+
100,
|
|
404
|
+
100,
|
|
405
|
+
100,
|
|
406
|
+
100,
|
|
407
|
+
100,
|
|
408
|
+
100,
|
|
409
|
+
100,
|
|
410
|
+
100,
|
|
411
|
+
100,
|
|
412
|
+
100,
|
|
413
|
+
100,
|
|
414
|
+
100,
|
|
415
|
+
100,
|
|
416
|
+
100,
|
|
417
|
+
100,
|
|
418
|
+
100,
|
|
419
|
+
100,
|
|
420
|
+
100,
|
|
421
|
+
100,
|
|
422
|
+
100,
|
|
423
|
+
100,
|
|
424
|
+
100,
|
|
425
|
+
100,
|
|
426
|
+
100,
|
|
427
|
+
100,
|
|
428
|
+
100,
|
|
429
|
+
100,
|
|
430
|
+
100,
|
|
431
|
+
100,
|
|
432
|
+
100,
|
|
433
|
+
100,
|
|
434
|
+
100,
|
|
435
|
+
100,
|
|
436
|
+
100,
|
|
437
|
+
100,
|
|
438
|
+
100,
|
|
439
|
+
100,
|
|
440
|
+
100,
|
|
441
|
+
100,
|
|
442
|
+
100,
|
|
443
|
+
100,
|
|
444
|
+
100,
|
|
445
|
+
100,
|
|
446
|
+
100,
|
|
447
|
+
100,
|
|
448
|
+
100,
|
|
449
|
+
100,
|
|
450
|
+
100,
|
|
451
|
+
100,
|
|
452
|
+
100,
|
|
453
|
+
100,
|
|
454
|
+
100,
|
|
455
|
+
100,
|
|
456
|
+
100,
|
|
457
|
+
100,
|
|
458
|
+
100,
|
|
459
|
+
100,
|
|
460
|
+
100
|
|
461
|
+
]);
|
|
462
|
+
/**
|
|
463
|
+
* Encodes a Uint8Array into a Base64 encoded Uint8Array.
|
|
464
|
+
*
|
|
465
|
+
* @credit https://github.com/hi-ogawa/js-utils
|
|
466
|
+
*
|
|
467
|
+
* @param input - The input Uint8Array or string to encode.
|
|
468
|
+
* @returns The Base64 encoded Uint8Array.
|
|
469
|
+
*/
|
|
470
|
+
function encodeBase64(input) {
|
|
471
|
+
if (isString(input)) input = stringToUint8Array(input);
|
|
472
|
+
const xLen = input.length;
|
|
473
|
+
const result = new Uint8Array(Math.ceil(xLen / 3) * 4);
|
|
474
|
+
const chunkLen = Math.floor(xLen / 3);
|
|
475
|
+
for (let i$1 = 0; i$1 < chunkLen; i$1++) {
|
|
476
|
+
const chunk = input[3 * i$1 + 0] << 16 | input[3 * i$1 + 1] << 8 | input[3 * i$1 + 2];
|
|
477
|
+
result[4 * i$1 + 0] = ENCODE_MAP[chunk >> 18 & 63];
|
|
478
|
+
result[4 * i$1 + 1] = ENCODE_MAP[chunk >> 12 & 63];
|
|
479
|
+
result[4 * i$1 + 2] = ENCODE_MAP[chunk >> 6 & 63];
|
|
480
|
+
result[4 * i$1 + 3] = ENCODE_MAP[chunk >> 0 & 63];
|
|
481
|
+
}
|
|
482
|
+
const i = chunkLen;
|
|
483
|
+
switch (xLen % 3) {
|
|
484
|
+
case 1: {
|
|
485
|
+
const chunk = input[3 * i + 0] << 16;
|
|
486
|
+
result[4 * i + 0] = ENCODE_MAP[chunk >> 18 & 63];
|
|
487
|
+
result[4 * i + 1] = ENCODE_MAP[chunk >> 12 & 63];
|
|
488
|
+
result[4 * i + 2] = ENCODE_PAD;
|
|
489
|
+
result[4 * i + 3] = ENCODE_PAD;
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
case 2: {
|
|
493
|
+
const chunk = input[3 * i + 0] << 16 | input[3 * i + 1] << 8;
|
|
494
|
+
result[4 * i + 0] = ENCODE_MAP[chunk >> 18 & 63];
|
|
495
|
+
result[4 * i + 1] = ENCODE_MAP[chunk >> 12 & 63];
|
|
496
|
+
result[4 * i + 2] = ENCODE_MAP[chunk >> 6 & 63];
|
|
497
|
+
result[4 * i + 3] = ENCODE_PAD;
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return utf8ArrayToString(result);
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Decodes a Base64 encoded Uint8Array into a Uint8Array.
|
|
505
|
+
*
|
|
506
|
+
* @credit https://github.com/hi-ogawa/js-utils
|
|
507
|
+
*
|
|
508
|
+
* @param input - The Base64 encoded Uint8Array or string to decode.
|
|
509
|
+
* @returns The decoded Uint8Array.
|
|
510
|
+
*/
|
|
511
|
+
function decodeBase64(input) {
|
|
512
|
+
if (isString(input)) input = stringToUint8Array(input);
|
|
513
|
+
const yLen = input.length;
|
|
514
|
+
if (yLen % 4 !== 0) throw new Error("invalid length");
|
|
515
|
+
let padLen = 0;
|
|
516
|
+
while (padLen < 2 && padLen < input.length && input[input.length - 1 - padLen] === ENCODE_PAD) padLen++;
|
|
517
|
+
for (let i$1 = 0; i$1 < input.length - padLen; i$1++) if (isUndefined(input[i$1]) || isUndefined(DECODE_MAP$1[input[i$1]]) || DECODE_MAP$1[input[i$1]] >= 64) throw new Error("invalid data");
|
|
518
|
+
const chunkLen = Math.floor((yLen - padLen) / 4);
|
|
519
|
+
const xLen = 3 * chunkLen + (3 - padLen) % 3;
|
|
520
|
+
const result = new Uint8Array(xLen);
|
|
521
|
+
for (let i$1 = 0; i$1 < chunkLen; i$1++) {
|
|
522
|
+
const chunk = DECODE_MAP$1[input[4 * i$1 + 0]] << 18 | DECODE_MAP$1[input[4 * i$1 + 1]] << 12 | DECODE_MAP$1[input[4 * i$1 + 2]] << 6 | DECODE_MAP$1[input[4 * i$1 + 3]] << 0;
|
|
523
|
+
result[3 * i$1] = chunk >> 16 & 255;
|
|
524
|
+
result[3 * i$1 + 1] = chunk >> 8 & 255;
|
|
525
|
+
result[3 * i$1 + 2] = chunk >> 0 & 255;
|
|
526
|
+
}
|
|
527
|
+
const i = chunkLen;
|
|
528
|
+
switch (xLen % 3) {
|
|
529
|
+
case 1: {
|
|
530
|
+
const chunk = DECODE_MAP$1[input[4 * i + 0]] << 18 | DECODE_MAP$1[input[4 * i + 1]] << 12;
|
|
531
|
+
result[3 * i] = chunk >> 16 & 255;
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
case 2: {
|
|
535
|
+
const chunk = DECODE_MAP$1[input[4 * i + 0]] << 18 | DECODE_MAP$1[input[4 * i + 1]] << 12 | DECODE_MAP$1[input[4 * i + 2]] << 6;
|
|
536
|
+
result[3 * i] = chunk >> 16 & 255;
|
|
537
|
+
result[3 * i + 1] = chunk >> 8 & 255;
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return result;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Converts a Base64 encoded string to a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string.
|
|
545
|
+
*
|
|
546
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
|
|
547
|
+
*
|
|
548
|
+
* @param base64 - The Base64 encoded string to convert.
|
|
549
|
+
* @returns The Base64url encoded string.
|
|
550
|
+
*/
|
|
551
|
+
function base64UrlEncode(base64) {
|
|
552
|
+
return btoa(String.fromCharCode(...base64)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Converts a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string to a Base64 encoded string.
|
|
556
|
+
*
|
|
557
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
|
|
558
|
+
*
|
|
559
|
+
* @param base64url - The Base64url encoded string to convert.
|
|
560
|
+
* @returns The Base64 encoded string.
|
|
561
|
+
*/
|
|
562
|
+
function base64UrlDecode(base64url) {
|
|
563
|
+
const base64 = base64url.replace(/-/g, "+").replace(/_/g, "/");
|
|
564
|
+
return new Uint8Array([...atob(base64 + "=".repeat((4 - base64.length % 4) % 4))].map((c) => c.charCodeAt(0)));
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
//#endregion
|
|
568
|
+
//#region src/hex.ts
|
|
569
|
+
const ALPHABET = "0123456789ABCDEF";
|
|
570
|
+
const DECODE_MAP = {
|
|
571
|
+
"0": 0,
|
|
572
|
+
"1": 1,
|
|
573
|
+
"2": 2,
|
|
574
|
+
"3": 3,
|
|
575
|
+
"4": 4,
|
|
576
|
+
"5": 5,
|
|
577
|
+
"6": 6,
|
|
578
|
+
"7": 7,
|
|
579
|
+
"8": 8,
|
|
580
|
+
"9": 9,
|
|
581
|
+
a: 10,
|
|
582
|
+
A: 10,
|
|
583
|
+
b: 11,
|
|
584
|
+
B: 11,
|
|
585
|
+
c: 12,
|
|
586
|
+
C: 12,
|
|
587
|
+
d: 13,
|
|
588
|
+
D: 13,
|
|
589
|
+
e: 14,
|
|
590
|
+
E: 14,
|
|
591
|
+
f: 15,
|
|
592
|
+
F: 15
|
|
593
|
+
};
|
|
594
|
+
/**
|
|
595
|
+
* Encodes a Uint8Array into a hexadecimal string.
|
|
596
|
+
*
|
|
597
|
+
* @param input - The input Uint8Array.
|
|
598
|
+
* @returns The hexadecimal string.
|
|
599
|
+
*/
|
|
600
|
+
function encodeHex(input) {
|
|
601
|
+
let result = "";
|
|
602
|
+
for (let i = 0; i < input.length; i++) {
|
|
603
|
+
result += ALPHABET[input[i] >> 4];
|
|
604
|
+
result += ALPHABET[input[i] & 15];
|
|
605
|
+
}
|
|
606
|
+
return result;
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Encodes a Uint8Array into an uppercase hexadecimal string.
|
|
610
|
+
*
|
|
611
|
+
* @param input - The input Uint8Array.
|
|
612
|
+
* @returns The uppercase hexadecimal string.
|
|
613
|
+
*/
|
|
614
|
+
function decodeHex(input) {
|
|
615
|
+
if (input.length % 2 !== 0) throw new Error("Invalid hex string");
|
|
616
|
+
const result = new Uint8Array(input.length / 2);
|
|
617
|
+
for (let i = 0; i < input.length; i += 2) {
|
|
618
|
+
if (!(input[i] in DECODE_MAP)) throw new Error("Invalid character");
|
|
619
|
+
if (!(input[i + 1] in DECODE_MAP)) throw new Error("Invalid character");
|
|
620
|
+
result[i / 2] |= DECODE_MAP[input[i]] << 4;
|
|
621
|
+
result[i / 2] |= DECODE_MAP[input[i + 1]];
|
|
622
|
+
}
|
|
623
|
+
return result;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
//#endregion
|
|
627
|
+
//#region src/encryption.ts
|
|
628
|
+
/**
|
|
629
|
+
* Creates a CryptoKey object that can be used to encrypt any string.
|
|
630
|
+
*
|
|
631
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
|
|
632
|
+
*
|
|
633
|
+
* @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings.
|
|
634
|
+
*/
|
|
635
|
+
async function createKey() {
|
|
636
|
+
return crypto.subtle.generateKey({
|
|
637
|
+
name: "AES-GCM",
|
|
638
|
+
length: 256
|
|
639
|
+
}, true, ["encrypt", "decrypt"]);
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Encodes a CryptoKey to base64 string, so that it can be embedded in JSON / JavaScript
|
|
643
|
+
*
|
|
644
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
|
|
645
|
+
*
|
|
646
|
+
* @param key - The CryptoKey to encode
|
|
647
|
+
* @returns A promise that resolves to a base64 string representing the key
|
|
648
|
+
*/
|
|
649
|
+
async function encodeKey(key) {
|
|
650
|
+
const exported = await crypto.subtle.exportKey("raw", key);
|
|
651
|
+
return encodeBase64(new Uint8Array(exported)).toString();
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Decodes a base64 string into bytes and then imports the key.
|
|
655
|
+
*
|
|
656
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
|
|
657
|
+
*
|
|
658
|
+
* @param encoded - The base64 encoded key
|
|
659
|
+
* @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings
|
|
660
|
+
*/
|
|
661
|
+
async function decodeKey(encoded) {
|
|
662
|
+
const bytes = decodeBase64(encoded);
|
|
663
|
+
return crypto.subtle.importKey("raw", bytes.buffer, "AES-GCM", true, ["encrypt", "decrypt"]);
|
|
664
|
+
}
|
|
665
|
+
const IV_LENGTH = 24;
|
|
666
|
+
/**
|
|
667
|
+
* Using a CryptoKey, use AES-GCM to encrypt a string into a base64 string.
|
|
668
|
+
*
|
|
669
|
+
* @remarks
|
|
670
|
+
* The initialization vector is randomly generated and prepended to the encrypted string. The IV is required for decryption, so it must be stored alongside the encrypted data.
|
|
671
|
+
*
|
|
672
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
|
|
673
|
+
*
|
|
674
|
+
* @param key - The CryptoKey to use for encryption
|
|
675
|
+
* @param plaintext - The plaintext string to encrypt
|
|
676
|
+
* @returns A promise that resolves to a base64 string representing the encrypted data
|
|
677
|
+
*/
|
|
678
|
+
async function encrypt(key, plaintext) {
|
|
679
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH / 2));
|
|
680
|
+
const encrypted = await crypto.subtle.encrypt({
|
|
681
|
+
name: "AES-GCM",
|
|
682
|
+
iv
|
|
683
|
+
}, key, stringToUtf8Array(plaintext));
|
|
684
|
+
return encodeHex(iv) + encodeBase64(new Uint8Array(encrypted));
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Takes a base64 encoded string, decodes it and returns the AES-GCM decrypted text.
|
|
688
|
+
*
|
|
689
|
+
* @remarks
|
|
690
|
+
* The initialization vector is expected to be prepended to the encrypted string. The IV is required for decryption, so it must be extracted from the start of the string.
|
|
691
|
+
*
|
|
692
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
|
|
693
|
+
*
|
|
694
|
+
* @param key - The CryptoKey to use for decryption
|
|
695
|
+
* @param encrypted - The encrypted base64 encoded string to decrypt
|
|
696
|
+
* @returns A promise that resolves to the decrypted string
|
|
697
|
+
*/
|
|
698
|
+
async function decrypt(key, encrypted) {
|
|
699
|
+
return utf8ArrayToString(await crypto.subtle.decrypt({
|
|
700
|
+
name: "AES-GCM",
|
|
701
|
+
iv: decodeHex(encrypted.slice(0, IV_LENGTH))
|
|
702
|
+
}, key, decodeBase64(encrypted.slice(IV_LENGTH))));
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Encrypts a buffer using AES-GCM with a given CryptoKey.
|
|
706
|
+
*
|
|
707
|
+
* @remarks
|
|
708
|
+
* The initialization vector (IV) is randomly generated and prepended to the encrypted data. The resulting data is then encoded as a base64 string for easy storage/transmission.
|
|
709
|
+
*
|
|
710
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
|
|
711
|
+
*
|
|
712
|
+
* @param key - The CryptoKey to use for encryption
|
|
713
|
+
* @param buffer - The buffer to encrypt
|
|
714
|
+
* @returns A promise that resolves to a base64 string representing the encrypted data
|
|
715
|
+
*/
|
|
716
|
+
async function encryptBuffer(key, buffer) {
|
|
717
|
+
const iv = crypto.getRandomValues(new Uint8Array(16));
|
|
718
|
+
const encrypted = await crypto.subtle.encrypt({
|
|
719
|
+
name: "AES-GCM",
|
|
720
|
+
iv
|
|
721
|
+
}, key, buffer);
|
|
722
|
+
return uint8ArrayToString(concatUint8Array([iv, new Uint8Array(encrypted)]));
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Decrypts a buffer using AES-GCM with a given CryptoKey.
|
|
726
|
+
*
|
|
727
|
+
* @remarks
|
|
728
|
+
* The initialization vector (IV) is expected to be prepended to the encrypted data. The IV is required for decryption, so it must be extracted from the start of the buffer.
|
|
729
|
+
*
|
|
730
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
|
|
731
|
+
*
|
|
732
|
+
* @param key - The CryptoKey to use for decryption
|
|
733
|
+
* @param encrypted - The encrypted base64 encoded string to decrypt
|
|
734
|
+
* @returns A promise that resolves to the decrypted string
|
|
735
|
+
*/
|
|
736
|
+
async function decryptBuffer(key, encrypted) {
|
|
737
|
+
const concatenated = base64StringToUint8Array(encrypted);
|
|
738
|
+
return crypto.subtle.decrypt({
|
|
739
|
+
name: "AES-GCM",
|
|
740
|
+
iv: concatenated.slice(0, 16)
|
|
741
|
+
}, key, concatenated.slice(16));
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
//#endregion
|
|
745
|
+
//#region src/random.ts
|
|
746
|
+
/**
|
|
747
|
+
* Generate a random byte array of the specified length using the Web Crypto API.
|
|
748
|
+
*
|
|
749
|
+
* @param length - The length of the random byte array to generate (default is 32 bytes)
|
|
750
|
+
* @returns A Uint8Array containing random bytes of the specified length
|
|
751
|
+
*/
|
|
752
|
+
function generateRandomBytes(length = 32) {
|
|
753
|
+
return crypto.getRandomValues(new Uint8Array(length));
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Generate a random string of the specified length using characters A-Z, a-z, and 0-9 for CSRF tokens, etc.
|
|
757
|
+
*
|
|
758
|
+
* @remarks
|
|
759
|
+
* This function uses the Web Crypto API's `crypto.getRandomValues` to generate secure random bytes,
|
|
760
|
+
* and then maps those bytes to characters in the specified character set. It uses rejection sampling
|
|
761
|
+
* to ensure a uniform distribution of characters without modulo bias.
|
|
762
|
+
*
|
|
763
|
+
* @param length - The length of the random string to generate (default is 32 characters)
|
|
764
|
+
* @returns A random string of the specified length
|
|
765
|
+
*/
|
|
766
|
+
function generateRandomString(length = 32) {
|
|
767
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
768
|
+
const charsLen = 62;
|
|
769
|
+
const maxValid = 256 - 256 % charsLen;
|
|
770
|
+
const result = [];
|
|
771
|
+
while (result.length < length) {
|
|
772
|
+
const bytes = generateRandomBytes(length - result.length);
|
|
773
|
+
for (const b of bytes) if (b < maxValid && result.length < length && chars[b % charsLen]) result.push(chars[b % charsLen]);
|
|
774
|
+
}
|
|
775
|
+
return result.join("");
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
//#endregion
|
|
779
|
+
Object.defineProperty(exports, 'base64UrlDecode', {
|
|
780
|
+
enumerable: true,
|
|
781
|
+
get: function () {
|
|
782
|
+
return base64UrlDecode;
|
|
783
|
+
}
|
|
784
|
+
});
|
|
785
|
+
Object.defineProperty(exports, 'base64UrlEncode', {
|
|
786
|
+
enumerable: true,
|
|
787
|
+
get: function () {
|
|
788
|
+
return base64UrlEncode;
|
|
789
|
+
}
|
|
790
|
+
});
|
|
791
|
+
Object.defineProperty(exports, 'createKey', {
|
|
792
|
+
enumerable: true,
|
|
793
|
+
get: function () {
|
|
794
|
+
return createKey;
|
|
795
|
+
}
|
|
796
|
+
});
|
|
797
|
+
Object.defineProperty(exports, 'decodeBase64', {
|
|
798
|
+
enumerable: true,
|
|
799
|
+
get: function () {
|
|
800
|
+
return decodeBase64;
|
|
801
|
+
}
|
|
802
|
+
});
|
|
803
|
+
Object.defineProperty(exports, 'decodeHex', {
|
|
804
|
+
enumerable: true,
|
|
805
|
+
get: function () {
|
|
806
|
+
return decodeHex;
|
|
807
|
+
}
|
|
808
|
+
});
|
|
809
|
+
Object.defineProperty(exports, 'decodeKey', {
|
|
810
|
+
enumerable: true,
|
|
811
|
+
get: function () {
|
|
812
|
+
return decodeKey;
|
|
813
|
+
}
|
|
814
|
+
});
|
|
815
|
+
Object.defineProperty(exports, 'decrypt', {
|
|
816
|
+
enumerable: true,
|
|
817
|
+
get: function () {
|
|
818
|
+
return decrypt;
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
Object.defineProperty(exports, 'decryptBuffer', {
|
|
822
|
+
enumerable: true,
|
|
823
|
+
get: function () {
|
|
824
|
+
return decryptBuffer;
|
|
825
|
+
}
|
|
826
|
+
});
|
|
827
|
+
Object.defineProperty(exports, 'encodeBase64', {
|
|
828
|
+
enumerable: true,
|
|
829
|
+
get: function () {
|
|
830
|
+
return encodeBase64;
|
|
831
|
+
}
|
|
832
|
+
});
|
|
833
|
+
Object.defineProperty(exports, 'encodeHex', {
|
|
834
|
+
enumerable: true,
|
|
835
|
+
get: function () {
|
|
836
|
+
return encodeHex;
|
|
837
|
+
}
|
|
838
|
+
});
|
|
839
|
+
Object.defineProperty(exports, 'encodeKey', {
|
|
840
|
+
enumerable: true,
|
|
841
|
+
get: function () {
|
|
842
|
+
return encodeKey;
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
Object.defineProperty(exports, 'encrypt', {
|
|
846
|
+
enumerable: true,
|
|
847
|
+
get: function () {
|
|
848
|
+
return encrypt;
|
|
849
|
+
}
|
|
850
|
+
});
|
|
851
|
+
Object.defineProperty(exports, 'encryptBuffer', {
|
|
852
|
+
enumerable: true,
|
|
853
|
+
get: function () {
|
|
854
|
+
return encryptBuffer;
|
|
855
|
+
}
|
|
856
|
+
});
|
|
857
|
+
Object.defineProperty(exports, 'generateRandomBytes', {
|
|
858
|
+
enumerable: true,
|
|
859
|
+
get: function () {
|
|
860
|
+
return generateRandomBytes;
|
|
861
|
+
}
|
|
862
|
+
});
|
|
863
|
+
Object.defineProperty(exports, 'generateRandomString', {
|
|
864
|
+
enumerable: true,
|
|
865
|
+
get: function () {
|
|
866
|
+
return generateRandomString;
|
|
867
|
+
}
|
|
868
|
+
});
|