@gjsify/crypto 0.3.16 → 0.3.18

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/lib/esm/cipher.js CHANGED
@@ -1,1295 +1 @@
1
- import { Buffer } from "node:buffer";
2
-
3
- //#region src/cipher.ts
4
- const SBOX = new Uint8Array([
5
- 99,
6
- 124,
7
- 119,
8
- 123,
9
- 242,
10
- 107,
11
- 111,
12
- 197,
13
- 48,
14
- 1,
15
- 103,
16
- 43,
17
- 254,
18
- 215,
19
- 171,
20
- 118,
21
- 202,
22
- 130,
23
- 201,
24
- 125,
25
- 250,
26
- 89,
27
- 71,
28
- 240,
29
- 173,
30
- 212,
31
- 162,
32
- 175,
33
- 156,
34
- 164,
35
- 114,
36
- 192,
37
- 183,
38
- 253,
39
- 147,
40
- 38,
41
- 54,
42
- 63,
43
- 247,
44
- 204,
45
- 52,
46
- 165,
47
- 229,
48
- 241,
49
- 113,
50
- 216,
51
- 49,
52
- 21,
53
- 4,
54
- 199,
55
- 35,
56
- 195,
57
- 24,
58
- 150,
59
- 5,
60
- 154,
61
- 7,
62
- 18,
63
- 128,
64
- 226,
65
- 235,
66
- 39,
67
- 178,
68
- 117,
69
- 9,
70
- 131,
71
- 44,
72
- 26,
73
- 27,
74
- 110,
75
- 90,
76
- 160,
77
- 82,
78
- 59,
79
- 214,
80
- 179,
81
- 41,
82
- 227,
83
- 47,
84
- 132,
85
- 83,
86
- 209,
87
- 0,
88
- 237,
89
- 32,
90
- 252,
91
- 177,
92
- 91,
93
- 106,
94
- 203,
95
- 190,
96
- 57,
97
- 74,
98
- 76,
99
- 88,
100
- 207,
101
- 208,
102
- 239,
103
- 170,
104
- 251,
105
- 67,
106
- 77,
107
- 51,
108
- 133,
109
- 69,
110
- 249,
111
- 2,
112
- 127,
113
- 80,
114
- 60,
115
- 159,
116
- 168,
117
- 81,
118
- 163,
119
- 64,
120
- 143,
121
- 146,
122
- 157,
123
- 56,
124
- 245,
125
- 188,
126
- 182,
127
- 218,
128
- 33,
129
- 16,
130
- 255,
131
- 243,
132
- 210,
133
- 205,
134
- 12,
135
- 19,
136
- 236,
137
- 95,
138
- 151,
139
- 68,
140
- 23,
141
- 196,
142
- 167,
143
- 126,
144
- 61,
145
- 100,
146
- 93,
147
- 25,
148
- 115,
149
- 96,
150
- 129,
151
- 79,
152
- 220,
153
- 34,
154
- 42,
155
- 144,
156
- 136,
157
- 70,
158
- 238,
159
- 184,
160
- 20,
161
- 222,
162
- 94,
163
- 11,
164
- 219,
165
- 224,
166
- 50,
167
- 58,
168
- 10,
169
- 73,
170
- 6,
171
- 36,
172
- 92,
173
- 194,
174
- 211,
175
- 172,
176
- 98,
177
- 145,
178
- 149,
179
- 228,
180
- 121,
181
- 231,
182
- 200,
183
- 55,
184
- 109,
185
- 141,
186
- 213,
187
- 78,
188
- 169,
189
- 108,
190
- 86,
191
- 244,
192
- 234,
193
- 101,
194
- 122,
195
- 174,
196
- 8,
197
- 186,
198
- 120,
199
- 37,
200
- 46,
201
- 28,
202
- 166,
203
- 180,
204
- 198,
205
- 232,
206
- 221,
207
- 116,
208
- 31,
209
- 75,
210
- 189,
211
- 139,
212
- 138,
213
- 112,
214
- 62,
215
- 181,
216
- 102,
217
- 72,
218
- 3,
219
- 246,
220
- 14,
221
- 97,
222
- 53,
223
- 87,
224
- 185,
225
- 134,
226
- 193,
227
- 29,
228
- 158,
229
- 225,
230
- 248,
231
- 152,
232
- 17,
233
- 105,
234
- 217,
235
- 142,
236
- 148,
237
- 155,
238
- 30,
239
- 135,
240
- 233,
241
- 206,
242
- 85,
243
- 40,
244
- 223,
245
- 140,
246
- 161,
247
- 137,
248
- 13,
249
- 191,
250
- 230,
251
- 66,
252
- 104,
253
- 65,
254
- 153,
255
- 45,
256
- 15,
257
- 176,
258
- 84,
259
- 187,
260
- 22
261
- ]);
262
- const INV_SBOX = new Uint8Array([
263
- 82,
264
- 9,
265
- 106,
266
- 213,
267
- 48,
268
- 54,
269
- 165,
270
- 56,
271
- 191,
272
- 64,
273
- 163,
274
- 158,
275
- 129,
276
- 243,
277
- 215,
278
- 251,
279
- 124,
280
- 227,
281
- 57,
282
- 130,
283
- 155,
284
- 47,
285
- 255,
286
- 135,
287
- 52,
288
- 142,
289
- 67,
290
- 68,
291
- 196,
292
- 222,
293
- 233,
294
- 203,
295
- 84,
296
- 123,
297
- 148,
298
- 50,
299
- 166,
300
- 194,
301
- 35,
302
- 61,
303
- 238,
304
- 76,
305
- 149,
306
- 11,
307
- 66,
308
- 250,
309
- 195,
310
- 78,
311
- 8,
312
- 46,
313
- 161,
314
- 102,
315
- 40,
316
- 217,
317
- 36,
318
- 178,
319
- 118,
320
- 91,
321
- 162,
322
- 73,
323
- 109,
324
- 139,
325
- 209,
326
- 37,
327
- 114,
328
- 248,
329
- 246,
330
- 100,
331
- 134,
332
- 104,
333
- 152,
334
- 22,
335
- 212,
336
- 164,
337
- 92,
338
- 204,
339
- 93,
340
- 101,
341
- 182,
342
- 146,
343
- 108,
344
- 112,
345
- 72,
346
- 80,
347
- 253,
348
- 237,
349
- 185,
350
- 218,
351
- 94,
352
- 21,
353
- 70,
354
- 87,
355
- 167,
356
- 141,
357
- 157,
358
- 132,
359
- 144,
360
- 216,
361
- 171,
362
- 0,
363
- 140,
364
- 188,
365
- 211,
366
- 10,
367
- 247,
368
- 228,
369
- 88,
370
- 5,
371
- 184,
372
- 179,
373
- 69,
374
- 6,
375
- 208,
376
- 44,
377
- 30,
378
- 143,
379
- 202,
380
- 63,
381
- 15,
382
- 2,
383
- 193,
384
- 175,
385
- 189,
386
- 3,
387
- 1,
388
- 19,
389
- 138,
390
- 107,
391
- 58,
392
- 145,
393
- 17,
394
- 65,
395
- 79,
396
- 103,
397
- 220,
398
- 234,
399
- 151,
400
- 242,
401
- 207,
402
- 206,
403
- 240,
404
- 180,
405
- 230,
406
- 115,
407
- 150,
408
- 172,
409
- 116,
410
- 34,
411
- 231,
412
- 173,
413
- 53,
414
- 133,
415
- 226,
416
- 249,
417
- 55,
418
- 232,
419
- 28,
420
- 117,
421
- 223,
422
- 110,
423
- 71,
424
- 241,
425
- 26,
426
- 113,
427
- 29,
428
- 41,
429
- 197,
430
- 137,
431
- 111,
432
- 183,
433
- 98,
434
- 14,
435
- 170,
436
- 24,
437
- 190,
438
- 27,
439
- 252,
440
- 86,
441
- 62,
442
- 75,
443
- 198,
444
- 210,
445
- 121,
446
- 32,
447
- 154,
448
- 219,
449
- 192,
450
- 254,
451
- 120,
452
- 205,
453
- 90,
454
- 244,
455
- 31,
456
- 221,
457
- 168,
458
- 51,
459
- 136,
460
- 7,
461
- 199,
462
- 49,
463
- 177,
464
- 18,
465
- 16,
466
- 89,
467
- 39,
468
- 128,
469
- 236,
470
- 95,
471
- 96,
472
- 81,
473
- 127,
474
- 169,
475
- 25,
476
- 181,
477
- 74,
478
- 13,
479
- 45,
480
- 229,
481
- 122,
482
- 159,
483
- 147,
484
- 201,
485
- 156,
486
- 239,
487
- 160,
488
- 224,
489
- 59,
490
- 77,
491
- 174,
492
- 42,
493
- 245,
494
- 176,
495
- 200,
496
- 235,
497
- 187,
498
- 60,
499
- 131,
500
- 83,
501
- 153,
502
- 97,
503
- 23,
504
- 43,
505
- 4,
506
- 126,
507
- 186,
508
- 119,
509
- 214,
510
- 38,
511
- 225,
512
- 105,
513
- 20,
514
- 99,
515
- 85,
516
- 33,
517
- 12,
518
- 125
519
- ]);
520
- const RCON = [
521
- 1,
522
- 2,
523
- 4,
524
- 8,
525
- 16,
526
- 32,
527
- 64,
528
- 128,
529
- 27,
530
- 54
531
- ];
532
- function gmul(a, b) {
533
- let p = 0;
534
- for (let i = 0; i < 8; i++) {
535
- if (b & 1) p ^= a;
536
- const hi = a & 128;
537
- a = a << 1 & 255;
538
- if (hi) a ^= 27;
539
- b >>= 1;
540
- }
541
- return p;
542
- }
543
- function keyExpansion(key) {
544
- const nk = key.length / 4;
545
- const nr = nk + 6;
546
- const nw = 4 * (nr + 1);
547
- const w = new Array(nw);
548
- for (let i = 0; i < nk; i++) {
549
- w[i] = new Uint8Array([
550
- key[4 * i],
551
- key[4 * i + 1],
552
- key[4 * i + 2],
553
- key[4 * i + 3]
554
- ]);
555
- }
556
- for (let i = nk; i < nw; i++) {
557
- let temp = new Uint8Array(w[i - 1]);
558
- if (i % nk === 0) {
559
- temp = new Uint8Array([
560
- SBOX[temp[1]] ^ RCON[i / nk - 1],
561
- SBOX[temp[2]],
562
- SBOX[temp[3]],
563
- SBOX[temp[0]]
564
- ]);
565
- } else if (nk > 6 && i % nk === 4) {
566
- temp = new Uint8Array([
567
- SBOX[temp[0]],
568
- SBOX[temp[1]],
569
- SBOX[temp[2]],
570
- SBOX[temp[3]]
571
- ]);
572
- }
573
- w[i] = new Uint8Array(4);
574
- for (let j = 0; j < 4; j++) w[i][j] = w[i - nk][j] ^ temp[j];
575
- }
576
- const roundKeys = [];
577
- for (let r = 0; r <= nr; r++) {
578
- const rk = new Uint8Array(16);
579
- for (let c = 0; c < 4; c++) {
580
- rk[4 * c] = w[4 * r + c][0];
581
- rk[4 * c + 1] = w[4 * r + c][1];
582
- rk[4 * c + 2] = w[4 * r + c][2];
583
- rk[4 * c + 3] = w[4 * r + c][3];
584
- }
585
- roundKeys.push(rk);
586
- }
587
- return roundKeys;
588
- }
589
- function aesEncryptBlock(block, roundKeys) {
590
- const state = new Uint8Array(block);
591
- const nr = roundKeys.length - 1;
592
- for (let i = 0; i < 16; i++) state[i] ^= roundKeys[0][i];
593
- for (let round = 1; round < nr; round++) {
594
- for (let i = 0; i < 16; i++) state[i] = SBOX[state[i]];
595
- const t1 = state[1];
596
- state[1] = state[5];
597
- state[5] = state[9];
598
- state[9] = state[13];
599
- state[13] = t1;
600
- const t2a = state[2];
601
- const t2b = state[6];
602
- state[2] = state[10];
603
- state[6] = state[14];
604
- state[10] = t2a;
605
- state[14] = t2b;
606
- const t3 = state[15];
607
- state[15] = state[11];
608
- state[11] = state[7];
609
- state[7] = state[3];
610
- state[3] = t3;
611
- for (let c = 0; c < 4; c++) {
612
- const i = c * 4;
613
- const a0 = state[i], a1 = state[i + 1], a2 = state[i + 2], a3 = state[i + 3];
614
- state[i] = gmul(2, a0) ^ gmul(3, a1) ^ a2 ^ a3;
615
- state[i + 1] = a0 ^ gmul(2, a1) ^ gmul(3, a2) ^ a3;
616
- state[i + 2] = a0 ^ a1 ^ gmul(2, a2) ^ gmul(3, a3);
617
- state[i + 3] = gmul(3, a0) ^ a1 ^ a2 ^ gmul(2, a3);
618
- }
619
- for (let i = 0; i < 16; i++) state[i] ^= roundKeys[round][i];
620
- }
621
- for (let i = 0; i < 16; i++) state[i] = SBOX[state[i]];
622
- const t1f = state[1];
623
- state[1] = state[5];
624
- state[5] = state[9];
625
- state[9] = state[13];
626
- state[13] = t1f;
627
- const t2af = state[2];
628
- const t2bf = state[6];
629
- state[2] = state[10];
630
- state[6] = state[14];
631
- state[10] = t2af;
632
- state[14] = t2bf;
633
- const t3f = state[15];
634
- state[15] = state[11];
635
- state[11] = state[7];
636
- state[7] = state[3];
637
- state[3] = t3f;
638
- for (let i = 0; i < 16; i++) state[i] ^= roundKeys[nr][i];
639
- return state;
640
- }
641
- function aesDecryptBlock(block, roundKeys) {
642
- const state = new Uint8Array(block);
643
- const nr = roundKeys.length - 1;
644
- for (let i = 0; i < 16; i++) state[i] ^= roundKeys[nr][i];
645
- for (let round = nr - 1; round > 0; round--) {
646
- const t1 = state[13];
647
- state[13] = state[9];
648
- state[9] = state[5];
649
- state[5] = state[1];
650
- state[1] = t1;
651
- const t2a = state[10];
652
- const t2b = state[14];
653
- state[10] = state[2];
654
- state[14] = state[6];
655
- state[2] = t2a;
656
- state[6] = t2b;
657
- const t3 = state[3];
658
- state[3] = state[7];
659
- state[7] = state[11];
660
- state[11] = state[15];
661
- state[15] = t3;
662
- for (let i = 0; i < 16; i++) state[i] = INV_SBOX[state[i]];
663
- for (let i = 0; i < 16; i++) state[i] ^= roundKeys[round][i];
664
- for (let c = 0; c < 4; c++) {
665
- const i = c * 4;
666
- const a0 = state[i], a1 = state[i + 1], a2 = state[i + 2], a3 = state[i + 3];
667
- state[i] = gmul(14, a0) ^ gmul(11, a1) ^ gmul(13, a2) ^ gmul(9, a3);
668
- state[i + 1] = gmul(9, a0) ^ gmul(14, a1) ^ gmul(11, a2) ^ gmul(13, a3);
669
- state[i + 2] = gmul(13, a0) ^ gmul(9, a1) ^ gmul(14, a2) ^ gmul(11, a3);
670
- state[i + 3] = gmul(11, a0) ^ gmul(13, a1) ^ gmul(9, a2) ^ gmul(14, a3);
671
- }
672
- }
673
- const t1f = state[13];
674
- state[13] = state[9];
675
- state[9] = state[5];
676
- state[5] = state[1];
677
- state[1] = t1f;
678
- const t2af = state[10];
679
- const t2bf = state[14];
680
- state[10] = state[2];
681
- state[14] = state[6];
682
- state[2] = t2af;
683
- state[6] = t2bf;
684
- const t3f = state[3];
685
- state[3] = state[7];
686
- state[7] = state[11];
687
- state[11] = state[15];
688
- state[15] = t3f;
689
- for (let i = 0; i < 16; i++) state[i] = INV_SBOX[state[i]];
690
- for (let i = 0; i < 16; i++) state[i] ^= roundKeys[0][i];
691
- return state;
692
- }
693
- function incrementCounter(counter) {
694
- for (let i = 15; i >= 0; i--) {
695
- if (++counter[i] !== 0) break;
696
- }
697
- }
698
- function gcmIncrementCounter(counter) {
699
- for (let i = 15; i >= 12; i--) {
700
- if (++counter[i] !== 0) break;
701
- }
702
- }
703
- /**
704
- * Multiply two 128-bit values in GF(2^128) using the irreducible polynomial
705
- * x^128 + x^7 + x^2 + x + 1 (represented as R = 0xe1 << 120).
706
- *
707
- * X and Y are 16-byte Uint8Arrays (big-endian bit ordering).
708
- * Returns a new 16-byte Uint8Array.
709
- */
710
- function gfMul(X, Y) {
711
- const Z = new Uint8Array(16);
712
- const V = new Uint8Array(X);
713
- for (let i = 0; i < 128; i++) {
714
- if (Y[i >>> 3] & 1 << 7 - (i & 7)) {
715
- for (let j = 0; j < 16; j++) Z[j] ^= V[j];
716
- }
717
- const lsb = V[15] & 1;
718
- for (let j = 15; j > 0; j--) {
719
- V[j] = V[j] >>> 1 | (V[j - 1] & 1) << 7;
720
- }
721
- V[0] = V[0] >>> 1;
722
- if (lsb) {
723
- V[0] ^= 225;
724
- }
725
- }
726
- return Z;
727
- }
728
- /**
729
- * GHASH function per NIST SP 800-38D.
730
- *
731
- * H: the hash subkey (AES_K(0^128)), 16 bytes
732
- * aad: additional authenticated data (arbitrary length)
733
- * ciphertext: ciphertext (arbitrary length)
734
- *
735
- * Returns a 16-byte authentication hash.
736
- */
737
- function ghash(H, aad, ciphertext) {
738
- const X = new Uint8Array(16);
739
- const aadBlocks = Math.ceil(aad.length / 16) || 0;
740
- for (let i = 0; i < aadBlocks; i++) {
741
- const start = i * 16;
742
- const end = Math.min(start + 16, aad.length);
743
- for (let j = 0; j < 16; j++) {
744
- const idx = start + j;
745
- if (idx < end) {
746
- X[j] ^= aad[idx];
747
- }
748
- }
749
- const product = gfMul(X, H);
750
- X.set(product);
751
- }
752
- const ctBlocks = Math.ceil(ciphertext.length / 16) || 0;
753
- for (let i = 0; i < ctBlocks; i++) {
754
- const start = i * 16;
755
- const end = Math.min(start + 16, ciphertext.length);
756
- for (let j = 0; j < 16; j++) {
757
- const idx = start + j;
758
- if (idx < end) {
759
- X[j] ^= ciphertext[idx];
760
- }
761
- }
762
- const product = gfMul(X, H);
763
- X.set(product);
764
- }
765
- const lenBlock = new Uint8Array(16);
766
- const aadBits = aad.length * 8;
767
- const ctBits = ciphertext.length * 8;
768
- const aadHi = Math.floor(aadBits / 4294967296);
769
- const aadLo = aadBits >>> 0;
770
- lenBlock[0] = aadHi >>> 24 & 255;
771
- lenBlock[1] = aadHi >>> 16 & 255;
772
- lenBlock[2] = aadHi >>> 8 & 255;
773
- lenBlock[3] = aadHi & 255;
774
- lenBlock[4] = aadLo >>> 24 & 255;
775
- lenBlock[5] = aadLo >>> 16 & 255;
776
- lenBlock[6] = aadLo >>> 8 & 255;
777
- lenBlock[7] = aadLo & 255;
778
- const ctHi = Math.floor(ctBits / 4294967296);
779
- const ctLo = ctBits >>> 0;
780
- lenBlock[8] = ctHi >>> 24 & 255;
781
- lenBlock[9] = ctHi >>> 16 & 255;
782
- lenBlock[10] = ctHi >>> 8 & 255;
783
- lenBlock[11] = ctHi & 255;
784
- lenBlock[12] = ctLo >>> 24 & 255;
785
- lenBlock[13] = ctLo >>> 16 & 255;
786
- lenBlock[14] = ctLo >>> 8 & 255;
787
- lenBlock[15] = ctLo & 255;
788
- for (let j = 0; j < 16; j++) X[j] ^= lenBlock[j];
789
- const product = gfMul(X, H);
790
- X.set(product);
791
- return X;
792
- }
793
- function parseAlgorithm(algorithm) {
794
- const lower = algorithm.toLowerCase();
795
- const match = lower.match(/^aes-(128|192|256)-(cbc|ctr|ecb|cfb|ofb|gcm)$/);
796
- if (!match) {
797
- throw new Error(`Unsupported cipher algorithm: ${algorithm}`);
798
- }
799
- const keyBits = parseInt(match[1]);
800
- const mode = match[2];
801
- return {
802
- keySize: keyBits / 8,
803
- ivSize: mode === "ecb" ? 0 : mode === "gcm" ? 12 : 16,
804
- mode
805
- };
806
- }
807
- function toBuffer(data, encoding) {
808
- if (typeof data === "string") {
809
- return Buffer.from(data, encoding || "utf8");
810
- }
811
- return Buffer.from(data);
812
- }
813
- function encodeOutput(data, encoding) {
814
- if (!encoding) return Buffer.from(data);
815
- return Buffer.from(data).toString(encoding);
816
- }
817
- /**
818
- * Count how many trailing bytes at the end of a Uint8Array form an incomplete
819
- * UTF-8 multibyte sequence. Returns 0 if the last character is complete.
820
- */
821
- function incompleteUtf8Tail(buf) {
822
- if (buf.length === 0) return 0;
823
- const end = buf.length;
824
- for (let back = 1; back <= Math.min(4, end); back++) {
825
- const b = buf[end - back];
826
- if ((b & 128) === 0) {
827
- return 0;
828
- }
829
- if ((b & 192) === 128) {
830
- continue;
831
- }
832
- let expected;
833
- if ((b & 224) === 192) expected = 2;
834
- else if ((b & 240) === 224) expected = 3;
835
- else if ((b & 248) === 240) expected = 4;
836
- else return 0;
837
- return back < expected ? back : 0;
838
- }
839
- return 0;
840
- }
841
- function pkcs7Pad(data) {
842
- const padLen = 16 - data.length % 16;
843
- const padded = new Uint8Array(data.length + padLen);
844
- padded.set(data);
845
- for (let i = data.length; i < padded.length; i++) padded[i] = padLen;
846
- return padded;
847
- }
848
- function pkcs7Unpad(data) {
849
- if (data.length === 0 || data.length % 16 !== 0) {
850
- throw new Error("bad decrypt");
851
- }
852
- const padLen = data[data.length - 1];
853
- if (padLen === 0 || padLen > 16) {
854
- throw new Error("bad decrypt");
855
- }
856
- for (let i = data.length - padLen; i < data.length; i++) {
857
- if (data[i] !== padLen) throw new Error("bad decrypt");
858
- }
859
- return new Uint8Array(data.slice(0, data.length - padLen));
860
- }
861
- var CipherBase = class {
862
- _roundKeys;
863
- _iv;
864
- _mode;
865
- _buffer = new Uint8Array(0);
866
- _autoPadding = true;
867
- _finalized = false;
868
- constructor(algorithm, key, iv) {
869
- const info = parseAlgorithm(algorithm);
870
- if (key.length !== info.keySize) {
871
- throw new Error(`Invalid key length ${key.length}, expected ${info.keySize} for ${algorithm}`);
872
- }
873
- if (info.ivSize > 0 && (!iv || iv.length !== info.ivSize)) {
874
- throw new Error(`Invalid IV length ${iv?.length ?? 0}, expected ${info.ivSize} for ${algorithm}`);
875
- }
876
- this._roundKeys = keyExpansion(key);
877
- this._iv = iv ? new Uint8Array(iv) : new Uint8Array(16);
878
- this._mode = info.mode;
879
- }
880
- setAutoPadding(autoPadding) {
881
- this._autoPadding = autoPadding;
882
- return this;
883
- }
884
- };
885
- var Cipher = class extends CipherBase {
886
- _prevBlock;
887
- _counter;
888
- _gcmH = null;
889
- _gcmJ0 = null;
890
- _gcmAAD = new Uint8Array(0);
891
- _gcmCiphertext = [];
892
- _gcmCiphertextLen = 0;
893
- _gcmAuthTag = null;
894
- _gcmAADSet = false;
895
- constructor(algorithm, key, iv) {
896
- super(algorithm, key, iv);
897
- this._prevBlock = new Uint8Array(this._iv);
898
- if (this._mode === "gcm") {
899
- this._gcmH = aesEncryptBlock(new Uint8Array(16), this._roundKeys);
900
- this._gcmJ0 = new Uint8Array(16);
901
- this._gcmJ0.set(this._iv.subarray(0, 12));
902
- this._gcmJ0[15] = 1;
903
- this._counter = new Uint8Array(this._gcmJ0);
904
- gcmIncrementCounter(this._counter);
905
- } else {
906
- this._counter = new Uint8Array(this._iv);
907
- }
908
- }
909
- /**
910
- * Set Additional Authenticated Data for GCM mode.
911
- * Must be called before any update() calls.
912
- */
913
- setAAD(data) {
914
- if (this._mode !== "gcm") {
915
- throw new Error("setAAD is only supported in GCM mode");
916
- }
917
- if (this._gcmCiphertextLen > 0) {
918
- throw new Error("setAAD must be called before update()");
919
- }
920
- this._gcmAAD = new Uint8Array(data);
921
- this._gcmAADSet = true;
922
- return this;
923
- }
924
- /**
925
- * Get the authentication tag after final() has been called.
926
- * Only valid for GCM mode.
927
- */
928
- getAuthTag() {
929
- if (this._mode !== "gcm") {
930
- throw new Error("getAuthTag is only supported in GCM mode");
931
- }
932
- if (!this._gcmAuthTag) {
933
- throw new Error("getAuthTag must be called after final()");
934
- }
935
- return Buffer.from(this._gcmAuthTag);
936
- }
937
- update(data, inputEncoding, outputEncoding) {
938
- const input = toBuffer(data, inputEncoding);
939
- const combined = new Uint8Array(this._buffer.length + input.length);
940
- combined.set(this._buffer);
941
- combined.set(input, this._buffer.length);
942
- if (this._mode === "gcm") {
943
- const output = this._processGcmEncrypt(combined);
944
- this._buffer = new Uint8Array(0);
945
- return encodeOutput(output, outputEncoding);
946
- }
947
- if (this._mode === "ctr" || this._mode === "cfb" || this._mode === "ofb") {
948
- const output = this._processStream(combined);
949
- this._buffer = new Uint8Array(0);
950
- return encodeOutput(output, outputEncoding);
951
- }
952
- const fullBlocks = Math.floor(combined.length / 16);
953
- const processLen = fullBlocks * 16;
954
- const output = [];
955
- for (let i = 0; i < processLen; i += 16) {
956
- const block = combined.slice(i, i + 16);
957
- output.push(this._encryptBlock(block));
958
- }
959
- this._buffer = combined.slice(processLen);
960
- const result = new Uint8Array(output.length * 16);
961
- for (let i = 0; i < output.length; i++) result.set(output[i], i * 16);
962
- return encodeOutput(result, outputEncoding);
963
- }
964
- final(outputEncoding) {
965
- if (this._finalized) throw new Error("Cipher already finalized");
966
- this._finalized = true;
967
- if (this._mode === "gcm") {
968
- let finalOutput = new Uint8Array(0);
969
- if (this._buffer.length > 0) {
970
- finalOutput = this._processGcmEncrypt(this._buffer);
971
- this._buffer = new Uint8Array(0);
972
- }
973
- const allCiphertext = new Uint8Array(this._gcmCiphertextLen);
974
- let offset = 0;
975
- for (const chunk of this._gcmCiphertext) {
976
- allCiphertext.set(chunk, offset);
977
- offset += chunk.length;
978
- }
979
- const ghashResult = ghash(this._gcmH, this._gcmAAD, allCiphertext);
980
- const encJ0 = aesEncryptBlock(this._gcmJ0, this._roundKeys);
981
- const tag = new Uint8Array(16);
982
- for (let i = 0; i < 16; i++) tag[i] = ghashResult[i] ^ encJ0[i];
983
- this._gcmAuthTag = Buffer.from(tag);
984
- return encodeOutput(finalOutput, outputEncoding);
985
- }
986
- if (this._mode === "ctr" || this._mode === "cfb" || this._mode === "ofb") {
987
- if (this._buffer.length > 0) {
988
- const output = this._processStream(this._buffer);
989
- this._buffer = new Uint8Array(0);
990
- return encodeOutput(output, outputEncoding);
991
- }
992
- return encodeOutput(new Uint8Array(0), outputEncoding);
993
- }
994
- let data = this._buffer;
995
- if (this._autoPadding) {
996
- data = pkcs7Pad(data);
997
- } else if (data.length % 16 !== 0) {
998
- throw new Error("data not multiple of block size");
999
- }
1000
- const output = [];
1001
- for (let i = 0; i < data.length; i += 16) {
1002
- output.push(this._encryptBlock(data.slice(i, i + 16)));
1003
- }
1004
- this._buffer = new Uint8Array(0);
1005
- if (output.length === 0) return encodeOutput(new Uint8Array(0), outputEncoding);
1006
- const result = new Uint8Array(output.length * 16);
1007
- for (let i = 0; i < output.length; i++) result.set(output[i], i * 16);
1008
- return encodeOutput(result, outputEncoding);
1009
- }
1010
- _encryptBlock(block) {
1011
- if (this._mode === "cbc") {
1012
- const xored = new Uint8Array(16);
1013
- for (let i = 0; i < 16; i++) xored[i] = block[i] ^ this._prevBlock[i];
1014
- const encrypted = aesEncryptBlock(xored, this._roundKeys);
1015
- this._prevBlock = encrypted;
1016
- return encrypted;
1017
- } else if (this._mode === "ecb") {
1018
- return aesEncryptBlock(block, this._roundKeys);
1019
- }
1020
- throw new Error(`Block encryption not supported for mode: ${this._mode}`);
1021
- }
1022
- _processStream(data) {
1023
- const output = new Uint8Array(data.length);
1024
- for (let i = 0; i < data.length; i += 16) {
1025
- const keystream = aesEncryptBlock(this._counter, this._roundKeys);
1026
- const remaining = Math.min(16, data.length - i);
1027
- for (let j = 0; j < remaining; j++) {
1028
- output[i + j] = data[i + j] ^ keystream[j];
1029
- }
1030
- incrementCounter(this._counter);
1031
- }
1032
- return output;
1033
- }
1034
- /**
1035
- * GCM encryption: CTR mode encryption, also accumulates ciphertext for GHASH.
1036
- */
1037
- _processGcmEncrypt(data) {
1038
- const output = new Uint8Array(data.length);
1039
- for (let i = 0; i < data.length; i += 16) {
1040
- const keystream = aesEncryptBlock(this._counter, this._roundKeys);
1041
- const remaining = Math.min(16, data.length - i);
1042
- for (let j = 0; j < remaining; j++) {
1043
- output[i + j] = data[i + j] ^ keystream[j];
1044
- }
1045
- gcmIncrementCounter(this._counter);
1046
- }
1047
- this._gcmCiphertext.push(new Uint8Array(output));
1048
- this._gcmCiphertextLen += output.length;
1049
- return output;
1050
- }
1051
- };
1052
- var Decipher = class extends CipherBase {
1053
- _prevBlock;
1054
- _counter;
1055
- _pendingUtf8 = new Uint8Array(0);
1056
- _gcmH = null;
1057
- _gcmJ0 = null;
1058
- _gcmAAD = new Uint8Array(0);
1059
- _gcmCiphertext = [];
1060
- _gcmCiphertextLen = 0;
1061
- _gcmExpectedTag = null;
1062
- _gcmAADSet = false;
1063
- constructor(algorithm, key, iv) {
1064
- super(algorithm, key, iv);
1065
- this._prevBlock = new Uint8Array(this._iv);
1066
- if (this._mode === "gcm") {
1067
- this._gcmH = aesEncryptBlock(new Uint8Array(16), this._roundKeys);
1068
- this._gcmJ0 = new Uint8Array(16);
1069
- this._gcmJ0.set(this._iv.subarray(0, 12));
1070
- this._gcmJ0[15] = 1;
1071
- this._counter = new Uint8Array(this._gcmJ0);
1072
- gcmIncrementCounter(this._counter);
1073
- } else {
1074
- this._counter = new Uint8Array(this._iv);
1075
- }
1076
- }
1077
- /**
1078
- * Set Additional Authenticated Data for GCM mode.
1079
- * Must be called before any update() calls.
1080
- */
1081
- setAAD(data) {
1082
- if (this._mode !== "gcm") {
1083
- throw new Error("setAAD is only supported in GCM mode");
1084
- }
1085
- if (this._gcmCiphertextLen > 0) {
1086
- throw new Error("setAAD must be called before update()");
1087
- }
1088
- this._gcmAAD = new Uint8Array(data);
1089
- this._gcmAADSet = true;
1090
- return this;
1091
- }
1092
- /**
1093
- * Set the expected authentication tag for GCM decryption.
1094
- * Must be called before final().
1095
- */
1096
- setAuthTag(tag) {
1097
- if (this._mode !== "gcm") {
1098
- throw new Error("setAuthTag is only supported in GCM mode");
1099
- }
1100
- this._gcmExpectedTag = Buffer.from(tag);
1101
- return this;
1102
- }
1103
- _encodeWithUtf8Handling(bytes, encoding, isFinal) {
1104
- if (!encoding || encoding !== "utf8" && encoding !== "utf-8") {
1105
- return encodeOutput(bytes, encoding);
1106
- }
1107
- let data;
1108
- if (this._pendingUtf8.length > 0) {
1109
- data = new Uint8Array(this._pendingUtf8.length + bytes.length);
1110
- data.set(this._pendingUtf8);
1111
- data.set(bytes, this._pendingUtf8.length);
1112
- this._pendingUtf8 = new Uint8Array(0);
1113
- } else {
1114
- data = bytes;
1115
- }
1116
- if (!isFinal) {
1117
- const tail = incompleteUtf8Tail(data);
1118
- if (tail > 0) {
1119
- this._pendingUtf8 = new Uint8Array(data.slice(data.length - tail));
1120
- data = new Uint8Array(data.slice(0, data.length - tail));
1121
- }
1122
- }
1123
- return Buffer.from(data).toString("utf8");
1124
- }
1125
- update(data, inputEncoding, outputEncoding) {
1126
- const input = toBuffer(data, inputEncoding);
1127
- const combined = new Uint8Array(this._buffer.length + input.length);
1128
- combined.set(this._buffer);
1129
- combined.set(input, this._buffer.length);
1130
- if (this._mode === "gcm") {
1131
- this._gcmCiphertext.push(new Uint8Array(combined));
1132
- this._gcmCiphertextLen += combined.length;
1133
- const output = this._processGcmDecrypt(combined);
1134
- this._buffer = new Uint8Array(0);
1135
- return this._encodeWithUtf8Handling(output, outputEncoding, false);
1136
- }
1137
- if (this._mode === "ctr" || this._mode === "cfb" || this._mode === "ofb") {
1138
- const output = this._processStream(combined);
1139
- this._buffer = new Uint8Array(0);
1140
- return this._encodeWithUtf8Handling(output, outputEncoding, false);
1141
- }
1142
- const fullBlocks = Math.floor(combined.length / 16);
1143
- if (fullBlocks === 0) {
1144
- this._buffer = combined;
1145
- return this._encodeWithUtf8Handling(new Uint8Array(0), outputEncoding, false);
1146
- }
1147
- const processBlocks = this._autoPadding ? fullBlocks - 1 : fullBlocks;
1148
- const processLen = processBlocks * 16;
1149
- const output = [];
1150
- for (let i = 0; i < processLen; i += 16) {
1151
- const block = combined.slice(i, i + 16);
1152
- output.push(this._decryptBlock(block));
1153
- }
1154
- this._buffer = combined.slice(processLen);
1155
- const result = new Uint8Array(output.length * 16);
1156
- for (let i = 0; i < output.length; i++) result.set(output[i], i * 16);
1157
- return this._encodeWithUtf8Handling(result, outputEncoding, false);
1158
- }
1159
- final(outputEncoding) {
1160
- if (this._finalized) throw new Error("Decipher already finalized");
1161
- this._finalized = true;
1162
- if (this._mode === "gcm") {
1163
- let finalOutput = new Uint8Array(0);
1164
- if (this._buffer.length > 0) {
1165
- this._gcmCiphertext.push(new Uint8Array(this._buffer));
1166
- this._gcmCiphertextLen += this._buffer.length;
1167
- finalOutput = this._processGcmDecrypt(this._buffer);
1168
- this._buffer = new Uint8Array(0);
1169
- }
1170
- if (!this._gcmExpectedTag) {
1171
- throw new Error("Unsupported state or unable to authenticate data");
1172
- }
1173
- const allCiphertext = new Uint8Array(this._gcmCiphertextLen);
1174
- let offset = 0;
1175
- for (const chunk of this._gcmCiphertext) {
1176
- allCiphertext.set(chunk, offset);
1177
- offset += chunk.length;
1178
- }
1179
- const ghashResult = ghash(this._gcmH, this._gcmAAD, allCiphertext);
1180
- const encJ0 = aesEncryptBlock(this._gcmJ0, this._roundKeys);
1181
- const computedTag = new Uint8Array(16);
1182
- for (let i = 0; i < 16; i++) computedTag[i] = ghashResult[i] ^ encJ0[i];
1183
- const expectedTag = this._gcmExpectedTag;
1184
- const tagLen = Math.min(expectedTag.length, 16);
1185
- let diff = 0;
1186
- for (let i = 0; i < tagLen; i++) {
1187
- diff |= computedTag[i] ^ expectedTag[i];
1188
- }
1189
- if (diff !== 0) {
1190
- throw new Error("Unsupported state or unable to authenticate data");
1191
- }
1192
- return this._encodeWithUtf8Handling(finalOutput, outputEncoding, true);
1193
- }
1194
- if (this._mode === "ctr" || this._mode === "cfb" || this._mode === "ofb") {
1195
- if (this._buffer.length > 0) {
1196
- const output = this._processStream(this._buffer);
1197
- this._buffer = new Uint8Array(0);
1198
- return this._encodeWithUtf8Handling(output, outputEncoding, true);
1199
- }
1200
- return this._encodeWithUtf8Handling(new Uint8Array(0), outputEncoding, true);
1201
- }
1202
- if (this._buffer.length === 0) {
1203
- return this._encodeWithUtf8Handling(new Uint8Array(0), outputEncoding, true);
1204
- }
1205
- if (this._buffer.length % 16 !== 0) {
1206
- throw new Error("bad decrypt");
1207
- }
1208
- const output = [];
1209
- for (let i = 0; i < this._buffer.length; i += 16) {
1210
- output.push(this._decryptBlock(this._buffer.slice(i, i + 16)));
1211
- }
1212
- const combined = new Uint8Array(output.length * 16);
1213
- for (let i = 0; i < output.length; i++) combined.set(output[i], i * 16);
1214
- const result = this._autoPadding ? pkcs7Unpad(combined) : combined;
1215
- this._buffer = new Uint8Array(0);
1216
- return this._encodeWithUtf8Handling(result, outputEncoding, true);
1217
- }
1218
- _decryptBlock(block) {
1219
- if (this._mode === "cbc") {
1220
- const decrypted = aesDecryptBlock(block, this._roundKeys);
1221
- const output = new Uint8Array(16);
1222
- for (let i = 0; i < 16; i++) output[i] = decrypted[i] ^ this._prevBlock[i];
1223
- this._prevBlock = new Uint8Array(block);
1224
- return output;
1225
- } else if (this._mode === "ecb") {
1226
- return aesDecryptBlock(block, this._roundKeys);
1227
- }
1228
- throw new Error(`Block decryption not supported for mode: ${this._mode}`);
1229
- }
1230
- _processStream(data) {
1231
- const output = new Uint8Array(data.length);
1232
- for (let i = 0; i < data.length; i += 16) {
1233
- const keystream = aesEncryptBlock(this._counter, this._roundKeys);
1234
- const remaining = Math.min(16, data.length - i);
1235
- for (let j = 0; j < remaining; j++) {
1236
- output[i + j] = data[i + j] ^ keystream[j];
1237
- }
1238
- incrementCounter(this._counter);
1239
- }
1240
- return output;
1241
- }
1242
- /**
1243
- * GCM decryption: CTR mode decryption (same as encryption, since CTR is symmetric).
1244
- */
1245
- _processGcmDecrypt(data) {
1246
- const output = new Uint8Array(data.length);
1247
- for (let i = 0; i < data.length; i += 16) {
1248
- const keystream = aesEncryptBlock(this._counter, this._roundKeys);
1249
- const remaining = Math.min(16, data.length - i);
1250
- for (let j = 0; j < remaining; j++) {
1251
- output[i + j] = data[i + j] ^ keystream[j];
1252
- }
1253
- gcmIncrementCounter(this._counter);
1254
- }
1255
- return output;
1256
- }
1257
- };
1258
- function createCipher(_algorithm, _password) {
1259
- throw new Error("crypto.createCipher() is deprecated. Use createCipheriv() instead.");
1260
- }
1261
- function createCipheriv(algorithm, key, iv) {
1262
- const keyBuf = typeof key === "string" ? Buffer.from(key) : new Uint8Array(key);
1263
- const ivBuf = iv == null ? null : typeof iv === "string" ? Buffer.from(iv) : new Uint8Array(iv);
1264
- return new Cipher(algorithm, keyBuf, ivBuf);
1265
- }
1266
- function createDecipher(_algorithm, _password) {
1267
- throw new Error("crypto.createDecipher() is deprecated. Use createDecipheriv() instead.");
1268
- }
1269
- function createDecipheriv(algorithm, key, iv) {
1270
- const keyBuf = typeof key === "string" ? Buffer.from(key) : new Uint8Array(key);
1271
- const ivBuf = iv == null ? null : typeof iv === "string" ? Buffer.from(iv) : new Uint8Array(iv);
1272
- return new Decipher(algorithm, keyBuf, ivBuf);
1273
- }
1274
- function getCiphers() {
1275
- return [
1276
- "aes-128-cbc",
1277
- "aes-128-ecb",
1278
- "aes-192-cbc",
1279
- "aes-192-ecb",
1280
- "aes-256-cbc",
1281
- "aes-256-ecb",
1282
- "aes-128-ctr",
1283
- "aes-192-ctr",
1284
- "aes-256-ctr",
1285
- "aes-128-cfb",
1286
- "aes-192-cfb",
1287
- "aes-256-cfb",
1288
- "aes-128-gcm",
1289
- "aes-192-gcm",
1290
- "aes-256-gcm"
1291
- ];
1292
- }
1293
-
1294
- //#endregion
1295
- export { createCipher, createCipheriv, createDecipher, createDecipheriv, getCiphers };
1
+ import{Buffer as e}from"node:buffer";const t=new Uint8Array([99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,147,38,54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179,41,227,47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64,143,146,157,56,245,188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115,96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,78,169,108,86,244,234,101,122,174,8,186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158,225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22]),n=new Uint8Array([82,9,106,213,48,54,165,56,191,64,163,158,129,243,215,251,124,227,57,130,155,47,255,135,52,142,67,68,196,222,233,203,84,123,148,50,166,194,35,61,238,76,149,11,66,250,195,78,8,46,161,102,40,217,36,178,118,91,162,73,109,139,209,37,114,248,246,100,134,104,152,22,212,164,92,204,93,101,182,146,108,112,72,80,253,237,185,218,94,21,70,87,167,141,157,132,144,216,171,0,140,188,211,10,247,228,88,5,184,179,69,6,208,44,30,143,202,63,15,2,193,175,189,3,1,19,138,107,58,145,17,65,79,103,220,234,151,242,207,206,240,180,230,115,150,172,116,34,231,173,53,133,226,249,55,232,28,117,223,110,71,241,26,113,29,41,197,137,111,183,98,14,170,24,190,27,252,86,62,75,198,210,121,32,154,219,192,254,120,205,90,244,31,221,168,51,136,7,199,49,177,18,16,89,39,128,236,95,96,81,127,169,25,181,74,13,45,229,122,159,147,201,156,239,160,224,59,77,174,42,245,176,200,235,187,60,131,83,153,97,23,43,4,126,186,119,214,38,225,105,20,99,85,33,12,125]),r=[1,2,4,8,16,32,64,128,27,54];function i(e,t){let n=0;for(let r=0;r<8;r++){t&1&&(n^=e);let r=e&128;e=e<<1&255,r&&(e^=27),t>>=1}return n}function a(e){let n=e.length/4,i=n+6,a=4*(i+1),o=Array(a);for(let t=0;t<n;t++)o[t]=new Uint8Array([e[4*t],e[4*t+1],e[4*t+2],e[4*t+3]]);for(let e=n;e<a;e++){let i=new Uint8Array(o[e-1]);e%n===0?i=new Uint8Array([t[i[1]]^r[e/n-1],t[i[2]],t[i[3]],t[i[0]]]):n>6&&e%n===4&&(i=new Uint8Array([t[i[0]],t[i[1]],t[i[2]],t[i[3]]])),o[e]=new Uint8Array(4);for(let t=0;t<4;t++)o[e][t]=o[e-n][t]^i[t]}let s=[];for(let e=0;e<=i;e++){let t=new Uint8Array(16);for(let n=0;n<4;n++)t[4*n]=o[4*e+n][0],t[4*n+1]=o[4*e+n][1],t[4*n+2]=o[4*e+n][2],t[4*n+3]=o[4*e+n][3];s.push(t)}return s}function o(e,n){let r=new Uint8Array(e),a=n.length-1;for(let e=0;e<16;e++)r[e]^=n[0][e];for(let e=1;e<a;e++){for(let e=0;e<16;e++)r[e]=t[r[e]];let a=r[1];r[1]=r[5],r[5]=r[9],r[9]=r[13],r[13]=a;let o=r[2],s=r[6];r[2]=r[10],r[6]=r[14],r[10]=o,r[14]=s;let c=r[15];r[15]=r[11],r[11]=r[7],r[7]=r[3],r[3]=c;for(let e=0;e<4;e++){let t=e*4,n=r[t],a=r[t+1],o=r[t+2],s=r[t+3];r[t]=i(2,n)^i(3,a)^o^s,r[t+1]=n^i(2,a)^i(3,o)^s,r[t+2]=n^a^i(2,o)^i(3,s),r[t+3]=i(3,n)^a^o^i(2,s)}for(let t=0;t<16;t++)r[t]^=n[e][t]}for(let e=0;e<16;e++)r[e]=t[r[e]];let o=r[1];r[1]=r[5],r[5]=r[9],r[9]=r[13],r[13]=o;let s=r[2],c=r[6];r[2]=r[10],r[6]=r[14],r[10]=s,r[14]=c;let l=r[15];r[15]=r[11],r[11]=r[7],r[7]=r[3],r[3]=l;for(let e=0;e<16;e++)r[e]^=n[a][e];return r}function s(e,t){let r=new Uint8Array(e),a=t.length-1;for(let e=0;e<16;e++)r[e]^=t[a][e];for(let e=a-1;e>0;e--){let a=r[13];r[13]=r[9],r[9]=r[5],r[5]=r[1],r[1]=a;let o=r[10],s=r[14];r[10]=r[2],r[14]=r[6],r[2]=o,r[6]=s;let c=r[3];r[3]=r[7],r[7]=r[11],r[11]=r[15],r[15]=c;for(let e=0;e<16;e++)r[e]=n[r[e]];for(let n=0;n<16;n++)r[n]^=t[e][n];for(let e=0;e<4;e++){let t=e*4,n=r[t],a=r[t+1],o=r[t+2],s=r[t+3];r[t]=i(14,n)^i(11,a)^i(13,o)^i(9,s),r[t+1]=i(9,n)^i(14,a)^i(11,o)^i(13,s),r[t+2]=i(13,n)^i(9,a)^i(14,o)^i(11,s),r[t+3]=i(11,n)^i(13,a)^i(9,o)^i(14,s)}}let o=r[13];r[13]=r[9],r[9]=r[5],r[5]=r[1],r[1]=o;let s=r[10],c=r[14];r[10]=r[2],r[14]=r[6],r[2]=s,r[6]=c;let l=r[3];r[3]=r[7],r[7]=r[11],r[11]=r[15],r[15]=l;for(let e=0;e<16;e++)r[e]=n[r[e]];for(let e=0;e<16;e++)r[e]^=t[0][e];return r}function c(e){for(let t=15;t>=0&&++e[t]===0;t--);}function l(e){for(let t=15;t>=12&&++e[t]===0;t--);}function u(e,t){let n=new Uint8Array(16),r=new Uint8Array(e);for(let e=0;e<128;e++){if(t[e>>>3]&1<<7-(e&7))for(let e=0;e<16;e++)n[e]^=r[e];let i=r[15]&1;for(let e=15;e>0;e--)r[e]=r[e]>>>1|(r[e-1]&1)<<7;r[0]>>>=1,i&&(r[0]^=225)}return n}function d(e,t,n){let r=new Uint8Array(16),i=Math.ceil(t.length/16)||0;for(let n=0;n<i;n++){let i=n*16,a=Math.min(i+16,t.length);for(let e=0;e<16;e++){let n=i+e;n<a&&(r[e]^=t[n])}let o=u(r,e);r.set(o)}let a=Math.ceil(n.length/16)||0;for(let t=0;t<a;t++){let i=t*16,a=Math.min(i+16,n.length);for(let e=0;e<16;e++){let t=i+e;t<a&&(r[e]^=n[t])}let o=u(r,e);r.set(o)}let o=new Uint8Array(16),s=t.length*8,c=n.length*8,l=Math.floor(s/4294967296),d=s>>>0;o[0]=l>>>24&255,o[1]=l>>>16&255,o[2]=l>>>8&255,o[3]=l&255,o[4]=d>>>24&255,o[5]=d>>>16&255,o[6]=d>>>8&255,o[7]=d&255;let f=Math.floor(c/4294967296),p=c>>>0;o[8]=f>>>24&255,o[9]=f>>>16&255,o[10]=f>>>8&255,o[11]=f&255,o[12]=p>>>24&255,o[13]=p>>>16&255,o[14]=p>>>8&255,o[15]=p&255;for(let e=0;e<16;e++)r[e]^=o[e];let m=u(r,e);return r.set(m),r}function f(e){let t=e.toLowerCase().match(/^aes-(128|192|256)-(cbc|ctr|ecb|cfb|ofb|gcm)$/);if(!t)throw Error(`Unsupported cipher algorithm: ${e}`);let n=parseInt(t[1]),r=t[2];return{keySize:n/8,ivSize:r===`ecb`?0:r===`gcm`?12:16,mode:r}}function p(t,n){return typeof t==`string`?e.from(t,n||`utf8`):e.from(t)}function m(t,n){return n?e.from(t).toString(n):e.from(t)}function h(e){if(e.length===0)return 0;let t=e.length;for(let n=1;n<=Math.min(4,t);n++){let r=e[t-n];if(!(r&128))return 0;if((r&192)==128)continue;let i;if((r&224)==192)i=2;else if((r&240)==224)i=3;else if((r&248)==240)i=4;else return 0;return n<i?n:0}return 0}function g(e){let t=16-e.length%16,n=new Uint8Array(e.length+t);n.set(e);for(let r=e.length;r<n.length;r++)n[r]=t;return n}function _(e){if(e.length===0||e.length%16!=0)throw Error(`bad decrypt`);let t=e[e.length-1];if(t===0||t>16)throw Error(`bad decrypt`);for(let n=e.length-t;n<e.length;n++)if(e[n]!==t)throw Error(`bad decrypt`);return new Uint8Array(e.slice(0,e.length-t))}var v=class{_roundKeys;_iv;_mode;_buffer=new Uint8Array;_autoPadding=!0;_finalized=!1;constructor(e,t,n){let r=f(e);if(t.length!==r.keySize)throw Error(`Invalid key length ${t.length}, expected ${r.keySize} for ${e}`);if(r.ivSize>0&&(!n||n.length!==r.ivSize))throw Error(`Invalid IV length ${n?.length??0}, expected ${r.ivSize} for ${e}`);this._roundKeys=a(t),this._iv=n?new Uint8Array(n):new Uint8Array(16),this._mode=r.mode}setAutoPadding(e){return this._autoPadding=e,this}},y=class extends v{_prevBlock;_counter;_gcmH=null;_gcmJ0=null;_gcmAAD=new Uint8Array;_gcmCiphertext=[];_gcmCiphertextLen=0;_gcmAuthTag=null;_gcmAADSet=!1;constructor(e,t,n){super(e,t,n),this._prevBlock=new Uint8Array(this._iv),this._mode===`gcm`?(this._gcmH=o(new Uint8Array(16),this._roundKeys),this._gcmJ0=new Uint8Array(16),this._gcmJ0.set(this._iv.subarray(0,12)),this._gcmJ0[15]=1,this._counter=new Uint8Array(this._gcmJ0),l(this._counter)):this._counter=new Uint8Array(this._iv)}setAAD(e){if(this._mode!==`gcm`)throw Error(`setAAD is only supported in GCM mode`);if(this._gcmCiphertextLen>0)throw Error(`setAAD must be called before update()`);return this._gcmAAD=new Uint8Array(e),this._gcmAADSet=!0,this}getAuthTag(){if(this._mode!==`gcm`)throw Error(`getAuthTag is only supported in GCM mode`);if(!this._gcmAuthTag)throw Error(`getAuthTag must be called after final()`);return e.from(this._gcmAuthTag)}update(e,t,n){let r=p(e,t),i=new Uint8Array(this._buffer.length+r.length);if(i.set(this._buffer),i.set(r,this._buffer.length),this._mode===`gcm`){let e=this._processGcmEncrypt(i);return this._buffer=new Uint8Array,m(e,n)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){let e=this._processStream(i);return this._buffer=new Uint8Array,m(e,n)}let a=Math.floor(i.length/16)*16,o=[];for(let e=0;e<a;e+=16){let t=i.slice(e,e+16);o.push(this._encryptBlock(t))}this._buffer=i.slice(a);let s=new Uint8Array(o.length*16);for(let e=0;e<o.length;e++)s.set(o[e],e*16);return m(s,n)}final(t){if(this._finalized)throw Error(`Cipher already finalized`);if(this._finalized=!0,this._mode===`gcm`){let n=new Uint8Array;this._buffer.length>0&&(n=this._processGcmEncrypt(this._buffer),this._buffer=new Uint8Array);let r=new Uint8Array(this._gcmCiphertextLen),i=0;for(let e of this._gcmCiphertext)r.set(e,i),i+=e.length;let a=d(this._gcmH,this._gcmAAD,r),s=o(this._gcmJ0,this._roundKeys),c=new Uint8Array(16);for(let e=0;e<16;e++)c[e]=a[e]^s[e];return this._gcmAuthTag=e.from(c),m(n,t)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){if(this._buffer.length>0){let e=this._processStream(this._buffer);return this._buffer=new Uint8Array,m(e,t)}return m(new Uint8Array,t)}let n=this._buffer;if(this._autoPadding)n=g(n);else if(n.length%16!=0)throw Error(`data not multiple of block size`);let r=[];for(let e=0;e<n.length;e+=16)r.push(this._encryptBlock(n.slice(e,e+16)));if(this._buffer=new Uint8Array,r.length===0)return m(new Uint8Array,t);let i=new Uint8Array(r.length*16);for(let e=0;e<r.length;e++)i.set(r[e],e*16);return m(i,t)}_encryptBlock(e){if(this._mode===`cbc`){let t=new Uint8Array(16);for(let n=0;n<16;n++)t[n]=e[n]^this._prevBlock[n];let n=o(t,this._roundKeys);return this._prevBlock=n,n}else if(this._mode===`ecb`)return o(e,this._roundKeys);throw Error(`Block encryption not supported for mode: ${this._mode}`)}_processStream(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n+=16){let r=o(this._counter,this._roundKeys),i=Math.min(16,e.length-n);for(let a=0;a<i;a++)t[n+a]=e[n+a]^r[a];c(this._counter)}return t}_processGcmEncrypt(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n+=16){let r=o(this._counter,this._roundKeys),i=Math.min(16,e.length-n);for(let a=0;a<i;a++)t[n+a]=e[n+a]^r[a];l(this._counter)}return this._gcmCiphertext.push(new Uint8Array(t)),this._gcmCiphertextLen+=t.length,t}},b=class extends v{_prevBlock;_counter;_pendingUtf8=new Uint8Array;_gcmH=null;_gcmJ0=null;_gcmAAD=new Uint8Array;_gcmCiphertext=[];_gcmCiphertextLen=0;_gcmExpectedTag=null;_gcmAADSet=!1;constructor(e,t,n){super(e,t,n),this._prevBlock=new Uint8Array(this._iv),this._mode===`gcm`?(this._gcmH=o(new Uint8Array(16),this._roundKeys),this._gcmJ0=new Uint8Array(16),this._gcmJ0.set(this._iv.subarray(0,12)),this._gcmJ0[15]=1,this._counter=new Uint8Array(this._gcmJ0),l(this._counter)):this._counter=new Uint8Array(this._iv)}setAAD(e){if(this._mode!==`gcm`)throw Error(`setAAD is only supported in GCM mode`);if(this._gcmCiphertextLen>0)throw Error(`setAAD must be called before update()`);return this._gcmAAD=new Uint8Array(e),this._gcmAADSet=!0,this}setAuthTag(t){if(this._mode!==`gcm`)throw Error(`setAuthTag is only supported in GCM mode`);return this._gcmExpectedTag=e.from(t),this}_encodeWithUtf8Handling(t,n,r){if(!n||n!==`utf8`&&n!==`utf-8`)return m(t,n);let i;if(this._pendingUtf8.length>0?(i=new Uint8Array(this._pendingUtf8.length+t.length),i.set(this._pendingUtf8),i.set(t,this._pendingUtf8.length),this._pendingUtf8=new Uint8Array):i=t,!r){let e=h(i);e>0&&(this._pendingUtf8=new Uint8Array(i.slice(i.length-e)),i=new Uint8Array(i.slice(0,i.length-e)))}return e.from(i).toString(`utf8`)}update(e,t,n){let r=p(e,t),i=new Uint8Array(this._buffer.length+r.length);if(i.set(this._buffer),i.set(r,this._buffer.length),this._mode===`gcm`){this._gcmCiphertext.push(new Uint8Array(i)),this._gcmCiphertextLen+=i.length;let e=this._processGcmDecrypt(i);return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(e,n,!1)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){let e=this._processStream(i);return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(e,n,!1)}let a=Math.floor(i.length/16);if(a===0)return this._buffer=i,this._encodeWithUtf8Handling(new Uint8Array,n,!1);let o=(this._autoPadding?a-1:a)*16,s=[];for(let e=0;e<o;e+=16){let t=i.slice(e,e+16);s.push(this._decryptBlock(t))}this._buffer=i.slice(o);let c=new Uint8Array(s.length*16);for(let e=0;e<s.length;e++)c.set(s[e],e*16);return this._encodeWithUtf8Handling(c,n,!1)}final(e){if(this._finalized)throw Error(`Decipher already finalized`);if(this._finalized=!0,this._mode===`gcm`){let t=new Uint8Array;if(this._buffer.length>0&&(this._gcmCiphertext.push(new Uint8Array(this._buffer)),this._gcmCiphertextLen+=this._buffer.length,t=this._processGcmDecrypt(this._buffer),this._buffer=new Uint8Array),!this._gcmExpectedTag)throw Error(`Unsupported state or unable to authenticate data`);let n=new Uint8Array(this._gcmCiphertextLen),r=0;for(let e of this._gcmCiphertext)n.set(e,r),r+=e.length;let i=d(this._gcmH,this._gcmAAD,n),a=o(this._gcmJ0,this._roundKeys),s=new Uint8Array(16);for(let e=0;e<16;e++)s[e]=i[e]^a[e];let c=this._gcmExpectedTag,l=Math.min(c.length,16),u=0;for(let e=0;e<l;e++)u|=s[e]^c[e];if(u!==0)throw Error(`Unsupported state or unable to authenticate data`);return this._encodeWithUtf8Handling(t,e,!0)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){if(this._buffer.length>0){let t=this._processStream(this._buffer);return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(t,e,!0)}return this._encodeWithUtf8Handling(new Uint8Array,e,!0)}if(this._buffer.length===0)return this._encodeWithUtf8Handling(new Uint8Array,e,!0);if(this._buffer.length%16!=0)throw Error(`bad decrypt`);let t=[];for(let e=0;e<this._buffer.length;e+=16)t.push(this._decryptBlock(this._buffer.slice(e,e+16)));let n=new Uint8Array(t.length*16);for(let e=0;e<t.length;e++)n.set(t[e],e*16);let r=this._autoPadding?_(n):n;return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(r,e,!0)}_decryptBlock(e){if(this._mode===`cbc`){let t=s(e,this._roundKeys),n=new Uint8Array(16);for(let e=0;e<16;e++)n[e]=t[e]^this._prevBlock[e];return this._prevBlock=new Uint8Array(e),n}else if(this._mode===`ecb`)return s(e,this._roundKeys);throw Error(`Block decryption not supported for mode: ${this._mode}`)}_processStream(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n+=16){let r=o(this._counter,this._roundKeys),i=Math.min(16,e.length-n);for(let a=0;a<i;a++)t[n+a]=e[n+a]^r[a];c(this._counter)}return t}_processGcmDecrypt(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n+=16){let r=o(this._counter,this._roundKeys),i=Math.min(16,e.length-n);for(let a=0;a<i;a++)t[n+a]=e[n+a]^r[a];l(this._counter)}return t}};function x(e,t){throw Error(`crypto.createCipher() is deprecated. Use createCipheriv() instead.`)}function S(t,n,r){return new y(t,typeof n==`string`?e.from(n):new Uint8Array(n),r==null?null:typeof r==`string`?e.from(r):new Uint8Array(r))}function C(e,t){throw Error(`crypto.createDecipher() is deprecated. Use createDecipheriv() instead.`)}function w(t,n,r){return new b(t,typeof n==`string`?e.from(n):new Uint8Array(n),r==null?null:typeof r==`string`?e.from(r):new Uint8Array(r))}function T(){return[`aes-128-cbc`,`aes-128-ecb`,`aes-192-cbc`,`aes-192-ecb`,`aes-256-cbc`,`aes-256-ecb`,`aes-128-ctr`,`aes-192-ctr`,`aes-256-ctr`,`aes-128-cfb`,`aes-192-cfb`,`aes-256-cfb`,`aes-128-gcm`,`aes-192-gcm`,`aes-256-gcm`]}export{x as createCipher,S as createCipheriv,C as createDecipher,w as createDecipheriv,T as getCiphers};