@storacha/encrypt-upload-client 1.1.56 → 1.1.58

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 (72) hide show
  1. package/dist/config/constants.d.ts +3 -3
  2. package/dist/config/constants.js +4 -3
  3. package/dist/config/env.d.ts +9 -6
  4. package/dist/config/service.d.ts +13 -13
  5. package/dist/core/client.d.ts +54 -41
  6. package/dist/core/client.js +68 -56
  7. package/dist/core/errors.d.ts +6 -6
  8. package/dist/core/metadata/encrypted-metadata.d.ts +13 -8
  9. package/dist/core/metadata/kms-metadata.d.ts +68 -36
  10. package/dist/core/metadata/lit-metadata.d.ts +63 -28
  11. package/dist/crypto/adapters/kms-crypto-adapter.d.ts +172 -137
  12. package/dist/crypto/adapters/lit-crypto-adapter.d.ts +107 -86
  13. package/dist/crypto/factories.browser.d.ts +9 -5
  14. package/dist/crypto/factories.browser.js +15 -7
  15. package/dist/crypto/factories.node.d.ts +13 -6
  16. package/dist/crypto/factories.node.js +19 -13
  17. package/dist/crypto/index.d.ts +5 -5
  18. package/dist/crypto/index.js +5 -5
  19. package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.d.ts +58 -54
  20. package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.js +174 -146
  21. package/dist/crypto/symmetric/node-aes-cbc-crypto.d.ts +36 -32
  22. package/dist/crypto/symmetric/node-aes-cbc-crypto.js +101 -95
  23. package/dist/examples/decrypt-test.d.ts +2 -2
  24. package/dist/examples/decrypt-test.js +78 -69
  25. package/dist/examples/encrypt-test.d.ts +5 -3
  26. package/dist/examples/encrypt-test.js +58 -55
  27. package/dist/handlers/decrypt-handler.d.ts +19 -5
  28. package/dist/handlers/encrypt-handler.d.ts +9 -3
  29. package/dist/handlers/encrypt-handler.js +93 -57
  30. package/dist/index.d.ts +2 -2
  31. package/dist/index.js +2 -2
  32. package/dist/protocols/lit.d.ts +33 -9
  33. package/dist/protocols/lit.js +134 -98
  34. package/dist/test/cid-verification.spec.d.ts +2 -2
  35. package/dist/test/cid-verification.spec.js +341 -313
  36. package/dist/test/crypto-compatibility.spec.d.ts +2 -2
  37. package/dist/test/crypto-compatibility.spec.js +184 -120
  38. package/dist/test/crypto-counter-security.spec.d.ts +2 -2
  39. package/dist/test/crypto-counter-security.spec.js +177 -138
  40. package/dist/test/crypto-streaming.spec.d.ts +2 -2
  41. package/dist/test/crypto-streaming.spec.js +208 -126
  42. package/dist/test/encrypted-metadata.spec.d.ts +2 -2
  43. package/dist/test/encrypted-metadata.spec.js +89 -62
  44. package/dist/test/factories.spec.d.ts +2 -2
  45. package/dist/test/factories.spec.js +275 -139
  46. package/dist/test/file-metadata.spec.d.ts +2 -2
  47. package/dist/test/file-metadata.spec.js +472 -416
  48. package/dist/test/fixtures/test-fixtures.d.ts +25 -20
  49. package/dist/test/fixtures/test-fixtures.js +61 -53
  50. package/dist/test/helpers/test-file-utils.d.ts +19 -14
  51. package/dist/test/helpers/test-file-utils.js +78 -76
  52. package/dist/test/https-enforcement.spec.d.ts +2 -2
  53. package/dist/test/https-enforcement.spec.js +278 -124
  54. package/dist/test/kms-crypto-adapter.spec.d.ts +2 -2
  55. package/dist/test/kms-crypto-adapter.spec.js +473 -304
  56. package/dist/test/lit-crypto-adapter.spec.d.ts +2 -2
  57. package/dist/test/lit-crypto-adapter.spec.js +206 -118
  58. package/dist/test/memory-efficiency.spec.d.ts +2 -2
  59. package/dist/test/memory-efficiency.spec.js +100 -87
  60. package/dist/test/mocks/key-manager.d.ts +71 -38
  61. package/dist/test/mocks/key-manager.js +129 -113
  62. package/dist/test/node-crypto-adapter.spec.d.ts +2 -2
  63. package/dist/test/node-crypto-adapter.spec.js +155 -102
  64. package/dist/test/node-generic-crypto-adapter.spec.d.ts +2 -2
  65. package/dist/test/node-generic-crypto-adapter.spec.js +134 -94
  66. package/dist/test/setup.d.ts +2 -2
  67. package/dist/test/setup.js +8 -9
  68. package/dist/tsconfig.spec.tsbuildinfo +1 -1
  69. package/dist/types.d.ts +219 -181
  70. package/dist/utils/file-metadata.d.ts +19 -13
  71. package/dist/utils.d.ts +14 -5
  72. package/package.json +4 -4
