2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{Error};
|
2019-06-13 15:16:43 +00:00
|
|
|
|
|
|
|
// chacha20-poly1305
|
|
|
|
|
2019-06-13 15:24:57 +00:00
|
|
|
fn rate_test(name: &str, bench: &dyn Fn() -> usize) {
|
2019-06-13 15:16:43 +00:00
|
|
|
|
2019-12-17 07:49:34 +00:00
|
|
|
print!("{:<20} ", name);
|
|
|
|
|
2019-06-13 15:16:43 +00:00
|
|
|
let start = std::time::SystemTime::now();
|
|
|
|
let duration = std::time::Duration::new(1, 0);
|
|
|
|
|
|
|
|
let mut bytes = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
bytes += bench();
|
|
|
|
let elapsed = start.elapsed().unwrap();
|
|
|
|
if elapsed > duration { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
let elapsed = start.elapsed().unwrap();
|
|
|
|
let elapsed = (elapsed.as_secs() as f64) +
|
|
|
|
(elapsed.subsec_millis() as f64)/1000.0;
|
|
|
|
|
2019-12-17 07:49:34 +00:00
|
|
|
println!("{:>8.1} MB/s", (bytes as f64)/(elapsed*1024.0*1024.0));
|
2019-06-13 15:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
|
|
|
|
let input = proxmox::sys::linux::random_data(1024*1024)?;
|
|
|
|
|
2019-06-21 13:58:00 +00:00
|
|
|
rate_test("crc32", &|| {
|
|
|
|
let mut crchasher = crc32fast::Hasher::new();
|
|
|
|
crchasher.update(&input);
|
|
|
|
let _checksum = crchasher.finalize();
|
|
|
|
input.len()
|
|
|
|
});
|
|
|
|
|
2019-06-13 15:24:57 +00:00
|
|
|
rate_test("zstd", &|| {
|
|
|
|
zstd::block::compress(&input, 1).unwrap();
|
|
|
|
input.len()
|
|
|
|
});
|
2019-06-13 15:16:43 +00:00
|
|
|
|
|
|
|
rate_test("sha256", &|| {
|
|
|
|
openssl::sha::sha256(&input);
|
|
|
|
input.len()
|
|
|
|
});
|
|
|
|
|
2019-06-13 15:24:57 +00:00
|
|
|
let key = proxmox::sys::linux::random_data(32)?;
|
|
|
|
|
|
|
|
let iv = proxmox::sys::linux::random_data(16)?;
|
|
|
|
|
2019-06-13 15:16:43 +00:00
|
|
|
let cipher = openssl::symm::Cipher::aes_256_gcm();
|
|
|
|
|
|
|
|
rate_test("aes-256-gcm", &|| {
|
|
|
|
let mut tag = [0u8;16];
|
|
|
|
openssl::symm::encrypt_aead(
|
|
|
|
cipher,
|
|
|
|
&key,
|
|
|
|
Some(&iv),
|
|
|
|
b"",
|
|
|
|
&input,
|
|
|
|
&mut tag).unwrap();
|
|
|
|
input.len()
|
|
|
|
});
|
|
|
|
|
|
|
|
let cipher = openssl::symm::Cipher::chacha20_poly1305();
|
|
|
|
|
|
|
|
rate_test("chacha20-poly1305", &|| {
|
|
|
|
let mut tag = [0u8;16];
|
|
|
|
openssl::symm::encrypt_aead(
|
|
|
|
cipher,
|
|
|
|
&key,
|
2019-12-17 07:45:20 +00:00
|
|
|
Some(&iv[..12]),
|
2019-06-13 15:16:43 +00:00
|
|
|
b"",
|
|
|
|
&input,
|
|
|
|
&mut tag).unwrap();
|
|
|
|
input.len()
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|