]> git.ardis.lu - static-encrypt/commitdiff
Add additionalData param
authorArdis Lu <ardisglu@gmail.com>
Sat, 25 Feb 2023 08:10:12 +0000 (00:10 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sat, 25 Feb 2023 08:10:12 +0000 (00:10 -0800)
decrypt.js
encrypt.js

index 409c15a8c9b25aa9550b64c17dc75c2f5cdd0470..ee4248b89d0bd1dc9955cca34f2e547f480ebaa0 100644 (file)
@@ -5,7 +5,9 @@
  * @returns {Promise<string>} 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
   ));
index 04e3e9717d16000df7ed7d96ceef9ed964fa9e23..e53edc865658c2f54405a4c9d0f043a46b9f4181 100644 (file)
@@ -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
   ));
This page took 0.037016 seconds and 4 git commands to generate.