From 77f1562a30807cfc3b74f03e19c5cbe5d7c0fe90 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 26 Nov 2024 13:49:08 +0000 Subject: [PATCH] webcrypto: improve managedNonce --- src/webcrypto.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/webcrypto.ts b/src/webcrypto.ts index a247c0c..4ae0d34 100644 --- a/src/webcrypto.ts +++ b/src/webcrypto.ts @@ -44,10 +44,10 @@ type CipherWithNonce = ((key: Uint8Array, nonce: Uint8Array, ...args: any[]) => // Uses CSPRG for nonce, nonce injected in ciphertext export function managedNonce(fn: T): RemoveNonce { - anumber(fn.nonceLength); + const { nonceLength } = fn; + anumber(nonceLength); return ((key: Uint8Array, ...args: any[]): any => ({ encrypt(plaintext: Uint8Array, ...argsEnc: any[]) { - const { nonceLength } = fn; const nonce = randomBytes(nonceLength); const ciphertext = (fn(key, nonce, ...args).encrypt as any)(plaintext, ...argsEnc); const out = concatBytes(nonce, ciphertext); @@ -55,7 +55,6 @@ export function managedNonce(fn: T): RemoveNonce { return out; }, decrypt(ciphertext: Uint8Array, ...argsDec: any[]) { - const { nonceLength } = fn; const nonce = ciphertext.subarray(0, nonceLength); const data = ciphertext.subarray(nonceLength); return (fn(key, nonce, ...args).decrypt as any)(data, ...argsDec);