API reference@evolu/commonCrypto › encryptWithXChaCha20Poly1305

function encryptWithXChaCha20Poly1305(
  deps: RandomBytesDep,
): (
  plaintext: Uint8Array,
  encryptionKey: Uint8Array<ArrayBufferLike> &
    Brand<"Entropy"> &
    Brand<"Length32"> &
    Brand<"EncryptionKey">,
) => [
  Uint8Array<ArrayBufferLike> & Brand<"XChaCha20Poly1305Ciphertext">,
  Uint8Array<ArrayBufferLike> & Brand<"Entropy"> & Brand<"Length24">,
];

Defined in: packages/common/src/Crypto.ts:173

Encrypts plaintext with XChaCha20-Poly1305.

Generates a random nonce internally and returns both the ciphertext and nonce. The nonce must be stored alongside the ciphertext for decryption.

Example

const deps = { randomBytes: createRandomBytes() };
const [ciphertext, nonce] = encryptWithXChaCha20Poly1305(deps)(
  utf8ToBytes("secret message"),
  encryptionKey,
);

See

https://github.com/paulmillr/noble-ciphers