forked from xoriors/rencfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_speed.rs
160 lines (143 loc) · 4.47 KB
/
crypto_speed.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::env::args;
use std::fs::File;
use std::io::{Read, Seek, Write};
use std::path::Path;
use std::sync::Arc;
use std::time::Instant;
use std::{fs, io};
use anyhow::Result;
use rand_core::RngCore;
use shush_rs::SecretVec;
use rencfs::crypto;
use rencfs::crypto::write::{CryptoInnerWriter, CryptoWrite};
use rencfs::crypto::Cipher;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt().init();
let cipher = Cipher::ChaCha20Poly1305;
let key = Arc::new(get_key(cipher)?);
println!("chacha\n");
let mut args = args();
let _ = args.next(); // skip the program name
let path_in = args.next().expect("path_in is missing");
let path_out = format!(
"/tmp/{}.enc",
Path::new(&path_in).file_name().unwrap().to_str().unwrap()
);
let out = Path::new(&path_out).to_path_buf();
if out.exists() {
fs::remove_file(&out)?;
}
stream_speed(&path_in, &path_out, cipher, &key)?;
println!();
file_speed(&path_in, &path_out, cipher, &key)?;
let cipher = Cipher::Aes256Gcm;
let key = Arc::new(get_key(cipher)?);
println!("\naesgcm\n");
stream_speed(&path_in, &path_out, cipher, &key)?;
println!();
file_speed(&path_in, &path_out, cipher, &key)?;
Ok(())
}
fn speed<F>(f: F, label: &str, size: u64) -> io::Result<()>
where
F: FnOnce() -> io::Result<()>,
{
let start = Instant::now();
f()?;
let duration = start.elapsed();
println!(
"{label} duration = {:?}, speed MB/s {:.2}",
duration,
(size as f64 / duration.as_secs_f64()) / 1024.0 / 1024.0
);
Ok(())
}
fn check_hash(r1: &mut impl Read, r2: &mut (impl Read + ?Sized)) -> Result<()> {
let hash1 = crypto::hash_reader(r1)?;
let hash2 = crypto::hash_reader(r2)?;
assert_eq!(hash1, hash2);
Ok(())
}
fn stream_speed(
path_in: &str,
path_out: &str,
cipher: Cipher,
key: &Arc<SecretVec<u8>>,
) -> Result<()> {
println!("stream speed");
let _ = fs::remove_file(path_out);
let mut file_in = File::open(path_in)?;
let file_out = File::create(path_out)?;
let path_out2 = Path::new(&path_out).to_path_buf().with_extension("dec");
let _ = fs::remove_file(path_out2.clone());
let mut file_out2 = File::create(path_out2.clone())?;
let mut writer = crypto::create_write(file_out, cipher, key);
let size = file_in.metadata()?.len();
let f = || crypto::create_read(File::open(path_out).unwrap(), cipher, key);
test_speed(&mut file_in, &mut writer, &mut file_out2, size, f)?;
file_in.seek(io::SeekFrom::Start(0))?;
check_hash(&mut file_in, &mut f())?;
fs::remove_file(path_out)?;
fs::remove_file(path_out2)?;
Ok(())
}
fn file_speed(path_in: &str, path_out: &str, cipher: Cipher, key: &SecretVec<u8>) -> Result<()> {
println!("file speed");
let _ = fs::remove_file(path_out);
let mut file_in = File::open(path_in)?;
let mut writer = crypto::create_write(File::create(Path::new(path_out))?, cipher, key);
let path_out2 = Path::new(&path_out).to_path_buf().with_extension("dec");
let _ = fs::remove_file(path_out2.clone());
let mut file_out2 = File::create(path_out2.clone())?;
let size = file_in.metadata()?.len();
let f = || crypto::create_read(File::open(path_out).unwrap(), cipher, key);
test_speed(&mut file_in, &mut writer, &mut file_out2, size, f)?;
file_in.seek(io::SeekFrom::Start(0)).unwrap();
check_hash(&mut file_in, &mut f())?;
fs::remove_file(path_out)?;
fs::remove_file(path_out2)?;
Ok(())
}
fn test_speed<W: CryptoInnerWriter + Send + Sync, R: Read + Send + Sync, FR>(
r: &mut impl Read,
w: &mut (impl CryptoWrite<W> + ?Sized),
w2: &mut impl Write,
size: u64,
r2: FR,
) -> io::Result<()>
where
FR: FnOnce() -> R,
{
let mut r = io::BufReader::new(r);
let mut w = io::BufWriter::new(w);
speed(
|| {
io::copy(&mut r, &mut w)?;
w.into_inner()
.map_err(|err| {
let (err, _) = err.into_parts();
err
})?
.finish()?;
Ok(())
},
"write",
size,
)?;
speed(
|| {
io::copy(&mut r2(), w2)?;
w2.flush()?;
Ok(())
},
"read",
size,
)?;
Ok(())
}
fn get_key(cipher: Cipher) -> io::Result<SecretVec<u8>> {
let mut key = vec![0; cipher.key_len()];
crypto::create_rng().fill_bytes(key.as_mut_slice());
Ok(SecretVec::from(key))
}