import { webcrypto as crypto } from "node:crypto"; export async function encrypt( plainText: string, keyString: string | undefined, ): Promise { if (!keyString) { throw new Error("Unable to encrypt the feature list."); } const bufToBase64 = (x: ArrayBuffer) => Buffer.from(x).toString("base64"); const key = await crypto.subtle.importKey( "raw", Buffer.from(keyString, "base64"), { name: "AES-CBC", length: 128, }, true, ["encrypt", "decrypt"], ); const iv = crypto.getRandomValues(new Uint8Array(16)); const encryptedBuffer = await crypto.subtle.encrypt( { name: "AES-CBC", iv, }, key, new TextEncoder().encode(plainText), ); return bufToBase64(iv) + "." + bufToBase64(encryptedBuffer); }