chacha20-poly1305-stream

Crates.iochacha20-poly1305-stream
lib.rschacha20-poly1305-stream
version0.1.1
sourcesrc
created_at2023-10-22 03:40:27.435643
updated_at2023-10-22 09:03:32.042388
descriptionA pure Rust implementation of the ChaCha20-Poly1305 AEAD from RFC 7539.
homepage
repositoryhttps://git.hatter.ink/hatter/chacha20-poly1305-stream
max_upload_size
id1010374
size87,888
Hatter Jiang (jht5945)

documentation

README

chacha20-poly1305-stream

ChaCha20 Poly1305 stream encrypt and decrypt library

Encrypt

// 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));

Run Example

$ 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
Commit count: 0

cargo fmt