paulmillr/noble-ciphers

Provide a wrapper function that would prepend nonce to ciphertext

Closed this issue · 0 comments

Nonce management is messy, and could be simplified:

import { xchacha20poly1305 } from '@noble/ciphers/chacha';
import { utf8ToBytes } from '@noble/ciphers/utils';
import { randomBytes, usePrependedRandomNonce } from '@noble/ciphers/webcrypto/utils';

const key = randomBytes(32);
const data = utf8ToBytes('hello, noble'); // strings must be converted to Uint8Array
const stream_x = usePrependedRandomNonce(xchacha20poly1305)(key);
const ciphertext = stream_x.encrypt(data);
const plaintext = stream_x.decrypt(ciphertext);

It would auto-generate nonce from csprng with proper byte length per-cipher.

The nonce would be prepended to ciphertext then.