Skip to content

Encryption & PQC

cipherlib

Symmetric ciphers, AEAD and post-quantum ML-KEM, implemented in pure Dart.

A pure-Dart cryptography library: modern stream ciphers, authenticated encryption, a full AES mode set, and post-quantum cryptography with no native bindings and a single dependency.

Highlights

Runs on every platform

Pure Dart with no native code or FFI, so the same library works everywhere Dart does: the VM, Flutter (Android, iOS, Windows, macOS, Linux), and the web (dart2js and dart2wasm).

Authenticated by default

AES-GCM, AES-CCM, and a Poly1305 variant of ChaCha20, XChaCha20, Salsa20, and XSalsa20, each with a constant-time tag check that rejects tampered messages.

Broad AES coverage

Ten modes, from ECB and CBC to GCM, CCM, and XTS.

One-shot or streaming

Encrypt a buffer in a single call, or pipe a Stream<List<int>> through any cipher as a StreamTransformer.

Post-quantum ready

ML-KEM-512/768/1024 (FIPS 203) for quantum-resistant key exchange.

Fast

Outruns PointyCastle and cryptography in nearly every benchmark, see the benchmarks below.

Quickstart

Install it, import it, and you are ready. This example comes straight from the README on the repository’s default branch, so it never drifts from the docs.

More recipes in the guide
main.dart
import 'package:cipherlib/cipherlib.dart';
import 'package:cipherlib/codecs.dart';
import 'package:cipherlib/random.dart';

void main() {
  final key = randomBytes(32); // AES-256
  final nonce = randomBytes(12); // Recommended IV size for GCM
  final aad = toUtf8('order-id=INV-1001');
  final plain = toUtf8('Ship 3 units to dock-7');

  final aes = AES(key).gcm(nonce, aad: aad);
  final sealed = aes.encrypt(plain);
  final opened = aes.decrypt(sealed);

  print('AES-256-GCM');
  print('key   : ${toHex(key)}');
  print('nonce : ${toHex(nonce)}');
  print('aad   : ${fromUtf8(aad)}');
  print('cipher: ${toHex(sealed)}');
  print('plain : ${fromUtf8(opened)}');
}

Performance

Throughput against PointyCastle, cryptography, and pqcrypto at a typical input size (shown on each card). Higher is better.

142/144

head-to-head benchmarks won (99%)

ChaCha201KB message
  • cipherlib2 Gbps
  • PointyCastle348 Mbps

5.7× the fastest alternative

ChaCha20 / Poly13051KB message
  • cipherlib1.26 Gbps
  • cryptography258 Mbps
  • PointyCastle256 Mbps

4.9× the fastest alternative

Salsa201KB message
  • cipherlib2.13 Gbps
  • PointyCastle344 Mbps

6.2× the fastest alternative

AES-256 / GCM1KB message
  • cipherlib333 Mbps
  • cryptography229 Mbps
  • PointyCastle15.25 Mbps

1.45× the fastest alternative

AES-256 / CTR1KB message
  • cipherlib1.11 Gbps
  • cryptography513 Mbps
  • PointyCastle154 Mbps

2.2× the fastest alternative

ML-KEM-768Encapsulation
  • cipherlib183 Mbps
  • pqcrypto19.13 Mbps

9.6× the fastest alternative

All benchmarks are done on 36GB Apple M3 Pro using compiled exe, Dart 3.12.2.Explore all benchmarks →

Supported algorithms

Block ciphers

Algorithm Public class and methods Source
AES AES NIST.FIPS.197
Blowfish Blowfish Blowfish-1993
Twofish Twofish Twofish-1998

AES supports 10 modes:

  • ECB - Electronic Codebook
  • CBC - Cipher Block Chaining
  • CTR - Counter
  • GCM - Galois/Counter Mode (authenticated)
  • CCM - Counter with CBC-MAC (authenticated)
  • CFB - Cipher Feedback
  • OFB - Output Feedback
  • IGE - Infinite Garble Extension
  • PCBC - Propagating Cipher Block Chaining
  • XTS - XEX Tweakable Block Cipher with Ciphertext Stealing

Blowfish and Twofish support 3 modes:

  • ECB - Electronic Codebook
  • CBC - Cipher Block Chaining
  • CTR - Counter

Stream ciphers

Algorithm Public class and methods Source
XOR XOR, xor Wikipedia
ChaCha20 ChaCha20, chacha20 RFC-8439
XChaCha20 XChaCha20, xchacha20 libsodium
Salsa20 Salsa20, salsa20 Snuffle-2005
XSalsa20 XSalsa20, xsalsa20 libsodium

Authenticated encryption (AEAD)

Algorithm Public class and methods Source
ChaCha20/Poly1305 ChaCha20Poly1305, chacha20poly1305 RFC-8439
XChaCha20/Poly1305 XChaCha20Poly1305, xchacha20poly1305 libsodium
Salsa20/Poly1305 Salsa20Poly1305, salsa20poly1305 Snuffle-2005
XSalsa20/Poly1305 XSalsa20Poly1305, xsalsa20poly1305 libsodium

AES adds authenticated encryption through its GCM and CCM modes.

Post-Quantum Cryptography

Algorithm Public class and methods Source
ML-KEM MLKEM NIST.FIPS.203

MLKEM.kem512(), MLKEM.kem768(), and MLKEM.kem1024() provide key generation, encapsulation, and decapsulation, with optional deterministic key generation from a 64-byte seed.

What's new in 0.7.2

Changelog →
  • 🐛 Fix AES-GCM producing wrong ciphertext on dart2wasm for messages of about 1 MiB or larger (#28). The block counter is stored in a Uint8List view over another buffer, and dart2wasm skips the modulo-256 truncation when such a view is assigned an out-of-range value, so counter[i]++ let the carry bleed into the neighbouring byte. From the first 32-bit carry onward the keystream desynchronised, silently breaking interoperability with every other platform (and with other AES-GCM implementations). All stored counter bytes are now masked explicitly.
  • ✅ The inc32 counter behaviour is now pinned against raw AES-ECB at every byte-carry boundary, plus an end-to-end check across the 1 MiB boundary, and the dart2wasm suite runs on every push instead of only at release time.
  • ✅ New cross-cutting test/counter_boundary_test.dart asserts counter continuity — block j of a run started at counter C equals block 0 of a run started at C + j — for AES/Twofish/Blowfish CTR, ChaCha20 (32- and 64-bit counters), XChaCha20, Salsa20 and XSalsa20. Because the starting counter is an input, this reaches the 32- and 64-bit carries that no achievable message length could walk to, alongside a 1 MiB run per cipher.
  • Improve the benchmark harness (benchmark/_base.dart): it now reports the median per-iteration time sampled across ~25ms batches instead of the arithmetic mean, making results robust against GC pauses and keeping each batch well above the coarsened web timer resolution. Benchmark tooling only; no library code or output is affected.

0.7.20.7.10.7.00.6.00.5.00.4.0

Part of the family