[API reference](https://evolu.dev/docs/api-reference) › [@evolu/common](https://evolu.dev/docs/api-reference/common) › [Crypto](https://evolu.dev/docs/api-reference/common/Crypto) › encryptWithXChaCha20Poly1305

```ts
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](https://github.com/evoluhq/evolu/blob/e7144e2bbe9069362b62dec1b64a8aa922b8f1b0/packages/common/src/Crypto.ts#L173)

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

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

## See

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