Skip to content

Commit b620018

Browse files
committed
Encrypt a folder using zip folder and encrypt file functions
1 parent 3e0c448 commit b620018

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/main.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@ use zip::{
1010
};
1111

1212
fn main() {
13+
let key = "password12345678password12345678".as_bytes();
1314
let encrypted_file_path = "sample-file.txt.encrypted";
1415

15-
encrypt_file(
16-
"sample-file.txt",
17-
"password12345678password12345678".as_bytes(),
18-
);
16+
encrypt_file("sample-file.txt", &key);
1917

20-
decrypt_file(
21-
encrypted_file_path,
22-
"password12345678password12345678".as_bytes(),
23-
);
18+
decrypt_file(encrypted_file_path, &key);
2419

25-
zip_folder("vault", "vault.zip");
20+
encrypt_folder("vault", &key);
2621
}
2722

2823
type Aes256Cbc = Cbc<Aes256, Pkcs7>;
@@ -93,3 +88,10 @@ fn zip_folder(folder_path: &str, output_path: &str) {
9388

9489
zip.finish().expect("Failed to finalize the zip file");
9590
}
91+
92+
fn encrypt_folder(folder_path: &str, key: &[u8]) {
93+
let zip_path = format!("{}.zip", folder_path);
94+
zip_folder(folder_path, &zip_path);
95+
encrypt_file(&zip_path, key);
96+
fs::remove_file(&zip_path).expect("Failed to remove the intermediate zip file :|");
97+
}

0 commit comments

Comments
 (0)