]> git.proxmox.com Git - proxmox-backup.git/blame - examples/cipherbench.rs
update to first proxmox crate split
[proxmox-backup.git] / examples / cipherbench.rs
CommitLineData
6ef1b649 1use anyhow::Error;
9399c98f
DM
2
3// chacha20-poly1305
4
0f375770 5fn rate_test(name: &str, bench: &dyn Fn() -> usize) {
9399c98f 6
4e8cff1c
TL
7 print!("{:<20} ", name);
8
9399c98f
DM
9 let start = std::time::SystemTime::now();
10 let duration = std::time::Duration::new(1, 0);
11
12 let mut bytes = 0;
13
14 loop {
15 bytes += bench();
16 let elapsed = start.elapsed().unwrap();
17 if elapsed > duration { break; }
18 }
19
20 let elapsed = start.elapsed().unwrap();
21 let elapsed = (elapsed.as_secs() as f64) +
22 (elapsed.subsec_millis() as f64)/1000.0;
23
4e8cff1c 24 println!("{:>8.1} MB/s", (bytes as f64)/(elapsed*1024.0*1024.0));
9399c98f
DM
25}
26
27
28fn main() -> Result<(), Error> {
29
30 let input = proxmox::sys::linux::random_data(1024*1024)?;
9399c98f 31
a7f67a9a
DM
32 rate_test("crc32", &|| {
33 let mut crchasher = crc32fast::Hasher::new();
34 crchasher.update(&input);
35 let _checksum = crchasher.finalize();
36 input.len()
37 });
38
0f375770
DM
39 rate_test("zstd", &|| {
40 zstd::block::compress(&input, 1).unwrap();
41 input.len()
42 });
9399c98f
DM
43
44 rate_test("sha256", &|| {
45 openssl::sha::sha256(&input);
46 input.len()
47 });
48
0f375770
DM
49 let key = proxmox::sys::linux::random_data(32)?;
50
51 let iv = proxmox::sys::linux::random_data(16)?;
52
9399c98f
DM
53 let cipher = openssl::symm::Cipher::aes_256_gcm();
54
55 rate_test("aes-256-gcm", &|| {
56 let mut tag = [0u8;16];
57 openssl::symm::encrypt_aead(
58 cipher,
59 &key,
60 Some(&iv),
61 b"",
62 &input,
63 &mut tag).unwrap();
64 input.len()
65 });
66
67 let cipher = openssl::symm::Cipher::chacha20_poly1305();
68
69 rate_test("chacha20-poly1305", &|| {
70 let mut tag = [0u8;16];
71 openssl::symm::encrypt_aead(
72 cipher,
73 &key,
4832f53a 74 Some(&iv[..12]),
9399c98f
DM
75 b"",
76 &input,
77 &mut tag).unwrap();
78 input.len()
79 });
80
81 Ok(())
82}