Skip to content

Commit 017f47a

Browse files
committed
lil fake progressbar
1 parent 59b7369 commit 017f47a

File tree

4 files changed

+157
-70
lines changed

4 files changed

+157
-70
lines changed

Cargo.lock

+66
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ keywords = ["encryption", "AES", "Scrypt", "file encryption", "security"]
1313
aes-gcm = "0.10.3"
1414
bincode = "1.3.3"
1515
clap = { version = "4.5.17", features = ["derive"] }
16+
indicatif = "0.17.8"
1617
patharg = "0.4.0"
1718
rprompt = "2.1.1"
1819
scrypt = "0.11.0"

src/lib.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use aes_gcm::{
2+
aead::{Aead, AeadCore, KeyInit},
3+
Aes256Gcm, Nonce, Key
4+
};
5+
use typenum;
6+
use rprompt;
7+
use std::fs;
8+
use scrypt::{
9+
password_hash::{
10+
rand_core::OsRng,
11+
PasswordHasher, SaltString
12+
},
13+
Scrypt,
14+
Params
15+
};
16+
17+
pub fn encrypt(content: Vec<u8>, key: &Key<Aes256Gcm>) -> Result<(Vec<u8>, Nonce<typenum::U12>), Box<dyn std::error::Error>> {
18+
let cipher = Aes256Gcm::new(key);
19+
let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
20+
21+
let ciphertext = cipher.encrypt(&nonce, &*content.as_ref()).map_err(|e| format!("Encryption failed: {}", e))?;
22+
23+
Ok((ciphertext, nonce))
24+
}
25+
26+
pub fn decrypt(encrypted_content: Vec<u8>, key: &Key<Aes256Gcm>) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
27+
let (ciphertext, nonce_vec): (Vec<u8>, Vec<u8>) = bincode::deserialize(&encrypted_content)?;
28+
let nonce = Nonce::from_slice(&nonce_vec);
29+
let cipher = Aes256Gcm::new(key);
30+
31+
let decrypted_content = cipher.decrypt(nonce, &*ciphertext.as_ref()).map_err(|e| format!("Decryption failed: {}", e))?;
32+
33+
34+
Ok(decrypted_content)
35+
}
36+
37+
pub fn derive_key(hash: Vec<u8>, salt: Vec<u8>) -> Result<Key<Aes256Gcm>, Box<dyn std::error::Error>> {
38+
let params: Params = Params::new(15, 8, 1, 32)?;
39+
40+
let hash_str = String::from_utf8(hash)?;
41+
let salt_str = String::from_utf8(salt)?;
42+
43+
let mut key_bytes = vec![0u8; 32];
44+
45+
scrypt::scrypt(
46+
hash_str.as_bytes(),
47+
salt_str.as_bytes(),
48+
&params,
49+
&mut key_bytes
50+
)?;
51+
52+
let key = Key::<Aes256Gcm>::from_slice(&key_bytes);
53+
54+
Ok(*key)
55+
}
56+
57+
pub fn generate_hash_and_salt() -> Result<(), Box<dyn std::error::Error>> {
58+
let binding = rprompt::prompt_reply("Enter a password to be hashed: ").unwrap();
59+
let password = binding.as_bytes();
60+
let salt = SaltString::generate(&mut OsRng);
61+
62+
let password_hash = Scrypt.hash_password(password, &salt)?;
63+
64+
fs::write("hash.txt", password_hash.to_string())?;
65+
fs::write("salt.txt", salt.to_string())?;
66+
67+
Ok(())
68+
}

src/main.rs

+22-70
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
use aes_gcm::{
2-
aead::{Aead, AeadCore, KeyInit},
3-
Aes256Gcm, Nonce, Key
4-
};
5-
use std::path::{Path, PathBuf};
1+
use cryptoooor::*;
62
use patharg::InputArg;
7-
use typenum;
83
use clap::Parser;
9-
use rprompt;
104
use std::fs;
11-
use scrypt::{
12-
password_hash::{
13-
rand_core::OsRng,
14-
PasswordHasher, SaltString
15-
},
16-
Scrypt,
17-
Params
18-
};
5+
use std::path::{Path, PathBuf};
6+
use indicatif::ProgressBar;
7+
use std::thread::sleep;
8+
use std::time::Duration;
199