@@ -1,103 +1,156 @@
1
- import { test, describe } from 'node:test';
2
- import assert from 'node:assert';
3
- import { NodeAesCbcCrypto } from '../src/crypto/symmetric/node-aes-cbc-crypto.js';
4
- import { stringToUint8Array, uint8ArrayToString, streamToUint8Array, createMockBlob, } from './helpers/test-file-utils.js';
1
+ import { test, describe } from 'node:test'
2
+ import assert from 'node:assert'
3
+ import { NodeAesCbcCrypto } from '../src/crypto/symmetric/node-aes-cbc-crypto.js'
4
+ import {
5
+ stringToUint8Array,
6
+ uint8ArrayToString,
7
+ streamToUint8Array,
8
+ createMockBlob,
9
+ } from './helpers/test-file-utils.js'
5
10
  await describe('NodeAesCbcCrypto', async () => {
6
- await test('should encrypt and decrypt data and return the original data', async () => {
7
- const adapter = new NodeAesCbcCrypto();
8
- const originalText = 'Hello, this is a test for Node.js AES-CBC encryption!';
9
- const mockBlob = createMockBlob(stringToUint8Array(originalText));
10
- // Encrypt
11
- const { key, iv, encryptedStream } = await adapter.encryptStream(mockBlob);
12
- // Decrypt
13
- const decryptedStream = await adapter.decryptStream(encryptedStream, key, iv);
14
- const decryptedBytes = await streamToUint8Array(decryptedStream);
15
- const decryptedText = uint8ArrayToString(decryptedBytes);
16
- assert.strictEqual(decryptedText, originalText);
17
- });
18
- await test('should handle empty data', async () => {
19
- const adapter = new NodeAesCbcCrypto();
20
- const mockBlob = createMockBlob(new Uint8Array(0));
21
- const { key, iv, encryptedStream } = await adapter.encryptStream(mockBlob);
22
- const decryptedStream = await adapter.decryptStream(encryptedStream, key, iv);
23
- const decryptedBytes = await streamToUint8Array(decryptedStream);
24
- assert.strictEqual(decryptedBytes.length, 0);
25
- });
26
- await test('should combine key and IV correctly', async () => {
27
- const adapter = new NodeAesCbcCrypto();
28
- const key = new Uint8Array(32).fill(3); // 32-byte AES-256 key
29
- const iv = new Uint8Array(16).fill(6); // 16-byte AES-CBC IV
30
- const combined = adapter.combineKeyAndIV(key, iv);
31
- assert.strictEqual(combined.length, 48, 'Combined length should be 48 bytes (32 + 16)');
32
- // Verify first 32 bytes are the key
33
- for (let i = 0; i < 32; i++) {
34
- assert.strictEqual(combined[i], 3, `Key byte ${i} should match`);
35
- }
36
- // Verify last 16 bytes are the IV
37
- for (let i = 32; i < 48; i++) {
38
- assert.strictEqual(combined[i], 6, `IV byte ${i - 32} should match`);
39
- }
40
- });
41
- await test('should split combined key and IV correctly', async () => {
42
- const adapter = new NodeAesCbcCrypto();
43
- const originalKey = new Uint8Array(32).fill(123);
44
- const originalIV = new Uint8Array(16).fill(234);
45
- const combined = adapter.combineKeyAndIV(originalKey, originalIV);
46
- const { key, iv } = adapter.splitKeyAndIV(combined);
47
- assert.strictEqual(key.length, 32, 'Split key should be 32 bytes');
48
- assert.strictEqual(iv.length, 16, 'Split IV should be 16 bytes');
49
- // Verify key matches
50
- for (let i = 0; i < 32; i++) {
51
- assert.strictEqual(key[i], originalKey[i], `Key byte ${i} should match original`);
52
- }
53
- // Verify IV matches
54
- for (let i = 0; i < 16; i++) {
55
- assert.strictEqual(iv[i], originalIV[i], `IV byte ${i} should match original`);
56
- }
57
- });
58
- await test('should roundtrip combine/split correctly', async () => {
59
- const adapter = new NodeAesCbcCrypto();
60
- const { randomBytes } = await import('crypto');
61
- // Convert Buffer to Uint8Array to match the expected types
62
- const originalKey = new Uint8Array(randomBytes(32));
63
- const originalIV = new Uint8Array(randomBytes(16));
64
- const combined = adapter.combineKeyAndIV(originalKey, originalIV);
65
- const { key, iv } = adapter.splitKeyAndIV(combined);
66
- assert.deepStrictEqual(key, originalKey, 'Roundtrip key should match original');
67
- assert.deepStrictEqual(iv, originalIV, 'Roundtrip IV should match original');
68
- });
69
- await test('should validate key length in combineKeyAndIV', async () => {
70
- const adapter = new NodeAesCbcCrypto();
71
- const wrongKey = new Uint8Array(31); // Wrong size
72
- const correctIV = new Uint8Array(16);
73
- assert.throws(() => adapter.combineKeyAndIV(wrongKey, correctIV), {
74
- name: 'Error',
75
- message: 'AES-256 key must be 32 bytes, got 31',
76
- }, 'Should throw error for wrong key size');
77
- });
78
- await test('should validate IV length in combineKeyAndIV', async () => {
79
- const adapter = new NodeAesCbcCrypto();
80
- const correctKey = new Uint8Array(32);
81
- const wrongIV = new Uint8Array(15); // Wrong size
82
- assert.throws(() => adapter.combineKeyAndIV(correctKey, wrongIV), {
83
- name: 'Error',
84
- message: 'AES-CBC IV must be 16 bytes, got 15',
85
- }, 'Should throw error for wrong IV size');
86
- });
87
- await test('should validate combined length in splitKeyAndIV', async () => {
88
- const adapter = new NodeAesCbcCrypto();
89
- const wrongCombined = new Uint8Array(47); // Wrong size
90
- assert.throws(() => adapter.splitKeyAndIV(wrongCombined), {
91
- name: 'Error',
92
- message: 'AES-256-CBC combined key+IV must be 48 bytes, got 47',
93
- }, 'Should throw error for wrong combined size');
94
- });
95
- await test('should use constants for validation', async () => {
96
- const adapter = new NodeAesCbcCrypto();
97
- // Test with different wrong sizes to ensure constants are used
98
- assert.throws(() => adapter.combineKeyAndIV(new Uint8Array(16), new Uint8Array(16)), /AES-256 key must be 32 bytes, got 16/, 'Should use KEY_LENGTH constant in error message');
99
- assert.throws(() => adapter.combineKeyAndIV(new Uint8Array(32), new Uint8Array(8)), /AES-CBC IV must be 16 bytes, got 8/, 'Should use IV_LENGTH constant in error message');
100
- assert.throws(() => adapter.splitKeyAndIV(new Uint8Array(32)), /AES-256-CBC combined key\+IV must be 48 bytes, got 32/, 'Should use calculated expected length in error message');
101
- });
102
- });
103
- //# sourceMappingURL=node-crypto-adapter.spec.js.map
11
+ await test('should encrypt and decrypt data and return the original data', async () => {
12
+ const adapter = new NodeAesCbcCrypto()
13
+ const originalText = 'Hello, this is a test for Node.js AES-CBC encryption!'
14
+ const mockBlob = createMockBlob(stringToUint8Array(originalText))
15
+ // Encrypt
16
+ const { key, iv, encryptedStream } = await adapter.encryptStream(mockBlob)
17
+ // Decrypt
18
+ const decryptedStream = await adapter.decryptStream(
19
+ encryptedStream,
20
+ key,
21
+ iv
22
+ )
23
+ const decryptedBytes = await streamToUint8Array(decryptedStream)
24
+ const decryptedText = uint8ArrayToString(decryptedBytes)
25
+ assert.strictEqual(decryptedText, originalText)
26
+ })
27
+ await test('should handle empty data', async () => {
28
+ const adapter = new NodeAesCbcCrypto()
29
+ const mockBlob = createMockBlob(new Uint8Array(0))
30
+ const { key, iv, encryptedStream } = await adapter.encryptStream(mockBlob)
31
+ const decryptedStream = await adapter.decryptStream(
32
+ encryptedStream,
33
+ key,
34
+ iv
35
+ )
36
+ const decryptedBytes = await streamToUint8Array(decryptedStream)
37
+ assert.strictEqual(decryptedBytes.length, 0)
38
+ })
39
+ await test('should combine key and IV correctly', async () => {
40
+ const adapter = new NodeAesCbcCrypto()
41
+ const key = new Uint8Array(32).fill(3) // 32-byte AES-256 key
42
+ const iv = new Uint8Array(16).fill(6) // 16-byte AES-CBC IV
43
+ const combined = adapter.combineKeyAndIV(key, iv)
44
+ assert.strictEqual(
45
+ combined.length,
46
+ 48,
47
+ 'Combined length should be 48 bytes (32 + 16)'
48
+ )
49
+ // Verify first 32 bytes are the key
50
+ for (let i = 0; i < 32; i++) {
51
+ assert.strictEqual(combined[i], 3, `Key byte ${i} should match`)
52
+ }
53
+ // Verify last 16 bytes are the IV
54
+ for (let i = 32; i < 48; i++) {
55
+ assert.strictEqual(combined[i], 6, `IV byte ${i - 32} should match`)
56
+ }
57
+ })
58
+ await test('should split combined key and IV correctly', async () => {
59
+ const adapter = new NodeAesCbcCrypto()
60
+ const originalKey = new Uint8Array(32).fill(123)
61
+ const originalIV = new Uint8Array(16).fill(234)
62
+ const combined = adapter.combineKeyAndIV(originalKey, originalIV)
63
+ const { key, iv } = adapter.splitKeyAndIV(combined)
64
+ assert.strictEqual(key.length, 32, 'Split key should be 32 bytes')
65
+ assert.strictEqual(iv.length, 16, 'Split IV should be 16 bytes')
66
+ // Verify key matches
67
+ for (let i = 0; i < 32; i++) {
68
+ assert.strictEqual(
69
+ key[i],
70
+ originalKey[i],
71
+ `Key byte ${i} should match original`
72
+ )
73
+ }
74
+ // Verify IV matches
75
+ for (let i = 0; i < 16; i++) {
76
+ assert.strictEqual(
77
+ iv[i],
78
+ originalIV[i],
79
+ `IV byte ${i} should match original`
80
+ )
81
+ }
82
+ })
83
+ await test('should roundtrip combine/split correctly', async () => {
84
+ const adapter = new NodeAesCbcCrypto()
85
+ const { randomBytes } = await import('crypto')
86
+ // Convert Buffer to Uint8Array to match the expected types
87
+ const originalKey = new Uint8Array(randomBytes(32))
88
+ const originalIV = new Uint8Array(randomBytes(16))
89
+ const combined = adapter.combineKeyAndIV(originalKey, originalIV)
90
+ const { key, iv } = adapter.splitKeyAndIV(combined)
91
+ assert.deepStrictEqual(
92
+ key,
93
+ originalKey,
94
+ 'Roundtrip key should match original'
95
+ )
96
+ assert.deepStrictEqual(iv, originalIV, 'Roundtrip IV should match original')
97
+ })
98
+ await test('should validate key length in combineKeyAndIV', async () => {
99
+ const adapter = new NodeAesCbcCrypto()
100
+ const wrongKey = new Uint8Array(31) // Wrong size
101
+ const correctIV = new Uint8Array(16)
102
+ assert.throws(
103
+ () => adapter.combineKeyAndIV(wrongKey, correctIV),
104
+ {
105
+ name: 'Error',
106
+ message: 'AES-256 key must be 32 bytes, got 31',
107
+ },
108
+ 'Should throw error for wrong key size'
109
+ )
110
+ })
111
+ await test('should validate IV length in combineKeyAndIV', async () => {
112
+ const adapter = new NodeAesCbcCrypto()
113
+ const correctKey = new Uint8Array(32)
114
+ const wrongIV = new Uint8Array(15) // Wrong size
115
+ assert.throws(
116
+ () => adapter.combineKeyAndIV(correctKey, wrongIV),
117
+ {
118
+ name: 'Error',
119
+ message: 'AES-CBC IV must be 16 bytes, got 15',
120
+ },
121
+ 'Should throw error for wrong IV size'
122
+ )
123
+ })
124
+ await test('should validate combined length in splitKeyAndIV', async () => {
125
+ const adapter = new NodeAesCbcCrypto()
126
+ const wrongCombined = new Uint8Array(47) // Wrong size
127
+ assert.throws(
128
+ () => adapter.splitKeyAndIV(wrongCombined),
129
+ {
130
+ name: 'Error',
131
+ message: 'AES-256-CBC combined key+IV must be 48 bytes, got 47',
132
+ },
133
+ 'Should throw error for wrong combined size'
134
+ )
135
+ })
136
+ await test('should use constants for validation', async () => {
137
+ const adapter = new NodeAesCbcCrypto()
138
+ // Test with different wrong sizes to ensure constants are used
139
+ assert.throws(
140
+ () => adapter.combineKeyAndIV(new Uint8Array(16), new Uint8Array(16)),
141
+ /AES-256 key must be 32 bytes, got 16/,
142
+ 'Should use KEY_LENGTH constant in error message'
143
+ )
144
+ assert.throws(
145
+ () => adapter.combineKeyAndIV(new Uint8Array(32), new Uint8Array(8)),
146
+ /AES-CBC IV must be 16 bytes, got 8/,
147
+ 'Should use IV_LENGTH constant in error message'
148
+ )
149
+ assert.throws(
150
+ () => adapter.splitKeyAndIV(new Uint8Array(32)),
151
+ /AES-256-CBC combined key\+IV must be 48 bytes, got 32/,
152
+ 'Should use calculated expected length in error message'
153
+ )
154
+ })
155
+ })
156
+ //# sourceMappingURL=node-crypto-adapter.spec.js.map
@@ -1,2 +1,2 @@
1
- export {};
2
- //# sourceMappingURL=node-generic-crypto-adapter.spec.d.ts.map
1
+ export {}
2
+ //# sourceMappingURL=node-generic-crypto-adapter.spec.d.ts.map
@@ -1,95 +1,135 @@
1
- import './setup.js';
2
- import { test, describe } from 'node:test';
3
- import assert from 'node:assert';
4
- import { GenericAesCtrStreamingCrypto } from '../src/crypto/symmetric/generic-aes-ctr-streaming-crypto.js';
5
- import { stringToUint8Array, streamToUint8Array, uint8ArrayToString, } from './helpers/test-file-utils.js';
1
+ import './setup.js'
2
+ import { test, describe } from 'node:test'
3
+ import assert from 'node:assert'
4
+ import { GenericAesCtrStreamingCrypto } from '../src/crypto/symmetric/generic-aes-ctr-streaming-crypto.js'
5
+ import {
6
+ stringToUint8Array,
7
+ streamToUint8Array,
8
+ uint8ArrayToString,
9
+ } from './helpers/test-file-utils.js'
6
10
  await describe('GenericAesCtrStreamingCrypto (Node Environment)', async () => {
7
- await test('should encrypt and decrypt a Blob and return the original data', async () => {
8
- const adapter = new GenericAesCtrStreamingCrypto();
9
- const originalText = 'Op, this is a test for streaming encryption!';
10
- const blob = new Blob([stringToUint8Array(originalText)]);
11
- // Encrypt
12
- const { key, iv, encryptedStream } = await adapter.encryptStream(blob);
13
- // Decrypt
14
- const decryptedStream = await adapter.decryptStream(encryptedStream, key, iv);
15
- const decryptedBytes = await streamToUint8Array(decryptedStream);
16
- const decryptedText = uint8ArrayToString(decryptedBytes);
17
- assert.strictEqual(decryptedText, originalText);
18
- });
19
- await test('should handle empty data', async () => {
20
- const adapter = new GenericAesCtrStreamingCrypto();
21
- const blob = new Blob([]);
22
- const { key, iv, encryptedStream } = await adapter.encryptStream(blob);
23
- const decryptedStream = await adapter.decryptStream(encryptedStream, key, iv);
24
- const decryptedBytes = await streamToUint8Array(decryptedStream);
25
- assert.strictEqual(decryptedBytes.length, 0);
26
- });
27
- await test('should combine key and IV correctly', async () => {
28
- const adapter = new GenericAesCtrStreamingCrypto();
29
- const key = new Uint8Array(32).fill(1); // 32-byte AES-256 key
30
- const iv = new Uint8Array(16).fill(2); // 16-byte AES-CTR IV
31
- const combined = adapter.combineKeyAndIV(key, iv);
32
- assert.strictEqual(combined.length, 48, 'Combined length should be 48 bytes (32 + 16)');
33
- // Verify first 32 bytes are the key
34
- for (let i = 0; i < 32; i++) {
35
- assert.strictEqual(combined[i], 1, `Key byte ${i} should match`);
36
- }
37
- // Verify last 16 bytes are the IV
38
- for (let i = 32; i < 48; i++) {
39
- assert.strictEqual(combined[i], 2, `IV byte ${i - 32} should match`);
40
- }
41
- });
42
- await test('should split combined key and IV correctly', async () => {
43
- const adapter = new GenericAesCtrStreamingCrypto();
44
- const originalKey = new Uint8Array(32).fill(42);
45
- const originalIV = new Uint8Array(16).fill(84);
46
- const combined = adapter.combineKeyAndIV(originalKey, originalIV);
47
- const { key, iv } = adapter.splitKeyAndIV(combined);
48
- assert.strictEqual(key.length, 32, 'Split key should be 32 bytes');
49
- assert.strictEqual(iv.length, 16, 'Split IV should be 16 bytes');
50
- // Verify key matches
51
- for (let i = 0; i < 32; i++) {
52
- assert.strictEqual(key[i], originalKey[i], `Key byte ${i} should match original`);
53
- }
54
- // Verify IV matches
55
- for (let i = 0; i < 16; i++) {
56
- assert.strictEqual(iv[i], originalIV[i], `IV byte ${i} should match original`);
57
- }
58
- });
59
- await test('should roundtrip combine/split correctly', async () => {
60
- const adapter = new GenericAesCtrStreamingCrypto();
61
- const originalKey = globalThis.crypto.getRandomValues(new Uint8Array(32));
62
- const originalIV = globalThis.crypto.getRandomValues(new Uint8Array(16));
63
- const combined = adapter.combineKeyAndIV(originalKey, originalIV);
64
- const { key, iv } = adapter.splitKeyAndIV(combined);
65
- assert.deepStrictEqual(key, originalKey, 'Roundtrip key should match original');
66
- assert.deepStrictEqual(iv, originalIV, 'Roundtrip IV should match original');
67
- });
68
- await test('should validate key length in combineKeyAndIV', async () => {
69
- const adapter = new GenericAesCtrStreamingCrypto();
70
- const wrongKey = new Uint8Array(31); // Wrong size
71
- const correctIV = new Uint8Array(16);
72
- assert.throws(() => adapter.combineKeyAndIV(wrongKey, correctIV), {
73
- name: 'Error',
74
- message: 'AES-256 key must be 32 bytes, got 31',
75
- }, 'Should throw error for wrong key size');
76
- });
77
- await test('should validate IV length in combineKeyAndIV', async () => {
78
- const adapter = new GenericAesCtrStreamingCrypto();
79
- const correctKey = new Uint8Array(32);
80
- const wrongIV = new Uint8Array(15); // Wrong size
81
- assert.throws(() => adapter.combineKeyAndIV(correctKey, wrongIV), {
82
- name: 'Error',
83
- message: 'AES-CTR IV must be 16 bytes, got 15',
84
- }, 'Should throw error for wrong IV size');
85
- });
86
- await test('should validate combined length in splitKeyAndIV', async () => {
87
- const adapter = new GenericAesCtrStreamingCrypto();
88
- const wrongCombined = new Uint8Array(47); // Wrong size
89
- assert.throws(() => adapter.splitKeyAndIV(wrongCombined), {
90
- name: 'Error',
91
- message: 'AES-256-CTR combined key+IV must be 48 bytes, got 47',
92
- }, 'Should throw error for wrong combined size');
93
- });
94
- });
95
- //# sourceMappingURL=node-generic-crypto-adapter.spec.js.map
11
+ await test('should encrypt and decrypt a Blob and return the original data', async () => {
12
+ const adapter = new GenericAesCtrStreamingCrypto()
13
+ const originalText = 'Op, this is a test for streaming encryption!'
14
+ const blob = new Blob([stringToUint8Array(originalText)])
15
+ // Encrypt
16
+ const { key, iv, encryptedStream } = await adapter.encryptStream(blob)
17
+ // Decrypt
18
+ const decryptedStream = await adapter.decryptStream(
19
+ encryptedStream,
20
+ key,
21
+ iv
22
+ )
23
+ const decryptedBytes = await streamToUint8Array(decryptedStream)
24
+ const decryptedText = uint8ArrayToString(decryptedBytes)
25
+ assert.strictEqual(decryptedText, originalText)
26
+ })
27
+ await test('should handle empty data', async () => {
28
+ const adapter = new GenericAesCtrStreamingCrypto()
29
+ const blob = new Blob([])
30
+ const { key, iv, encryptedStream } = await adapter.encryptStream(blob)
31
+ const decryptedStream = await adapter.decryptStream(
32
+ encryptedStream,
33
+ key,
34
+ iv
35
+ )
36
+ const decryptedBytes = await streamToUint8Array(decryptedStream)
37
+ assert.strictEqual(decryptedBytes.length, 0)
38
+ })
39
+ await test('should combine key and IV correctly', async () => {
40
+ const adapter = new GenericAesCtrStreamingCrypto()
41
+ const key = new Uint8Array(32).fill(1) // 32-byte AES-256 key
42
+ const iv = new Uint8Array(16).fill(2) // 16-byte AES-CTR IV
43
+ const combined = adapter.combineKeyAndIV(key, iv)
44
+ assert.strictEqual(
45
+ combined.length,
46
+ 48,
47
+ 'Combined length should be 48 bytes (32 + 16)'
48
+ )
49
+ // Verify first 32 bytes are the key
50
+ for (let i = 0; i < 32; i++) {
51
+ assert.strictEqual(combined[i], 1, `Key byte ${i} should match`)
52
+ }
53
+ // Verify last 16 bytes are the IV
54
+ for (let i = 32; i < 48; i++) {
55
+ assert.strictEqual(combined[i], 2, `IV byte ${i - 32} should match`)
56
+ }
57
+ })
58
+ await test('should split combined key and IV correctly', async () => {
59
+ const adapter = new GenericAesCtrStreamingCrypto()
60
+ const originalKey = new Uint8Array(32).fill(42)
61
+ const originalIV = new Uint8Array(16).fill(84)
62
+ const combined = adapter.combineKeyAndIV(originalKey, originalIV)
63
+ const { key, iv } = adapter.splitKeyAndIV(combined)
64
+ assert.strictEqual(key.length, 32, 'Split key should be 32 bytes')
65
+ assert.strictEqual(iv.length, 16, 'Split IV should be 16 bytes')
66
+ // Verify key matches
67
+ for (let i = 0; i < 32; i++) {
68
+ assert.strictEqual(
69
+ key[i],
70
+ originalKey[i],
71
+ `Key byte ${i} should match original`
72
+ )
73
+ }
74
+ // Verify IV matches
75
+ for (let i = 0; i < 16; i++) {
76
+ assert.strictEqual(
77
+ iv[i],
78
+ originalIV[i],
79
+ `IV byte ${i} should match original`
80
+ )
81
+ }
82
+ })
83
+ await test('should roundtrip combine/split correctly', async () => {
84
+ const adapter = new GenericAesCtrStreamingCrypto()
85
+ const originalKey = globalThis.crypto.getRandomValues(new Uint8Array(32))
86
+ const originalIV = globalThis.crypto.getRandomValues(new Uint8Array(16))
87
+ const combined = adapter.combineKeyAndIV(originalKey, originalIV)
88
+ const { key, iv } = adapter.splitKeyAndIV(combined)
89
+ assert.deepStrictEqual(
90
+ key,
91
+ originalKey,
92
+ 'Roundtrip key should match original'
93
+ )
94
+ assert.deepStrictEqual(iv, originalIV, 'Roundtrip IV should match original')
95
+ })
96
+ await test('should validate key length in combineKeyAndIV', async () => {
97
+ const adapter = new GenericAesCtrStreamingCrypto()
98
+ const wrongKey = new Uint8Array(31) // Wrong size
99
+ const correctIV = new Uint8Array(16)
100
+ assert.throws(
101
+ () => adapter.combineKeyAndIV(wrongKey, correctIV),
102
+ {
103
+ name: 'Error',
104
+ message: 'AES-256 key must be 32 bytes, got 31',
105
+ },
106
+ 'Should throw error for wrong key size'
107
+ )
108
+ })
109
+ await test('should validate IV length in combineKeyAndIV', async () => {
110
+ const adapter = new GenericAesCtrStreamingCrypto()
111
+ const correctKey = new Uint8Array(32)
112
+ const wrongIV = new Uint8Array(15) // Wrong size
113
+ assert.throws(
114
+ () => adapter.combineKeyAndIV(correctKey, wrongIV),
115
+ {
116
+ name: 'Error',
117
+ message: 'AES-CTR IV must be 16 bytes, got 15',
118
+ },
119
+ 'Should throw error for wrong IV size'
120
+ )
121
+ })
122
+ await test('should validate combined length in splitKeyAndIV', async () => {
123
+ const adapter = new GenericAesCtrStreamingCrypto()
124
+ const wrongCombined = new Uint8Array(47) // Wrong size
125
+ assert.throws(
126
+ () => adapter.splitKeyAndIV(wrongCombined),
127
+ {
128
+ name: 'Error',
129
+ message: 'AES-256-CTR combined key+IV must be 48 bytes, got 47',
130
+ },
131
+ 'Should throw error for wrong combined size'
132
+ )
133
+ })
134
+ })
135
+ //# sourceMappingURL=node-generic-crypto-adapter.spec.js.map
@@ -1,2 +1,2 @@
1
- export {};
2
- //# sourceMappingURL=setup.d.ts.map
1
+ export {}
2
+ //# sourceMappingURL=setup.d.ts.map
@@ -1,12 +1,11 @@
1
1
  // Centralized polyfill for globalThis.crypto for Node.js <19
2
2
  if (typeof globalThis.crypto === 'undefined') {
3
- try {
4
- // @ts-expect-error
5
- globalThis.crypto = (await import('crypto')).webcrypto;
6
- }
7
- catch (e) {
8
- throw new Error('globalThis.crypto is not available.');
9
- }
3
+ try {
4
+ // @ts-expect-error
5
+ globalThis.crypto = (await import('crypto')).webcrypto
6
+ } catch (e) {
7
+ throw new Error('globalThis.crypto is not available.')
8
+ }
10
9
  }
11
- export {};
12
- //# sourceMappingURL=setup.js.map
10
+ export {}
11
+ //# sourceMappingURL=setup.js.map