1 import { c } from 'ttag';
3 import type { PrivateKeyReference, PublicKeyReference } from '@proton/crypto';
4 import { CryptoProxy, VERIFICATION_STATUS } from '@proton/crypto';
6 export const getDecryptedSessionKey = async ({
7 data: serializedMessage,
10 data: string | Uint8Array;
11 privateKeys: PrivateKeyReference | PrivateKeyReference[];
13 const messageType = serializedMessage instanceof Uint8Array ? 'binaryMessage' : 'armoredMessage';
14 const sessionKey = await CryptoProxy.decryptSessionKey({
15 [messageType]: serializedMessage,
16 decryptionKeys: privateKeys,
19 throw new Error('Could not decrypt session key');
25 export const decryptPassphrase = async ({
30 validateSignature = true,
32 armoredPassphrase: string;
33 armoredSignature?: string;
34 privateKeys: PrivateKeyReference[];
35 publicKeys: PublicKeyReference[];
36 validateSignature?: boolean;
38 const sessionKey = await getDecryptedSessionKey({ data: armoredPassphrase, privateKeys });
40 const { data: decryptedPassphrase, verified } = await CryptoProxy.decryptMessage({
41 armoredMessage: armoredPassphrase,
43 sessionKeys: sessionKey,
44 verificationKeys: publicKeys,
47 if (validateSignature && verified !== VERIFICATION_STATUS.SIGNED_AND_VALID) {
48 const error = new Error(c('Error').t`Signature verification failed`);
49 error.name = 'SignatureError';
53 return { decryptedPassphrase, sessionKey, verified };