1 import type { PrivateKeyReference, PublicKeyReference, SessionKey } from '@proton/crypto';
2 import { CryptoProxy } from '@proton/crypto';
3 import { stringToUtf8Array } from '@proton/crypto/lib/utils';
5 import type { SimpleMap } from '../../interfaces';
6 import type { EncryptPartResult, SignPartResult } from '../../interfaces/calendar';
8 export function signPart(dataToSign: string, signingKey: PrivateKeyReference): Promise<SignPartResult>;
9 export function signPart(
10 dataToSign: string | undefined,
11 signingKey: PrivateKeyReference
12 ): Promise<SignPartResult | undefined>;
14 export async function signPart(
15 dataToSign: string | undefined,
16 signingKey: PrivateKeyReference
17 ): Promise<SignPartResult | undefined> {
22 const signature = await CryptoProxy.signMessage({
23 binaryData: stringToUtf8Array(dataToSign),
24 signingKeys: [signingKey],
33 export function encryptPart(
34 dataToEncrypt: string,
35 signingKey: PrivateKeyReference,
36 sessionKey: SessionKey
37 ): Promise<EncryptPartResult>;
38 export function encryptPart(
39 dataToEncrypt: string | undefined,
40 signingKey: PrivateKeyReference,
41 sessionKey: SessionKey
42 ): Promise<EncryptPartResult | undefined>;
44 export async function encryptPart(
45 dataToEncrypt: string | undefined,
46 signingKey: PrivateKeyReference,
47 sessionKey: SessionKey
48 ): Promise<EncryptPartResult | undefined> {
52 const { message: encryptedData, signature: binarySignature } = await CryptoProxy.encryptMessage({
53 binaryData: stringToUtf8Array(dataToEncrypt),
54 signingKeys: [signingKey],
61 dataPacket: encryptedData,
62 signature: await CryptoProxy.getArmoredSignature({ binarySignature }),
66 export const getEncryptedSessionKey = async ({ data, algorithm }: SessionKey, publicKey: PublicKeyReference) => {
67 const encryptedSessionKey = await CryptoProxy.encryptSessionKey({
70 encryptionKeys: [publicKey],
73 return encryptedSessionKey;
76 export const createSessionKey = async (publicKey: PublicKeyReference) =>
77 CryptoProxy.generateSessionKey({ recipientKeys: publicKey });
79 export const getEncryptedSessionKeysMap = async (
80 sessionKey: SessionKey,
81 publicKeyMap: SimpleMap<PublicKeyReference> = {}
83 const emails = Object.keys(publicKeyMap);
87 const result: SimpleMap<Uint8Array> = {};
89 emails.map(async (email) => {
90 const publicKey = publicKeyMap[email];
94 result[email] = await getEncryptedSessionKey(sessionKey, publicKey);