To implement client-side encryption, we will use the Web Crypto API for encryption and decryption operations. We will ensure that the backend never has access to the plaintext data or encryption keys. First, let's create a function to generate a secure encryption key using the Web Crypto API:
async function generateEncryptionKey(): Promise<CryptoKey> {
// Generate a new AES-GCM key
const key = await crypto.subtle.generateKey(
{
name: "AES-GCM",