rusty_chacha 0.7.2 rusty_chacha: ^0.7.2 copied to clipboard
A Flutter library for fast ChaCha20-Poly1305 encryption, powered by Rust
Rusty ChaCha 💃🦀 #
A Flutter library for fast ChaCha20-Poly1305 encryption, leveraging the capabilities of the Rust chacha20poly1305 crate.
🚧 Under Development: Not recommended for production use yet. 🚧
Features #
- Encrypt and decrypt data with ChaCha20-Poly1305 (authenticated)
- Additional authenticated data (AAD)
- Optional compression using zstd
Blazingly fast 🔥 #
Thanks to Rust encryption and decryption with ChaCha20-Poly1305 run at 500-1000 MiB/s. This is up to 50x faster than packages like cryptography_flutter or pointycastle.
Getting Started #
- With Flutter, run
flutter pub add rusty_chacha
Usage #
import 'dart:typed_data';
import 'package:collection/collection.dart';
import 'package:rusty_chacha/rusty_chacha.dart';
main() async {
final data = Uint8List.fromList([1, 2, 3, 4, 5]);
// Create and use a ChaCha20Poly1305 cipher with a random key:
RustyChaCha20Poly1305 cipher = await RustyChaCha.create();
Uint8List myEncryptedData = await cipher.encrypt(cleartext: data);
Uint8List decrypted1 = await cipher.decrypt(ciphertext: myEncryptedData);
assert(const ListEquality().equals(data, decrypted1));
// Or with explicit key:
Uint8List key = await RustyChaCha20Poly1305.generateKey();
cipher = await RustyChaCha.create(key: key);
// Compression example:
// If compression is used during encryption, it also has to be set for decryption!
// However compressionLevel does not matter.
cipher = await RustyChaCha.create(
key: key,
compression: const Compression.zstd(compressionLevel: 3), // moderate compression
);
Uint8List myCompressedAndEncryptedData = await cipher.encrypt(cleartext: data);
Uint8List decrypted2 = await cipher.decrypt(ciphertext: myCompressedAndEncryptedData);
assert(const ListEquality().equals(data, decrypted2));
// AAD example:
cipher = await RustyChaCha.create();
Uint8List additionalData = Uint8List.fromList([1, 2, 3]); // some additional (non-secret) data
Uint8List myEncryptedDataWithAad = await cipher.encrypt(
cleartext: data,
aad: additionalData, // pass it in when encrypting
);
Uint8List decrypted3 = await cipher.decrypt(
ciphertext: myEncryptedDataWithAad,
aad: additionalData, // pass it in to decrypt (decrypt will fail if additionalData is not the same)
);
assert(const ListEquality().equals(data, decrypted3));
// Create and use a XChaCha20Poly1305 with an extended 192-bit (24-byte) nonce instead of 96-bit (12-byte):
RustyXChaCha20Poly1305 cipherX = await RustyChaCha.createX();
Uint8List myEncryptedData2 = await cipher.encrypt(cleartext: data);
Uint8List decrypted4 = await cipher.decrypt(ciphertext: myEncryptedData2);
assert(const ListEquality().equals(data, decrypted4));
}
Supported platforms (for now) #
- iOS
- MacOS
- Windows
- Linux
Thanks to #
- RustCrypto for creating the chacha20poly1305 crate
- @fzyzcjy for the amazing flutter_rust_bridge
- Mimir for providing a project template.