|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 3 | + |
1 | 4 | import { |
2 | 5 | FieldInfo, |
3 | 6 | NestedWriteVisitor, |
@@ -37,13 +40,20 @@ class EncryptedHandler extends DefaultPrismaProxyHandler { |
37 | 40 | super(prisma, model, options); |
38 | 41 |
|
39 | 42 | this.queryUtils = new QueryUtils(prisma, options); |
| 43 | + |
| 44 | + if (!options.encryption) throw new Error('Encryption options must be provided'); |
| 45 | + |
| 46 | + if (this.isCustomEncryption(options.encryption!)) { |
| 47 | + if (!options.encryption.encrypt || !options.encryption.decrypt) |
| 48 | + throw new Error('Custom encryption must provide encrypt and decrypt functions'); |
| 49 | + } else { |
| 50 | + if (!options.encryption.encryptionKey) throw new Error('Encryption key must be provided'); |
| 51 | + if (options.encryption.encryptionKey.length !== 32) throw new Error('Encryption key must be 32 bytes'); |
| 52 | + } |
40 | 53 | } |
41 | 54 |
|
42 | | - private async getKey(secret: string): Promise<CryptoKey> { |
43 | | - return crypto.subtle.importKey('raw', this.encoder.encode(secret).slice(0, 32), 'AES-GCM', false, [ |
44 | | - 'encrypt', |
45 | | - 'decrypt', |
46 | | - ]); |
| 55 | + private async getKey(secret: Uint8Array): Promise<CryptoKey> { |
| 56 | + return crypto.subtle.importKey('raw', secret, 'AES-GCM', false, ['encrypt', 'decrypt']); |
47 | 57 | } |
48 | 58 |
|
49 | 59 | private isCustomEncryption(encryption: CustomEncryption | SimpleEncryption): encryption is CustomEncryption { |
@@ -82,7 +92,7 @@ class EncryptedHandler extends DefaultPrismaProxyHandler { |
82 | 92 | const key = await this.getKey(this.options.encryption!.encryptionKey); |
83 | 93 |
|
84 | 94 | // Convert base64 back to bytes |
85 | | - const bytes = Uint8Array.from(atob(data)); |
| 95 | + const bytes = Uint8Array.from(atob(data), (c) => c.charCodeAt(0)); |
86 | 96 |
|
87 | 97 | // First 12 bytes are IV, rest is encrypted data |
88 | 98 | const decrypted = await crypto.subtle.decrypt( |
|
0 commit comments