End to end encrypted attachment upload and streaming

This commit is contained in:
Aslan 2026-01-16 18:30:12 -05:00
parent 575e9e2010
commit 64ad8498f5
74 changed files with 2368 additions and 151 deletions

View file

@ -60,6 +60,28 @@ const decryptData = async <T>(cryptoData: ICryptoEncrypted): Promise<T> => {
return decoded as T;
};
const encryptBytes = async (
cryptoData: ICryptoData<ArrayBuffer>,
): Promise<ArrayBuffer> => {
const encrypted = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv: cryptoData.iv },
cryptoData.key,
cryptoData.data,
);
return encrypted;
};
const decryptBytes = async (
cryptoData: ICryptoEncrypted,
): Promise<ArrayBuffer> => {
return await crypto.subtle.decrypt(
{ name: "AES-GCM", iv: cryptoData.iv },
cryptoData.key,
cryptoData.encryptedData,
);
};
const generateIv = (): Uint8Array<ArrayBuffer> => {
return crypto.getRandomValues(new Uint8Array(12));
};
@ -84,12 +106,39 @@ const bytesToHex = (bytes: ArrayBuffer): string => {
return hex;
};
const bufferToBase64 = (buffer: ArrayBuffer): string => {
const bytes = new Uint8Array(buffer);
let binary = "";
for (let i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
};
const base64ToBuffer = (base64: string): ArrayBuffer => {
const binary = atob(base64);
const len = binary.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes.buffer;
};
export {
importKey,
deriveKey,
encryptData,
decryptData,
encryptBytes,
decryptBytes,
generateIv,
hexToBytes,
bytesToHex,
bufferToBase64,
base64ToBuffer,
};