[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:255](https://github.com/evoluhq/evolu/blob/dd96d79f1dbe9a49fa12ce8e0aa7d3d0177795ca/packages/common/src/Crypto.ts#L255)

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
import {
  assertEqual,
  assertType,
  createRandomBytes,
  EncryptionKey,
  encryptWithXChaCha20Poly1305,
  type XChaCha20Poly1305Ciphertext,
  utf8ToBytes,
} from "@evolu/common";

const plaintext = utf8ToBytes("secret message");
const encryptionKey = EncryptionKey.orThrow(new Uint8Array(32).fill(7));
const [ciphertext, nonce] = encryptWithXChaCha20Poly1305({
  randomBytes: createRandomBytes(),
})(plaintext, encryptionKey);

assertType<typeof ciphertext, XChaCha20Poly1305Ciphertext>();
assertEqual(nonce.length, 24);
```

## See

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