]> git.ardis.lu - static-encrypt/commitdiff
Move encrypt into its own module
authorArdis Lu <ardisglu@gmail.com>
Thu, 23 Feb 2023 03:58:19 +0000 (19:58 -0800)
committerArdis Lu <ardisglu@gmail.com>
Thu, 23 Feb 2023 03:58:19 +0000 (19:58 -0800)
encrypt.js [new file with mode: 0644]
index.html

diff --git a/encrypt.js b/encrypt.js
new file mode 100644 (file)
index 0000000..febaffa
--- /dev/null
@@ -0,0 +1,45 @@
+export default async function encrypt(plaintext, password) {
+  const encoder = new TextEncoder();
+  const encodedPlaintext = encoder.encode(plaintext);
+  const encodedPassword = encoder.encode(password);
+
+  const salt = new Uint8Array(32);
+  const iv = new Uint8Array(32);
+  crypto.getRandomValues(salt);
+  crypto.getRandomValues(iv);
+
+  const keyMaterial = await crypto.subtle.importKey(
+    'raw',
+    encodedPassword,
+    'PBKDF2',
+    false,
+    ['deriveKey']
+  );
+
+  const key = await crypto.subtle.deriveKey(
+    {
+      name: 'PBKDF2',
+      hash: 'SHA-512',
+      salt,
+      iterations: 400_000
+    },
+    keyMaterial,
+    { name: 'AES-GCM', length: 256 },
+    false,
+    ['encrypt']
+  );
+
+  const ciphertext = new Uint8Array(await crypto.subtle.encrypt(
+    { name: 'AES-GCM', iv },
+    key,
+    encodedPlaintext
+  ));
+
+  const contentBuffer = new Uint8Array(salt.byteLength + iv.byteLength + ciphertext.byteLength);
+  contentBuffer.set(salt);
+  contentBuffer.set(iv, salt.byteLength);
+  contentBuffer.set(ciphertext, salt.byteLength + iv.byteLength);
+  const content = btoa(String.fromCharCode(...contentBuffer));
+
+  return content;
+}
\ No newline at end of file
index 35caf7c562cfa2432861c8582c594e54d712f9fc..0a7f3c43c060d0782c0fde99017a306d7e04abff 100644 (file)
   </footer>
 
   <script type="module">
+    import encrypt from '/encrypt.js';
     import decrypt from '/decrypt.js';
 
-    async function encrypt(plaintext, password) {
-      const encoder = new TextEncoder();
-      const encodedPlaintext = encoder.encode(plaintext);
-      const encodedPassword = encoder.encode(password);
-
-      const salt = new Uint8Array(32);
-      const iv = new Uint8Array(32);
-      crypto.getRandomValues(salt);
-      crypto.getRandomValues(iv);
-
-      const keyMaterial = await crypto.subtle.importKey(
-        'raw',
-        encodedPassword,
-        'PBKDF2',
-        false,
-        ['deriveKey']
-      );
-
-      const key = await crypto.subtle.deriveKey(
-        {
-          name: 'PBKDF2',
-          hash: 'SHA-512',
-          salt,
-          iterations: 400_000
-        },
-        keyMaterial,
-        { name: 'AES-GCM', length: 256 },
-        false,
-        ['encrypt']
-      );
-
-      const ciphertext = new Uint8Array(await crypto.subtle.encrypt(
-        { name: 'AES-GCM', iv },
-        key,
-        encodedPlaintext
-      ));
-
-      const contentBuffer = new Uint8Array(salt.byteLength + iv.byteLength + ciphertext.byteLength);
-      contentBuffer.set(salt);
-      contentBuffer.set(iv, salt.byteLength);
-      contentBuffer.set(ciphertext, salt.byteLength + iv.byteLength);
-      const content = btoa(String.fromCharCode(...contentBuffer));
-
-      return content;
-    }
-
     function downloadHtml(htmlString, fileName) {
       const encodedHtml = new TextEncoder().encode(htmlString);
       const file = new File([encodedHtml], fileName, { type: 'text/html' });
This page took 0.033661 seconds and 4 git commands to generate.