@stryke/crypto 0.5.43 → 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.
Files changed (95) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cloudflare.cjs +960 -0
  3. package/dist/cloudflare.d.cts +251 -0
  4. package/dist/cloudflare.d.cts.map +1 -0
  5. package/dist/cloudflare.d.mts +251 -0
  6. package/dist/cloudflare.d.mts.map +1 -0
  7. package/dist/cloudflare.mjs +942 -0
  8. package/dist/cloudflare.mjs.map +1 -0
  9. package/dist/index.cjs +16 -12
  10. package/dist/index.d.cts +2 -4
  11. package/dist/index.d.mts +2 -4
  12. package/dist/index.mjs +2 -4
  13. package/dist/neutral-BISXAuIW.cjs +868 -0
  14. package/dist/{encryption.d.mts → neutral-BJmyEdJq.d.mts} +77 -2
  15. package/dist/neutral-BJmyEdJq.d.mts.map +1 -0
  16. package/dist/neutral-BtZoJSl-.mjs +779 -0
  17. package/dist/neutral-BtZoJSl-.mjs.map +1 -0
  18. package/dist/{encryption.d.cts → neutral-CW6EXiYo.d.cts} +77 -2
  19. package/dist/neutral-CW6EXiYo.d.cts.map +1 -0
  20. package/dist/neutral.cjs +16 -16
  21. package/dist/neutral.d.cts +2 -4
  22. package/dist/neutral.d.mts +2 -4
  23. package/dist/neutral.mjs +2 -4
  24. package/dist/node.cjs +663 -0
  25. package/dist/node.d.cts +117 -0
  26. package/dist/node.d.cts.map +1 -0
  27. package/dist/node.d.mts +117 -0
  28. package/dist/node.d.mts.map +1 -0
  29. package/dist/node.mjs +654 -0
  30. package/dist/node.mjs.map +1 -0
  31. package/package.json +55 -20
  32. package/dist/base-64.cjs +0 -440
  33. package/dist/base-64.d.cts +0 -40
  34. package/dist/base-64.d.cts.map +0 -1
  35. package/dist/base-64.d.mts +0 -40
  36. package/dist/base-64.d.mts.map +0 -1
  37. package/dist/base-64.mjs +0 -438
  38. package/dist/base-64.mjs.map +0 -1
  39. package/dist/convert/src/array-buffer-to-string.cjs +0 -19
  40. package/dist/convert/src/array-buffer-to-string.mjs +0 -19
  41. package/dist/convert/src/array-buffer-to-string.mjs.map +0 -1
  42. package/dist/convert/src/neutral.cjs +0 -7
  43. package/dist/convert/src/neutral.mjs +0 -9
  44. package/dist/convert/src/parse-type-definition.cjs +0 -1
  45. package/dist/convert/src/parse-type-definition.mjs +0 -3
  46. package/dist/convert/src/string-to-uint8-array.cjs +0 -24
  47. package/dist/convert/src/string-to-uint8-array.mjs +0 -23
  48. package/dist/convert/src/string-to-uint8-array.mjs.map +0 -1
  49. package/dist/convert/src/string-to-utf8-array.cjs +0 -15
  50. package/dist/convert/src/string-to-utf8-array.mjs +0 -15
  51. package/dist/convert/src/string-to-utf8-array.mjs.map +0 -1
  52. package/dist/convert/src/uint8-array-to-stream.cjs +0 -22
  53. package/dist/convert/src/uint8-array-to-stream.mjs +0 -22
  54. package/dist/convert/src/uint8-array-to-stream.mjs.map +0 -1
  55. package/dist/convert/src/uint8-array-to-string.cjs +0 -15
  56. package/dist/convert/src/uint8-array-to-string.mjs +0 -16
  57. package/dist/convert/src/uint8-array-to-string.mjs.map +0 -1
  58. package/dist/convert/src/utf8-array-to-string.cjs +0 -15
  59. package/dist/convert/src/utf8-array-to-string.mjs +0 -15
  60. package/dist/convert/src/utf8-array-to-string.mjs.map +0 -1
  61. package/dist/encryption.cjs +0 -134
  62. package/dist/encryption.d.cts.map +0 -1
  63. package/dist/encryption.d.mts.map +0 -1
  64. package/dist/encryption.mjs +0 -129
  65. package/dist/encryption.mjs.map +0 -1
  66. package/dist/encryption.node.cjs +0 -63
  67. package/dist/encryption.node.d.cts +0 -42
  68. package/dist/encryption.node.d.cts.map +0 -1
  69. package/dist/encryption.node.d.mts +0 -42
  70. package/dist/encryption.node.d.mts.map +0 -1
  71. package/dist/encryption.node.mjs +0 -62
  72. package/dist/encryption.node.mjs.map +0 -1
  73. package/dist/hex.cjs +0 -62
  74. package/dist/hex.d.cts +0 -18
  75. package/dist/hex.d.cts.map +0 -1
  76. package/dist/hex.d.mts +0 -18
  77. package/dist/hex.d.mts.map +0 -1
  78. package/dist/hex.mjs +0 -61
  79. package/dist/hex.mjs.map +0 -1
  80. package/dist/type-checks/src/index.cjs +0 -5
  81. package/dist/type-checks/src/index.mjs +0 -7
  82. package/dist/type-checks/src/is-buffer.cjs +0 -12
  83. package/dist/type-checks/src/is-buffer.mjs +0 -12
  84. package/dist/type-checks/src/is-buffer.mjs.map +0 -1
  85. package/dist/type-checks/src/is-collection.cjs +0 -1
  86. package/dist/type-checks/src/is-collection.mjs +0 -3
  87. package/dist/type-checks/src/is-string.cjs +0 -12
  88. package/dist/type-checks/src/is-string.mjs +0 -12
  89. package/dist/type-checks/src/is-string.mjs.map +0 -1
  90. package/dist/type-checks/src/is-undefined.cjs +0 -8
  91. package/dist/type-checks/src/is-undefined.mjs +0 -8
  92. package/dist/type-checks/src/is-undefined.mjs.map +0 -1
  93. package/dist/type-checks/src/type-detect.cjs +0 -15
  94. package/dist/type-checks/src/type-detect.mjs +0 -17
  95. package/dist/type-checks/src/type-detect.mjs.map +0 -1
