Crates.io | chacha20-poly1305-stream |
lib.rs | chacha20-poly1305-stream |
version | 0.1.1 |
source | src |
created_at | 2023-10-22 03:40:27.435643 |
updated_at | 2023-10-22 09:03:32.042388 |
description | A pure Rust implementation of the ChaCha20-Poly1305 AEAD from RFC 7539. |
homepage | |
repository | https://git.hatter.ink/hatter/chacha20-poly1305-stream |
max_upload_size | |
id | 1010374 |
size | 87,888 |
ChaCha20 Poly1305 stream encrypt and decrypt library
// IMPORTANT! key and nonce SHOULD generate by random
let key = [0u8; 32];
let nonce = [0; 12];
let mut encryptor = ChaCha20Poly1305StreamEncryptor::new(&key, &nonce).unwrap();
let mut ciphertext = vec![];
ciphertext.extend_from_slice(&encryptor.update(b"Hello "));
ciphertext.extend_from_slice(&encryptor.update(b" World"));
ciphertext.extend_from_slice(&encryptor.update(b"!"));
let (last_block, tag) = encryptor.finalize();
ciphertext.extend_from_slice(&last_block);
ciphertext.extend_from_slice(&tag);
println!("Ciphertext: {}", hex::encode(&ciphertext));
$ cargo run --example encrypt_and_decrypt
Finished dev [unoptimized + debuginfo] target(s) in 0.19s
Running `target/debug/examples/encrypt_and_decrypt`
Ciphertext: d7628bd23a71182df7c8fb1852d3dc42b88a61e2fce340d9c5b323884d
Plaintext : Hello World!
Benchmark @MacBook Pro (Retina, 15-inch, Late 2013/2 GHz Quad-Core Intel Core i7)
$ cargo r --release --example bench
ChaCha20Poly1305 encrypt : 287.06 M/s
ChaCha20Poly1305 encrypt/decrypt : 144.93 M/s