From: Ardis Lu Date: Sat, 25 Feb 2023 08:10:12 +0000 (-0800) Subject: Add additionalData param X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=30e2ad4705fd2374c7782592891a5738e5e47f0c;p=static-encrypt Add additionalData param --- diff --git a/decrypt.js b/decrypt.js index 409c15a..ee4248b 100644 --- a/decrypt.js +++ b/decrypt.js @@ -5,7 +5,9 @@ * @returns {Promise} A promise that resolves to the plaintext. */ export default async function decrypt(content, password) { - const encodedPassword = new TextEncoder().encode(password); + const encoder = new TextEncoder(); + const encodedPassword = encoder.encode(password); + const additionalData = encoder.encode('https://github.com/ardislu/static-encrypt'); const contentBuffer = Uint8Array.from(atob(content), c => c.charCodeAt(0)); const salt = new Uint8Array(32); @@ -37,7 +39,7 @@ export default async function decrypt(content, password) { ); const encodedPlaintext = new Uint8Array(await crypto.subtle.decrypt( - { name: 'AES-GCM', iv }, + { name: 'AES-GCM', iv, additionalData }, key, ciphertext )); diff --git a/encrypt.js b/encrypt.js index 04e3e97..e53edc8 100644 --- a/encrypt.js +++ b/encrypt.js @@ -8,6 +8,7 @@ export default async function encrypt(plaintext, password) { const encoder = new TextEncoder(); const encodedPlaintext = encoder.encode(plaintext); const encodedPassword = encoder.encode(password); + const additionalData = encoder.encode('https://github.com/ardislu/static-encrypt'); const salt = new Uint8Array(32); const iv = new Uint8Array(12); @@ -36,7 +37,7 @@ export default async function encrypt(plaintext, password) { ); const ciphertext = new Uint8Array(await crypto.subtle.encrypt( - { name: 'AES-GCM', iv }, + { name: 'AES-GCM', iv, additionalData }, key, encodedPlaintext ));