@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
package/dist/node.mjs ADDED
@@ -0,0 +1,654 @@
1
+ import { Buffer as Buffer$1 } from "node:buffer";
2
+ import { createCipheriv, createDecipheriv, createSecretKey, pbkdf2Sync, randomBytes } from "node:crypto";
3
+
4
+ //#region ../type-checks/src/is-undefined.ts
5
+ const isUndefined = (value) => {
6
+ return value === void 0;
7
+ };
8
+
9
+ //#endregion
10
+ //#region ../type-checks/src/is-buffer.ts
11
+ const isBufferExists = typeof Buffer !== "undefined";
12
+ /**
13
+ * Check if the provided value's type is `Buffer`
14
+ */
15
+ const isBuffer = isBufferExists ? Buffer.isBuffer.bind(Buffer) : function isBuffer$1(value) {
16
+ return false;
17
+ };
18
+
19
+ //#endregion
20
+ //#region ../type-checks/src/type-detect.ts
21
+ const globalObject = ((Obj) => {
22
+ if (typeof globalThis === "object") return globalThis;
23
+ Object.defineProperty(Obj, "typeDetectGlobalObject", {
24
+ get() {
25
+ return this;
26
+ },
27
+ configurable: true
28
+ });
29
+ return globalThis;
30
+ })(Object.prototype);
31
+
32
+ //#endregion
33
+ //#region ../type-checks/src/is-string.ts
34
+ const isString = (value) => {
35
+ try {
36
+ return typeof value === "string";
37
+ } catch {
38
+ return false;
39
+ }
40
+ };
41
+
42
+ //#endregion
43
+ //#region ../convert/src/string-to-uint8-array.ts
44
+ /**
45
+ * Convert a string to Uint8Array
46
+ *
47
+ * @param text - The text to convert
48
+ * @returns The converted Uint8Array
49
+ */
50
+ function stringToUint8Array(text) {
51
+ return Uint8Array.from([...encodeURIComponent(text)].map((letter) => letter.codePointAt(0)));
52
+ }
53
+
54
+ //#endregion
55
+ //#region ../convert/src/string-to-utf8-array.ts
56
+ const encoder = new TextEncoder();
57
+
58
+ //#endregion
59
+ //#region ../convert/src/utf8-array-to-string.ts
60
+ const decoder = new TextDecoder();
61
+ /**
62
+ * Convert a utf-8 array to string
63
+ *
64
+ * @param input - Utf-8 Array
65
+ * @returns The converted string
66
+ */
67
+ function utf8ArrayToString(input) {
68
+ return decoder.decode(input);
69
+ }
70
+
71
+ //#endregion
72
+ //#region src/base-64.ts
73
+ const ENCODE_MAP = new Uint8Array([
74
+ 65,
75
+ 66,
76
+ 67,
77
+ 68,
78
+ 69,
79
+ 70,
80
+ 71,
81
+ 72,
82
+ 73,
83
+ 74,
84
+ 75,
85
+ 76,
86
+ 77,
87
+ 78,
88
+ 79,
89
+ 80,
90
+ 81,
91
+ 82,
92
+ 83,
93
+ 84,
94
+ 85,
95
+ 86,
96
+ 87,
97
+ 88,
98
+ 89,
99
+ 90,
100
+ 97,
101
+ 98,
102
+ 99,
103
+ 100,
104
+ 101,
105
+ 102,
106
+ 103,
107
+ 104,
108
+ 105,
109
+ 106,
110
+ 107,
111
+ 108,
112
+ 109,
113
+ 110,
114
+ 111,
115
+ 112,
116
+ 113,
117
+ 114,
118
+ 115,
119
+ 116,
120
+ 117,
121
+ 118,
122
+ 119,
123
+ 120,
124
+ 121,
125
+ 122,
126
+ 48,
127
+ 49,
128
+ 50,
129
+ 51,
130
+ 52,
131
+ 53,
132
+ 54,
133
+ 55,
134
+ 56,
135
+ 57,
136
+ 43,
137
+ 47
138
+ ]);
139
+ const ENCODE_PAD = 61;
140
+ const DECODE_MAP$1 = new Uint8Array([
141
+ 100,
142
+ 100,
143
+ 100,
144
+ 100,
145
+ 100,
146
+ 100,
147
+ 100,
148
+ 100,
149
+ 100,
150
+ 100,
151
+ 100,
152
+ 100,
153
+ 100,
154
+ 100,
155
+ 100,
156
+ 100,
157
+ 100,
158
+ 100,
159
+ 100,
160
+ 100,
161
+ 100,
162
+ 100,
163
+ 100,
164
+ 100,
165
+ 100,
166
+ 100,
167
+ 100,
168
+ 100,
169
+ 100,
170
+ 100,
171
+ 100,
172
+ 100,
173
+ 100,
174
+ 100,
175
+ 100,
176
+ 100,
177
+ 100,
178
+ 100,
179
+ 100,
180
+ 100,
181
+ 100,
182
+ 100,
183
+ 100,
184
+ 62,
185
+ 100,
186
+ 100,
187
+ 100,
188
+ 63,
189
+ 52,
190
+ 53,
191
+ 54,
192
+ 55,
193
+ 56,
194
+ 57,
195
+ 58,
196
+ 59,
197
+ 60,
198
+ 61,
199
+ 100,
200
+ 100,
201
+ 100,
202
+ 100,
203
+ 100,
204
+ 100,
205
+ 100,
206
+ 0,
207
+ 1,
208
+ 2,
209
+ 3,
210
+ 4,
211
+ 5,
212
+ 6,
213
+ 7,
214
+ 8,
215
+ 9,
216
+ 10,
217
+ 11,
218
+ 12,
219
+ 13,
220
+ 14,
221
+ 15,
222
+ 16,
223
+ 17,
224
+ 18,
225
+ 19,
226
+ 20,
227
+ 21,
228
+ 22,
229
+ 23,
230
+ 24,
231
+ 25,
232
+ 100,
233
+ 100,
234
+ 100,
235
+ 100,
236
+ 100,
237
+ 100,
238
+ 26,
239
+ 27,
240
+ 28,
241
+ 29,
242
+ 30,
243
+ 31,
244
+ 32,
245
+ 33,
246
+ 34,
247
+ 35,
248
+ 36,
249
+ 37,
250
+ 38,
251
+ 39,
252
+ 40,
253
+ 41,
254
+ 42,
255
+ 43,
256
+ 44,
257
+ 45,
258
+ 46,
259
+ 47,
260
+ 48,
261
+ 49,
262
+ 50,
263
+ 51,
264
+ 100,
265
+ 100,
266
+ 100,
267
+ 100,
268
+ 100,
269
+ 100,
270
+ 100,
271
+ 100,
272
+ 100,
273
+ 100,
274
+ 100,
275
+ 100,
276
+ 100,
277
+ 100,
278
+ 100,
279
+ 100,
280
+ 100,
281
+ 100,
282
+ 100,
283
+ 100,
284
+ 100,
285
+ 100,
286
+ 100,
287
+ 100,
288
+ 100,
289
+ 100,
290
+ 100,
291
+ 100,
292
+ 100,
293
+ 100,
294
+ 100,
295
+ 100,
296
+ 100,
297
+ 100,
298
+ 100,
299
+ 100,
300
+ 100,
301
+ 100,
302
+ 100,
303
+ 100,
304
+ 100,
305
+ 100,
306
+ 100,
307
+ 100,
308
+ 100,
309
+ 100,
310
+ 100,
311
+ 100,
312
+ 100,
313
+ 100,
314
+ 100,
315
+ 100,
316
+ 100,
317
+ 100,
318
+ 100,
319
+ 100,
320
+ 100,
321
+ 100,
322
+ 100,
323
+ 100,
324
+ 100,
325
+ 100,
326
+ 100,
327
+ 100,
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
+ ]);
397
+ /**
398
+ * Encodes a Uint8Array into a Base64 encoded Uint8Array.
399
+ *
400
+ * @credit https://github.com/hi-ogawa/js-utils
401
+ *
402
+ * @param input - The input Uint8Array or string to encode.
403
+ * @returns The Base64 encoded Uint8Array.
404
+ */
405
+ function encodeBase64(input) {
406
+ if (isString(input)) input = stringToUint8Array(input);
407
+ const xLen = input.length;
408
+ const result = new Uint8Array(Math.ceil(xLen / 3) * 4);
409
+ const chunkLen = Math.floor(xLen / 3);
410
+ for (let i$1 = 0; i$1 < chunkLen; i$1++) {
411
+ const chunk = input[3 * i$1 + 0] << 16 | input[3 * i$1 + 1] << 8 | input[3 * i$1 + 2];
412
+ result[4 * i$1 + 0] = ENCODE_MAP[chunk >> 18 & 63];
413
+ result[4 * i$1 + 1] = ENCODE_MAP[chunk >> 12 & 63];
414
+ result[4 * i$1 + 2] = ENCODE_MAP[chunk >> 6 & 63];
415
+ result[4 * i$1 + 3] = ENCODE_MAP[chunk >> 0 & 63];
416
+ }
417
+ const i = chunkLen;
418
+ switch (xLen % 3) {
419
+ case 1: {
420
+ const chunk = input[3 * i + 0] << 16;
421
+ result[4 * i + 0] = ENCODE_MAP[chunk >> 18 & 63];
422
+ result[4 * i + 1] = ENCODE_MAP[chunk >> 12 & 63];
423
+ result[4 * i + 2] = ENCODE_PAD;
424
+ result[4 * i + 3] = ENCODE_PAD;
425
+ break;
426
+ }
427
+ case 2: {
428
+ const chunk = input[3 * i + 0] << 16 | input[3 * i + 1] << 8;
429
+ result[4 * i + 0] = ENCODE_MAP[chunk >> 18 & 63];
430
+ result[4 * i + 1] = ENCODE_MAP[chunk >> 12 & 63];
431
+ result[4 * i + 2] = ENCODE_MAP[chunk >> 6 & 63];
432
+ result[4 * i + 3] = ENCODE_PAD;
433
+ break;
434
+ }
435
+ }
436
+ return utf8ArrayToString(result);
437
+ }
438
+ /**
439
+ * Decodes a Base64 encoded Uint8Array into a Uint8Array.
440
+ *
441
+ * @credit https://github.com/hi-ogawa/js-utils
442
+ *
443
+ * @param input - The Base64 encoded Uint8Array or string to decode.
444
+ * @returns The decoded Uint8Array.
445
+ */
446
+ function decodeBase64(input) {
447
+ if (isString(input)) input = stringToUint8Array(input);
448
+ const yLen = input.length;
449
+ if (yLen % 4 !== 0) throw new Error("invalid length");
450
+ let padLen = 0;
451
+ while (padLen < 2 && padLen < input.length && input[input.length - 1 - padLen] === ENCODE_PAD) padLen++;
452
+ 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");
453
+ const chunkLen = Math.floor((yLen - padLen) / 4);
454
+ const xLen = 3 * chunkLen + (3 - padLen) % 3;
455
+ const result = new Uint8Array(xLen);
456
+ for (let i$1 = 0; i$1 < chunkLen; i$1++) {
457
+ 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;
458
+ result[3 * i$1] = chunk >> 16 & 255;
459
+ result[3 * i$1 + 1] = chunk >> 8 & 255;
460
+ result[3 * i$1 + 2] = chunk >> 0 & 255;
461
+ }
462
+ const i = chunkLen;
463
+ switch (xLen % 3) {
464
+ case 1: {
465
+ const chunk = DECODE_MAP$1[input[4 * i + 0]] << 18 | DECODE_MAP$1[input[4 * i + 1]] << 12;
466
+ result[3 * i] = chunk >> 16 & 255;
467
+ break;
468
+ }
469
+ case 2: {
470
+ 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;
471
+ result[3 * i] = chunk >> 16 & 255;
472
+ result[3 * i + 1] = chunk >> 8 & 255;
473
+ break;
474
+ }
475
+ }
476
+ return result;
477
+ }
478
+ /**
479
+ * Converts a Base64 encoded string to a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string.
480
+ *
481
+ * @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
482
+ *
483
+ * @param base64 - The Base64 encoded string to convert.
484
+ * @returns The Base64url encoded string.
485
+ */
486
+ function base64UrlEncode(base64) {
487
+ return btoa(String.fromCharCode(...base64)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
488
+ }
489
+ /**
490
+ * Converts a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string to a Base64 encoded string.
491
+ *
492
+ * @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
493
+ *
494
+ * @param base64url - The Base64url encoded string to convert.
495
+ * @returns The Base64 encoded string.
496
+ */
497
+ function base64UrlDecode(base64url) {
498
+ const base64 = base64url.replace(/-/g, "+").replace(/_/g, "/");
499
+ return new Uint8Array([...atob(base64 + "=".repeat((4 - base64.length % 4) % 4))].map((c) => c.charCodeAt(0)));
500
+ }
501
+
502
+ //#endregion
503
+ //#region src/encryption.node.ts
504
+ const CIPHER_ALGORITHM = "chacha20-poly1305";
505
+ const CIPHER_KEY_LENGTH = 32;
506
+ const CIPHER_IV_LENGTH = 16;
507
+ const CIPHER_TAG_LENGTH = 16;
508
+ const CIPHER_SALT_LENGTH = 64;
509
+ const PBKDF2_ITERATIONS = 1e5;
510
+ /**
511
+ * Creates and returns a new key object containing a secret key for symmetric encryption or \`Hmac\`.
512
+ *
513
+ * @param key - The key to use. If \`key\` is a \`Buffer\`, \`TypedArray\`, or \`DataView\`, the \`encoding\` argument is ignored.
514
+ * @param encoding - The \`encoding\` of the \`key\` string. Must be one of \`'utf8'\`, \`'utf16le'\`, \`'latin1'\`, or \`'base64'\`. Default is \`'utf8'\`.
515
+ * @returns The new \`KeyObject\`.
516
+ */
517
+ function createKey(key, encoding) {
518
+ return typeof key === "string" ? createSecretKey(key, encoding) : createSecretKey(key);
519
+ }
520
+ /**
521
+ * Symmetrically encrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
522
+ *
523
+ * @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
524
+ *
525
+ * @param secret - The secret key used for encryption.
526
+ * @param plaintext - The data to encrypt.
527
+ * @returns The encrypted data.
528
+ */
529
+ function encrypt(secret, plaintext) {
530
+ const key = pbkdf2Sync(secret, randomBytes(CIPHER_SALT_LENGTH), PBKDF2_ITERATIONS, CIPHER_KEY_LENGTH, "sha512");
531
+ const iv = randomBytes(CIPHER_IV_LENGTH);
532
+ const salt = randomBytes(CIPHER_SALT_LENGTH);
533
+ const cipher = createCipheriv(CIPHER_ALGORITHM, key, randomBytes(CIPHER_IV_LENGTH));
534
+ const encrypted = Buffer$1.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
535
+ const tag = cipher.getAuthTag();
536
+ return Buffer$1.concat([
537
+ salt,
538
+ iv,
539
+ tag,
540
+ encrypted
541
+ ]).toString("hex");
542
+ }
543
+ /**
544
+ * Symmetrically decrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
545
+ *
546
+ * @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
547
+ *
548
+ * @param secret - The secret key used for decryption.
549
+ * @param encrypted - The encrypted data to decrypt.
550
+ * @returns The decrypted data.
551
+ */
552
+ function decrypt(secret, encrypted) {
553
+ const buffer = Buffer$1.from(encrypted, "hex");
554
+ const decipher = createDecipheriv(CIPHER_ALGORITHM, pbkdf2Sync(secret, buffer.slice(0, CIPHER_SALT_LENGTH), PBKDF2_ITERATIONS, CIPHER_KEY_LENGTH, "sha512"), buffer.slice(CIPHER_SALT_LENGTH, CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH));
555
+ decipher.setAuthTag(buffer.slice(CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH, CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH + CIPHER_TAG_LENGTH));
556
+ return decipher.update(buffer.slice(CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH + CIPHER_TAG_LENGTH)) + decipher.final("utf8");
557
+ }
558
+
559
+ //#endregion
560
+ //#region src/hex.ts
561
+ const ALPHABET = "0123456789ABCDEF";
562
+ const DECODE_MAP = {
563
+ "0": 0,
564
+ "1": 1,
565
+ "2": 2,
566
+ "3": 3,
567
+ "4": 4,
568
+ "5": 5,
569
+ "6": 6,
570
+ "7": 7,
571
+ "8": 8,
572
+ "9": 9,
573
+ a: 10,
574
+ A: 10,
575
+ b: 11,
576
+ B: 11,
577
+ c: 12,
578
+ C: 12,
579
+ d: 13,
580
+ D: 13,
581
+ e: 14,
582
+ E: 14,
583
+ f: 15,
584
+ F: 15
585
+ };
586
+ /**
587
+ * Encodes a Uint8Array into a hexadecimal string.
588
+ *
589
+ * @param input - The input Uint8Array.
590
+ * @returns The hexadecimal string.
591
+ */
592
+ function encodeHex(input) {
593
+ let result = "";
594
+ for (let i = 0; i < input.length; i++) {
595
+ result += ALPHABET[input[i] >> 4];
596
+ result += ALPHABET[input[i] & 15];
597
+ }
598
+ return result;
599
+ }
600
+ /**
601
+ * Encodes a Uint8Array into an uppercase hexadecimal string.
602
+ *
603
+ * @param input - The input Uint8Array.
604
+ * @returns The uppercase hexadecimal string.
605
+ */
606
+ function decodeHex(input) {
607
+ if (input.length % 2 !== 0) throw new Error("Invalid hex string");
608
+ const result = new Uint8Array(input.length / 2);
609
+ for (let i = 0; i < input.length; i += 2) {
610
+ if (!(input[i] in DECODE_MAP)) throw new Error("Invalid character");
611
+ if (!(input[i + 1] in DECODE_MAP)) throw new Error("Invalid character");
612
+ result[i / 2] |= DECODE_MAP[input[i]] << 4;
613
+ result[i / 2] |= DECODE_MAP[input[i + 1]];
614
+ }
615
+ return result;
616
+ }
617
+
618
+ //#endregion
619
+ //#region src/random.ts
620
+ /**
621
+ * Generate a random byte array of the specified length using the Web Crypto API.
622
+ *
623
+ * @param length - The length of the random byte array to generate (default is 32 bytes)
624
+ * @returns A Uint8Array containing random bytes of the specified length
625
+ */
626
+ function generateRandomBytes(length = 32) {
627
+ return crypto.getRandomValues(new Uint8Array(length));
628
+ }
629
+ /**
630
+ * Generate a random string of the specified length using characters A-Z, a-z, and 0-9 for CSRF tokens, etc.
631
+ *
632
+ * @remarks
633
+ * This function uses the Web Crypto API's `crypto.getRandomValues` to generate secure random bytes,
634
+ * and then maps those bytes to characters in the specified character set. It uses rejection sampling
635
+ * to ensure a uniform distribution of characters without modulo bias.
636
+ *
637
+ * @param length - The length of the random string to generate (default is 32 characters)
638
+ * @returns A random string of the specified length
639
+ */
640
+ function generateRandomString(length = 32) {
641
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
642
+ const charsLen = 62;
643
+ const maxValid = 256 - 256 % charsLen;
644
+ const result = [];
645
+ while (result.length < length) {
646
+ const bytes = generateRandomBytes(length - result.length);
647
+ for (const b of bytes) if (b < maxValid && result.length < length && chars[b % charsLen]) result.push(chars[b % charsLen]);
648
+ }
649
+ return result.join("");
650
+ }
651
+
652
+ //#endregion
653
+ export { base64UrlDecode, base64UrlEncode, createKey, decodeBase64, decodeHex, decrypt, encodeBase64, encodeHex, encrypt, generateRandomBytes, generateRandomString };
654
+ //# sourceMappingURL=node.mjs.map