@@ -0,0 +1,779 @@
1
+ //#region ../convert/src/array-buffer-to-string.ts
2
+ /**
3
+ * Convert an ArrayBuffer or Uint8Array to a string
4
+ *
5
+ * @param buffer - The ArrayBuffer or Uint8Array to convert
6
+ * @returns The converted string
7
+ */
8
+ function arrayBufferToString(buffer) {
9
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
10
+ const len = bytes.byteLength;
11
+ if (len < 65535) return String.fromCharCode.apply(null, bytes);
12
+ let binary = "";
13
+ for (let i = 0; i < len; i++) binary += String.fromCharCode(bytes[i]);
14
+ return binary;
15
+ }
16
+
17
+ //#endregion
18
+ //#region ../type-checks/src/is-undefined.ts
19
+ const isUndefined = (value) => {
20
+ return value === void 0;
21
+ };
22
+
23
+ //#endregion
24
+ //#region ../type-checks/src/is-buffer.ts
25
+ const isBufferExists = typeof Buffer !== "undefined";
26
+ /**
27
+ * Check if the provided value's type is `Buffer`
28
+ */
29
+ const isBuffer = isBufferExists ? Buffer.isBuffer.bind(Buffer) : function isBuffer$1(value) {
30
+ return false;
31
+ };
32
+
33
+ //#endregion
34
+ //#region ../type-checks/src/type-detect.ts
35
+ const globalObject = ((Obj) => {
36
+ if (typeof globalThis === "object") return globalThis;
37
+ Object.defineProperty(Obj, "typeDetectGlobalObject", {
38
+ get() {
39
+ return this;
40
+ },
41
+ configurable: true
42
+ });
43
+ return globalThis;
44
+ })(Object.prototype);
45
+
46
+ //#endregion
47
+ //#region ../type-checks/src/is-string.ts
48
+ const isString = (value) => {
49
+ try {
50
+ return typeof value === "string";
51
+ } catch {
52
+ return false;
53
+ }
54
+ };
55
+
56
+ //#endregion
57
+ //#region ../convert/src/string-to-uint8-array.ts
58
+ /**
59
+ * Convert a string to Uint8Array
60
+ *
61
+ * @param text - The text to convert
62
+ * @returns The converted Uint8Array
63
+ */
64
+ function stringToUint8Array(text) {
65
+ return Uint8Array.from([...encodeURIComponent(text)].map((letter) => letter.codePointAt(0)));
66
+ }
67
+ /**
68
+ * Convert a base64 string to a Uint8Array
69
+ *
70
+ * @param data - The base64 string to convert
71
+ * @returns The converted Uint8Array
72
+ */
73
+ function base64StringToUint8Array(data) {
74
+ return stringToUint8Array(atob(data));
75
+ }
76
+
77
+ //#endregion
78
+ //#region ../convert/src/string-to-utf8-array.ts
79
+ const encoder = new TextEncoder();
80
+ /**
81
+ * Convert a string to a utf-8 array
82
+ *
83
+ * @param input - The string to convert
84
+ * @returns The converted utf-8 array
85
+ */
86
+ function stringToUtf8Array(input) {
87
+ return encoder.encode(input);
88
+ }
89
+
90
+ //#endregion
91
+ //#region ../convert/src/uint8-array-to-stream.ts
92
+ /**
93
+ * Concatenate an array of Uint8Array chunks into a single Uint8Array
94
+ *
95
+ * @param chunks - Array of Uint8Array chunks to concatenate
96
+ * @returns The concatenated Uint8Array
97
+ */
98
+ function concatUint8Array(chunks) {
99
+ let total = 0;
100
+ for (const chunk of chunks) total += chunk.length;
101
+ const result = new Uint8Array(total);
102
+ let offset = 0;
103
+ for (const chunk of chunks) {
104
+ result.set(chunk, offset);
105
+ offset += chunk.length;
106
+ }
107
+ return result;
108
+ }
109
+
110
+ //#endregion
111
+ //#region ../convert/src/uint8-array-to-string.ts
112
+ /**
113
+ * Convert a Uint8Array to a base64 string
114
+ *
115
+ * @param buffer - The Uint8Array to convert
116
+ * @returns The converted base64 string
117
+ */
118
+ function uint8ArrayToString(buffer) {
119
+ return btoa(arrayBufferToString(buffer));
120
+ }
121
+
122
+ //#endregion
123
+ //#region ../convert/src/utf8-array-to-string.ts
124
+ const decoder = new TextDecoder();
125
+ /**
126
+ * Convert a utf-8 array to string
127
+ *
128
+ * @param input - Utf-8 Array
129
+ * @returns The converted string
130
+ */
131
+ function utf8ArrayToString(input) {
132
+ return decoder.decode(input);
133
+ }
134
+
135
+ //#endregion
136
+ //#region src/base-64.ts
137
+ const ENCODE_MAP = new Uint8Array([
138
+ 65,
139
+ 66,
140
+ 67,
141
+ 68,
142
+ 69,
143
+ 70,
144
+ 71,
145
+ 72,
146
+ 73,
147
+ 74,
148
+ 75,
149
+ 76,
150
+ 77,
151
+ 78,
152
+ 79,
153
+ 80,
154
+ 81,
155
+ 82,
156
+ 83,
157
+ 84,
158
+ 85,
159
+ 86,
160
+ 87,
161
+ 88,
162
+ 89,
163
+ 90,
164
+ 97,
165
+ 98,
166
+ 99,
167
+ 100,
168
+ 101,
169
+ 102,
170
+ 103,
171
+ 104,
172
+ 105,
173
+ 106,
174
+ 107,
175
+ 108,
176
+ 109,
177
+ 110,
178
+ 111,
179
+ 112,
180
+ 113,
181
+ 114,
182
+ 115,
183
+ 116,
184
+ 117,
185
+ 118,
186
+ 119,
187
+ 120,
188
+ 121,
189
+ 122,
190
+ 48,
191
+ 49,
192
+ 50,
193
+ 51,
194
+ 52,
195
+ 53,
196
+ 54,
197
+ 55,
198
+ 56,
199
+ 57,
200
+ 43,
201
+ 47
202
+ ]);
203
+ const ENCODE_PAD = 61;
204
+ const DECODE_MAP$1 = new Uint8Array([
205
+ 100,
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
+ 62,
249
+ 100,
250
+ 100,
251
+ 100,
252
+ 63,
253
+ 52,
254
+ 53,
255
+ 54,
256
+ 55,
257
+ 56,
258
+ 57,
259
+ 58,
260
+ 59,
261
+ 60,
262
+ 61,
263
+ 100,
264
+ 100,
265
+ 100,
266
+ 100,
267
+ 100,
268
+ 100,
269
+ 100,
270
+ 0,
271
+ 1,
272
+ 2,
273
+ 3,
274
+ 4,
275
+ 5,
276
+ 6,
277
+ 7,
278
+ 8,
279
+ 9,
280
+ 10,
281
+ 11,
282
+ 12,
283
+ 13,
284
+ 14,
285
+ 15,
286
+ 16,
287
+ 17,
288
+ 18,
289
+ 19,
290
+ 20,
291
+ 21,
292
+ 22,
293
+ 23,
294
+ 24,
295
+ 25,
296
+ 100,
297
+ 100,
298
+ 100,
299
+ 100,
300
+ 100,
301
+ 100,
302
+ 26,
303
+ 27,
304
+ 28,
305
+ 29,
306
+ 30,
307
+ 31,
308
+ 32,
309
+ 33,
310
+ 34,
311
+ 35,
312
+ 36,
313
+ 37,
314
+ 38,
315
+ 39,
316
+ 40,
317
+ 41,
318
+ 42,
319
+ 43,
320
+ 44,
321
+ 45,
322
+ 46,
323
+ 47,
324
+ 48,
325
+ 49,
326
+ 50,
327
+ 51,
328
+ 100,
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
+ ]);
461
+ /**
462
+ * Encodes a Uint8Array into a Base64 encoded Uint8Array.
463
+ *
464
+ * @credit https://github.com/hi-ogawa/js-utils
465
+ *
466
+ * @param input - The input Uint8Array or string to encode.
467
+ * @returns The Base64 encoded Uint8Array.
468
+ */
469
+ function encodeBase64(input) {
470
+ if (isString(input)) input = stringToUint8Array(input);
471
+ const xLen = input.length;
472
+ const result = new Uint8Array(Math.ceil(xLen / 3) * 4);
473
+ const chunkLen = Math.floor(xLen / 3);
474
+ for (let i$1 = 0; i$1 < chunkLen; i$1++) {
475
+ const chunk = input[3 * i$1 + 0] << 16 | input[3 * i$1 + 1] << 8 | input[3 * i$1 + 2];
476
+ result[4 * i$1 + 0] = ENCODE_MAP[chunk >> 18 & 63];
477
+ result[4 * i$1 + 1] = ENCODE_MAP[chunk >> 12 & 63];
478
+ result[4 * i$1 + 2] = ENCODE_MAP[chunk >> 6 & 63];
479
+ result[4 * i$1 + 3] = ENCODE_MAP[chunk >> 0 & 63];
480
+ }
481
+ const i = chunkLen;
482
+ switch (xLen % 3) {
483
+ case 1: {
484
+ const chunk = input[3 * i + 0] << 16;
485
+ result[4 * i + 0] = ENCODE_MAP[chunk >> 18 & 63];
486
+ result[4 * i + 1] = ENCODE_MAP[chunk >> 12 & 63];
487
+ result[4 * i + 2] = ENCODE_PAD;
488
+ result[4 * i + 3] = ENCODE_PAD;
489
+ break;
490
+ }
491
+ case 2: {
492
+ const chunk = input[3 * i + 0] << 16 | input[3 * i + 1] << 8;
493
+ result[4 * i + 0] = ENCODE_MAP[chunk >> 18 & 63];
494
+ result[4 * i + 1] = ENCODE_MAP[chunk >> 12 & 63];
495
+ result[4 * i + 2] = ENCODE_MAP[chunk >> 6 & 63];
496
+ result[4 * i + 3] = ENCODE_PAD;
497
+ break;
498
+ }
499
+ }
500
+ return utf8ArrayToString(result);
501
+ }
502
+ /**
503
+ * Decodes a Base64 encoded Uint8Array into a Uint8Array.
504
+ *
505
+ * @credit https://github.com/hi-ogawa/js-utils
506
+ *
507
+ * @param input - The Base64 encoded Uint8Array or string to decode.
508
+ * @returns The decoded Uint8Array.
509
+ */
510
+ function decodeBase64(input) {
511
+ if (isString(input)) input = stringToUint8Array(input);
512
+ const yLen = input.length;
513
+ if (yLen % 4 !== 0) throw new Error("invalid length");
514
+ let padLen = 0;
515
+ while (padLen < 2 && padLen < input.length && input[input.length - 1 - padLen] === ENCODE_PAD) padLen++;
516
+ 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");
517
+ const chunkLen = Math.floor((yLen - padLen) / 4);
518
+ const xLen = 3 * chunkLen + (3 - padLen) % 3;
519
+ const result = new Uint8Array(xLen);
520
+ for (let i$1 = 0; i$1 < chunkLen; i$1++) {
521
+ 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;
522
+ result[3 * i$1] = chunk >> 16 & 255;
523
+ result[3 * i$1 + 1] = chunk >> 8 & 255;
524
+ result[3 * i$1 + 2] = chunk >> 0 & 255;
525
+ }
526
+ const i = chunkLen;
527
+ switch (xLen % 3) {
528
+ case 1: {
529
+ const chunk = DECODE_MAP$1[input[4 * i + 0]] << 18 | DECODE_MAP$1[input[4 * i + 1]] << 12;
530
+ result[3 * i] = chunk >> 16 & 255;
531
+ break;
532
+ }
533
+ case 2: {
534
+ 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;
535
+ result[3 * i] = chunk >> 16 & 255;
536
+ result[3 * i + 1] = chunk >> 8 & 255;
537
+ break;
538
+ }
539
+ }
540
+ return result;
541
+ }
542
+ /**
543
+ * Converts a Base64 encoded string to a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string.
544
+ *
545
+ * @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
546
+ *
547
+ * @param base64 - The Base64 encoded string to convert.
548
+ * @returns The Base64url encoded string.
549
+ */
550
+ function base64UrlEncode(base64) {
551
+ return btoa(String.fromCharCode(...base64)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
552
+ }
553
+ /**
554
+ * Converts a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string to a Base64 encoded string.
555
+ *
556
+ * @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
557
+ *
558
+ * @param base64url - The Base64url encoded string to convert.
559
+ * @returns The Base64 encoded string.
560
+ */
561
+ function base64UrlDecode(base64url) {
562
+ const base64 = base64url.replace(/-/g, "+").replace(/_/g, "/");
563
+ return new Uint8Array([...atob(base64 + "=".repeat((4 - base64.length % 4) % 4))].map((c) => c.charCodeAt(0)));
564
+ }
565
+
566
+ //#endregion
567
+ //#region src/hex.ts
568
+ const ALPHABET = "0123456789ABCDEF";
569
+ const DECODE_MAP = {
570
+ "0": 0,
571
+ "1": 1,
572
+ "2": 2,
573
+ "3": 3,
574
+ "4": 4,
575
+ "5": 5,
576
+ "6": 6,
577
+ "7": 7,
578
+ "8": 8,
579
+ "9": 9,
580
+ a: 10,
581
+ A: 10,
582
+ b: 11,
583
+ B: 11,
584
+ c: 12,
585
+ C: 12,
586
+ d: 13,
587
+ D: 13,
588
+ e: 14,
589
+ E: 14,
590
+ f: 15,
591
+ F: 15
592
+ };
593
+ /**
594
+ * Encodes a Uint8Array into a hexadecimal string.
595
+ *
596
+ * @param input - The input Uint8Array.
597
+ * @returns The hexadecimal string.
598
+ */
599
+ function encodeHex(input) {
600
+ let result = "";
601
+ for (let i = 0; i < input.length; i++) {
602
+ result += ALPHABET[input[i] >> 4];
603
+ result += ALPHABET[input[i] & 15];
604
+ }
605
+ return result;
606
+ }
607
+ /**
608
+ * Encodes a Uint8Array into an uppercase hexadecimal string.
609
+ *
610
+ * @param input - The input Uint8Array.
611
+ * @returns The uppercase hexadecimal string.
612
+ */
613
+ function decodeHex(input) {
614
+ if (input.length % 2 !== 0) throw new Error("Invalid hex string");
615
+ const result = new Uint8Array(input.length / 2);
616
+ for (let i = 0; i < input.length; i += 2) {
617
+ if (!(input[i] in DECODE_MAP)) throw new Error("Invalid character");
618
+ if (!(input[i + 1] in DECODE_MAP)) throw new Error("Invalid character");
619
+ result[i / 2] |= DECODE_MAP[input[i]] << 4;
620
+ result[i / 2] |= DECODE_MAP[input[i + 1]];
621
+ }
622
+ return result;
623
+ }
624
+
625
+ //#endregion
626
+ //#region src/encryption.ts
627
+ /**
628
+ * Creates a CryptoKey object that can be used to encrypt any string.
629
+ *
630
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
631
+ *
632
+ * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings.
633
+ */
634
+ async function createKey() {
635
+ return crypto.subtle.generateKey({
636
+ name: "AES-GCM",
637
+ length: 256
638
+ }, true, ["encrypt", "decrypt"]);
639
+ }
640
+ /**
641
+ * Encodes a CryptoKey to base64 string, so that it can be embedded in JSON / JavaScript
642
+ *
643
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
644
+ *
645
+ * @param key - The CryptoKey to encode
646
+ * @returns A promise that resolves to a base64 string representing the key
647
+ */
648
+ async function encodeKey(key) {
649
+ const exported = await crypto.subtle.exportKey("raw", key);
650
+ return encodeBase64(new Uint8Array(exported)).toString();
651
+ }
652
+ /**
653
+ * Decodes a base64 string into bytes and then imports the key.
654
+ *
655
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
656
+ *
657
+ * @param encoded - The base64 encoded key
658
+ * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings
659
+ */
660
+ async function decodeKey(encoded) {
661
+ const bytes = decodeBase64(encoded);
662
+ return crypto.subtle.importKey("raw", bytes.buffer, "AES-GCM", true, ["encrypt", "decrypt"]);
663
+ }
664
+ const IV_LENGTH = 24;
665
+ /**
666
+ * Using a CryptoKey, use AES-GCM to encrypt a string into a base64 string.
667
+ *
668
+ * @remarks
669
+ * 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.
670
+ *
671
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
672
+ *
673
+ * @param key - The CryptoKey to use for encryption
674
+ * @param plaintext - The plaintext string to encrypt
675
+ * @returns A promise that resolves to a base64 string representing the encrypted data
676
+ */
677
+ async function encrypt(key, plaintext) {
678
+ const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH / 2));
679
+ const encrypted = await crypto.subtle.encrypt({
680
+ name: "AES-GCM",
681
+ iv
682
+ }, key, stringToUtf8Array(plaintext));
683
+ return encodeHex(iv) + encodeBase64(new Uint8Array(encrypted));
684
+ }
685
+ /**
686
+ * Takes a base64 encoded string, decodes it and returns the AES-GCM decrypted text.
687
+ *
688
+ * @remarks
689
+ * 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.
690
+ *
691
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
692
+ *
693
+ * @param key - The CryptoKey to use for decryption
694
+ * @param encrypted - The encrypted base64 encoded string to decrypt
695
+ * @returns A promise that resolves to the decrypted string
696
+ */
697
+ async function decrypt(key, encrypted) {
698
+ return utf8ArrayToString(await crypto.subtle.decrypt({
699
+ name: "AES-GCM",
700
+ iv: decodeHex(encrypted.slice(0, IV_LENGTH))
701
+ }, key, decodeBase64(encrypted.slice(IV_LENGTH))));
702
+ }
703
+ /**
704
+ * Encrypts a buffer using AES-GCM with a given CryptoKey.
705
+ *
706
+ * @remarks
707
+ * 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.
708
+ *
709
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
710
+ *
711
+ * @param key - The CryptoKey to use for encryption
712
+ * @param buffer - The buffer to encrypt
713
+ * @returns A promise that resolves to a base64 string representing the encrypted data
714
+ */
715
+ async function encryptBuffer(key, buffer) {
716
+ const iv = crypto.getRandomValues(new Uint8Array(16));
717
+ const encrypted = await crypto.subtle.encrypt({
718
+ name: "AES-GCM",
719
+ iv
720
+ }, key, buffer);
721
+ return uint8ArrayToString(concatUint8Array([iv, new Uint8Array(encrypted)]));
722
+ }
723
+ /**
724
+ * Decrypts a buffer using AES-GCM with a given CryptoKey.
725
+ *
726
+ * @remarks
727
+ * 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.
728
+ *
729
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
730
+ *
731
+ * @param key - The CryptoKey to use for decryption
732
+ * @param encrypted - The encrypted base64 encoded string to decrypt
733
+ * @returns A promise that resolves to the decrypted string
734
+ */
735
+ async function decryptBuffer(key, encrypted) {
736
+ const concatenated = base64StringToUint8Array(encrypted);
737
+ return crypto.subtle.decrypt({
738
+ name: "AES-GCM",
739
+ iv: concatenated.slice(0, 16)
740
+ }, key, concatenated.slice(16));
741
+ }
742
+
743
+ //#endregion
744
+ //#region src/random.ts
745
+ /**
746
+ * Generate a random byte array of the specified length using the Web Crypto API.
747
+ *
748
+ * @param length - The length of the random byte array to generate (default is 32 bytes)
749
+ * @returns A Uint8Array containing random bytes of the specified length
750
+ */
751
+ function generateRandomBytes(length = 32) {
752
+ return crypto.getRandomValues(new Uint8Array(length));
753
+ }
754
+ /**
755
+ * Generate a random string of the specified length using characters A-Z, a-z, and 0-9 for CSRF tokens, etc.
756
+ *
757
+ * @remarks
758
+ * This function uses the Web Crypto API's `crypto.getRandomValues` to generate secure random bytes,
759
+ * and then maps those bytes to characters in the specified character set. It uses rejection sampling
760
+ * to ensure a uniform distribution of characters without modulo bias.
761
+ *
762
+ * @param length - The length of the random string to generate (default is 32 characters)
763
+ * @returns A random string of the specified length
764
+ */
765
+ function generateRandomString(length = 32) {
766
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
767
+ const charsLen = 62;
768
+ const maxValid = 256 - 256 % charsLen;
769
+ const result = [];
770
+ while (result.length < length) {
771
+ const bytes = generateRandomBytes(length - result.length);
772
+ for (const b of bytes) if (b < maxValid && result.length < length && chars[b % charsLen]) result.push(chars[b % charsLen]);
773
+ }
774
+ return result.join("");
775
+ }
776
+
777
+ //#endregion
778
+ export { decrypt as a, encrypt as c, encodeHex as d, base64UrlDecode as f, encodeBase64 as h, decodeKey as i, encryptBuffer as l, decodeBase64 as m, generateRandomString as n, decryptBuffer as o, base64UrlEncode as p, createKey as r, encodeKey as s, generateRandomBytes as t, decodeHex as u };
779
+ //# sourceMappingURL=neutral-BtZoJSl-.mjs.map