From: Ardis Lu Date: Wed, 23 Jul 2025 04:57:27 +0000 (-0700) Subject: Refactor to avoid spread operator X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=a268a8bcc9ac0c2f6fc6daf1e6bd4d6b25258ba4;p=static-encrypt-passkey Refactor to avoid spread operator Spread operator will get stack overflow on large inputs. Can simplify in the future with `Uint8Array.prototype.toBase64()` once widely available. --- diff --git a/script.js b/script.js index 684840b..75c442c 100644 --- 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; }