-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.rs
182 lines (151 loc) · 4.76 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use anyhow::anyhow;
use chacha20poly1305::{
aead::{stream, Aead, NewAead},
XChaCha20Poly1305,
};
use rand::{rngs::OsRng, RngCore};
use std::{
fs::{self, File},
io::{Read, Write},
};
fn main() -> Result<(), anyhow::Error> {
let mut small_file_key = [0u8; 32];
let mut small_file_nonce = [0u8; 24];
OsRng.fill_bytes(&mut small_file_key);
OsRng.fill_bytes(&mut small_file_nonce);
println!("Encrypting 100.bin to 100.encrypted");
encrypt_small_file(
"100.bin",
"100.encrypted",
&small_file_key,
&small_file_nonce,
)?;
println!("Decrypting 100.encrypted to 100.decrypted");
decrypt_small_file(
"100.encrypted",
"100.decrypted",
&small_file_key,
&small_file_nonce,
)?;
let mut large_file_key = [0u8; 32];
let mut large_file_nonce = [0u8; 19];
OsRng.fill_bytes(&mut large_file_key);
OsRng.fill_bytes(&mut large_file_nonce);
println!("Encrypting 2048.bin to 2048.encrypted");
encrypt_large_file(
"2048.bin",
"2048.encrypted",
&large_file_key,
&large_file_nonce,
)?;
println!("Decrypting 2048.encrypted to 2048.decrypted");
decrypt_large_file(
"2048.encrypted",
"2048.decrypted",
&large_file_key,
&large_file_nonce,
)?;
println!("Encrypting 500.bin to 500.encrypted");
encrypt_large_file(
"500.bin",
"500.encrypted",
&large_file_key,
&large_file_nonce,
)?;
println!("Decrypting 500.encrypted to 500.decrypted");
decrypt_large_file(
"500.encrypted",
"500.decrypted",
&large_file_key,
&large_file_nonce,
)?;
Ok(())
}
fn encrypt_small_file(
filepath: &str,
dist: &str,
key: &[u8; 32],
nonce: &[u8; 24],
) -> Result<(), anyhow::Error> {
let cipher = XChaCha20Poly1305::new(key.into());
let file_data = fs::read(filepath)?;
let encrypted_file = cipher
.encrypt(nonce.into(), file_data.as_ref())
.map_err(|err| anyhow!("Encrypting small file: {}", err))?;
fs::write(&dist, encrypted_file)?;
Ok(())
}
fn decrypt_small_file(
encrypted_file_path: &str,
dist: &str,
key: &[u8; 32],
nonce: &[u8; 24],
) -> Result<(), anyhow::Error> {
let cipher = XChaCha20Poly1305::new(key.into());
let file_data = fs::read(encrypted_file_path)?;
let decrypted_file = cipher
.decrypt(nonce.into(), file_data.as_ref())
.map_err(|err| anyhow!("Decrypting small file: {}", err))?;
fs::write(&dist, decrypted_file)?;
Ok(())
}
fn encrypt_large_file(
source_file_path: &str,
dist_file_path: &str,
key: &[u8; 32],
nonce: &[u8; 19],
) -> Result<(), anyhow::Error> {
let aead = XChaCha20Poly1305::new(key.as_ref().into());
let mut stream_encryptor = stream::EncryptorBE32::from_aead(aead, nonce.as_ref().into());
const BUFFER_LEN: usize = 500;
let mut buffer = [0u8; BUFFER_LEN];
let mut source_file = File::open(source_file_path)?;
let mut dist_file = File::create(dist_file_path)?;
loop {
let read_count = source_file.read(&mut buffer)?;
if read_count == BUFFER_LEN {
let ciphertext = stream_encryptor
.encrypt_next(buffer.as_slice())
.map_err(|err| anyhow!("Encrypting large file: {}", err))?;
dist_file.write(&ciphertext)?;
} else {
let ciphertext = stream_encryptor
.encrypt_last(&buffer[..read_count])
.map_err(|err| anyhow!("Encrypting large file: {}", err))?;
dist_file.write(&ciphertext)?;
break;
}
}
Ok(())
}
fn decrypt_large_file(
encrypted_file_path: &str,
dist: &str,
key: &[u8; 32],
nonce: &[u8; 19],
) -> Result<(), anyhow::Error> {
let aead = XChaCha20Poly1305::new(key.as_ref().into());
let mut stream_decryptor = stream::DecryptorBE32::from_aead(aead, nonce.as_ref().into());
const BUFFER_LEN: usize = 500 + 16;
let mut buffer = [0u8; BUFFER_LEN];
let mut encrypted_file = File::open(encrypted_file_path)?;
let mut dist_file = File::create(dist)?;
loop {
let read_count = encrypted_file.read(&mut buffer)?;
if read_count == BUFFER_LEN {
let plaintext = stream_decryptor
.decrypt_next(buffer.as_slice())
.map_err(|err| anyhow!("Decrypting large file: {}", err))?;
dist_file.write(&plaintext)?;
} else if read_count == 0 {
break;
} else {
let plaintext = stream_decryptor
.decrypt_last(&buffer[..read_count])
.map_err(|err| anyhow!("Decrypting large file: {}", err))?;
dist_file.write(&plaintext)?;
break;
}
}
Ok(())
}