Encripting a file on the Client side.
Is the following method a valid and secure way to encrypt files?
Does it work with large files (is the entire file stored in Memory during the process)?
const encryptFile = async (
file: Blob
): Promise<{ key: CryptoKey; buffer: Buffer }> => {
const fileBuffer = await file.arrayBuffer();
const key = await crypto.subtle.generateKey(
{ name: "AES-GCM", length: 128 },
true, // extractable
["encrypt", "decrypt"]
);
const encrypted = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv: new Uint8Array(12) /* don't reuse key! */ },
key,
fileBuffer
);
const buf = Buffer.from(encrypted);
return { key, buffer: buf };
};
Are there any Better ways?
Why I need it
I want to build an E2E encryption into my file-sharing site. After encrypting the file with the posted method, I upload it to the server as a binary file.