]> git.ardis.lu - static-encrypt-passkey/commitdiff
Refactor to avoid spread operator
authorArdis Lu <ardisglu@gmail.com>
Wed, 23 Jul 2025 04:57:27 +0000 (21:57 -0700)
committerArdis Lu <ardisglu@gmail.com>
Wed, 23 Jul 2025 04:57:27 +0000 (21:57 -0700)
Spread operator will get stack overflow on large inputs. Can simplify in the future with `Uint8Array.prototype.toBase64()` once widely available.

script.js

index 684840bd7523635ad19899749e7e871aeb3b8200..75c442c5c03bef7040ab99cf7ea5e8329183937d 100644 (file)
--- a/script.js
+++ b/script.js
@@ -98,8 +98,15 @@ export async function encrypt(plaintext) {
     paddedPlaintext
   ));
 
-  const buffer = Uint8Array.from([...salt, ...iv, ...ciphertext]);
-  const content = btoa(String.fromCharCode(...buffer));
+  const buffer = new Uint8Array(salt.byteLength + iv.byteLength + ciphertext.byteLength);
+  buffer.set(salt);
+  buffer.set(iv, salt.byteLength);
+  buffer.set(ciphertext, salt.byteLength + iv.byteLength);
+  let binary = '';
+  for (let i = 0; i < buffer.byteLength; i++) {
+    binary += String.fromCharCode(buffer[i]);
+  }
+  const content = btoa(binary);
 
   return content;
 }
This page took 0.036211 seconds and 4 git commands to generate.