Skip to content

Commit dcf12df

Browse files
committed
handle decryption
1 parent 708f939 commit dcf12df

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/CryptoController.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,21 @@ const CryptoController = {
6363
},
6464

6565
async decrypt(encryptedJSON: string, parseSecret: any): Promise<string> {
66-
const buffer = base64ToBuffer(encryptedJSON);
67-
const salt = buffer.slice(0, 16);
68-
const iv = buffer.slice(16, 16 + 12);
69-
const data = buffer.slice(16 + 12);
70-
const key = await importKey(parseSecret);
71-
const aesKey = await deriveKey(key, salt, ['decrypt']);
72-
const decrypted = await webcrypto.subtle.decrypt({ name: 'AES-GCM', iv }, aesKey, data);
73-
return decoder.decode(decrypted);
66+
try {
67+
const buffer = base64ToBuffer(encryptedJSON);
68+
if (buffer.length < 28) { // minimum: 16 salt + 12 IV
69+
throw new Error('Invalid encrypted data format');
70+
}
71+
const salt = buffer.slice(0, 16);
72+
const iv = buffer.slice(16, 16 + 12);
73+
const data = buffer.slice(16 + 12);
74+
const key = await importKey(parseSecret);
75+
const aesKey = await deriveKey(key, salt, ['decrypt']);
76+
const decrypted = await webcrypto.subtle.decrypt({ name: 'AES-GCM', iv }, aesKey, data);
77+
return decoder.decode(decrypted);
78+
} catch (error) {
79+
throw new Error(`Decryption failed: ${error.message}`);
80+
}
7481
},
7582
};
7683

0 commit comments

Comments
 (0)