2个版本
0.1.1 | 2023年10月22日 |
---|---|
0.1.0 | 2023年10月22日 |
#11 in #chacha20
在tiny-encrypt中使用
62KB
1.5K SLoC
chacha20-poly1305-stream
ChaCha20 Poly1305流加密和解密库
加密
// 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!
基准测试 @MacBook Pro(Retina,15英寸,2013年后期/2 GHz四核Intel Core i7)
$ cargo r --release --example bench
ChaCha20Poly1305 encrypt : 287.06 M/s
ChaCha20Poly1305 encrypt/decrypt : 144.93 M/s
依赖项
~30–260KB