2010
#[derive(Parser, Debug)]
2111
#[command(version, about, long_about = None)]
@@ -29,6 +19,8 @@ struct Args {
2919

3020
fn main() -> Result<(), Box<dyn std::error::Error>> {
3121
let args = Args::parse();
22+
let bar = ProgressBar::new(100);
23+
3224

3325
let (hash, salt) = match (fs::read("hash.txt"), fs::read("salt.txt")) {
3426
(Ok(hash), Ok(salt)) => {
@@ -46,11 +38,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4638
let key = derive_key(hash, salt)?;
4739

4840
let file_path = args.file.to_string();
49-
41+
5042
match fs::read(&file_path) {
5143
Ok(content) => {
5244
match args.decrypt {
5345
true => {
46+
println!("Decrypting your file...");
47+
for _ in 0..100 {
48+
bar.inc(1);
49+
sleep(Duration::from_millis(10));
50+
}
5451
let encrypted_content = fs::read(&file_path)?;
5552
let decrypted_content = decrypt(encrypted_content, &key)?;
5653

@@ -59,9 +56,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5956
output_path.set_extension("dec");
6057

6158
fs::write(output_path, decrypted_content)?;
59+
bar.finish();
60+
6261
println!("File successfully decrypted!");
6362
},
6463
false => {
64+
println!("Encrypting your file...");
65+
for _ in 0..100 {
66+
bar.inc(1);
67+
sleep(Duration::from_millis(10));
68+
}
6569
let (ciphertext, nonce) = encrypt(content.into(), &key).map_err(|e| format!("Encryption failed: {}", e))?;
6670

6771
let path = Path::new(&file_path);
@@ -73,65 +77,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
7377
let encrypted_data = (ciphertext, nonce_vec);
7478

7579
fs::write(output_path, bincode::serialize(&encrypted_data)?)?;
80+
bar.finish();
81+
7682
println!("File successfully encrypted!");
7783
},
7884
}
7985
}
8086
Err(e) => println!("Error reading file: {}", e),
8187
}
82-
83-
Ok(())
84-
}
85-
86-
fn encrypt(content: Vec<u8>, key: &Key<Aes256Gcm>) -> Result<(Vec<u8>, Nonce<typenum::U12>), Box<dyn std::error::Error>> {
87-
let cipher = Aes256Gcm::new(key);
88-
let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
89-
90-
let ciphertext = cipher.encrypt(&nonce, &*content.as_ref()).map_err(|e| format!("Encryption failed: {}", e))?;
91-
92-
Ok((ciphertext, nonce))
93-
}
94-
95-
fn decrypt(encrypted_content: Vec<u8>, key: &Key<Aes256Gcm>) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
96-
let (ciphertext, nonce_vec): (Vec<u8>, Vec<u8>) = bincode::deserialize(&encrypted_content)?;
97-
let nonce = Nonce::from_slice(&nonce_vec);
98-
let cipher = Aes256Gcm::new(key);
99-
100-
let decrypted_content = cipher.decrypt(nonce, &*ciphertext.as_ref()).map_err(|e| format!("Decryption failed: {}", e))?;
101-
102-
103-
Ok(decrypted_content)
104-
}
105-
106-
fn derive_key(hash: Vec<u8>, salt: Vec<u8>) -> Result<Key<Aes256Gcm>, Box<dyn std::error::Error>> {
107-
let params: Params = Params::new(15, 8, 1, 32)?;
108-
109-
let hash_str = String::from_utf8(hash)?;
110-
let salt_str = String::from_utf8(salt)?;
111-
112-
let mut key_bytes = vec![0u8; 32];
113-
114-
scrypt::scrypt(
115-
hash_str.as_bytes(),
116-
salt_str.as_bytes(),
117-
&params,
118-
&mut key_bytes
119-
)?;
120-
121-
let key = Key::<Aes256Gcm>::from_slice(&key_bytes);
122-
123-
Ok(*key)
124-
}
125-
126-
fn generate_hash_and_salt() -> Result<(), Box<dyn std::error::Error>> {
127-
let binding = rprompt::prompt_reply("Enter a password to be hashed: ").unwrap();
128-
let password = binding.as_bytes();
129-
let salt = SaltString::generate(&mut OsRng);
130-
131-
let password_hash = Scrypt.hash_password(password, &salt)?;
132-
133-
fs::write("hash.txt", password_hash.to_string())?;
134-
fs::write("salt.txt", salt.to_string())?;
135-
13688
Ok(())
13789
}

0 commit comments

Comments
 (0)