Skip to content

Commit 335e4d3

Browse files
committed
feat: update version to 0.1.5 and enhance error handling across commands
1 parent 763e1b1 commit 335e4d3

File tree

10 files changed

+369
-178
lines changed

10 files changed

+369
-178
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "smart-locker"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
authors = ["William"]
55
edition = "2021"
66
license = "MIT"
@@ -52,6 +52,7 @@ ring = "0.17.14"
5252
copypasta = "0.10.1"
5353
colored = "3.0"
5454
flate2 = "1.0"
55+
thiserror = "1.0"
5556

5657
[features]
5758
default = []

src/commands/decrypt.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
11
use crate::utils::toolbox::get_locker_dir;
2+
use crate::SmartLockerError;
23
use aes_gcm::aead::Aead;
34
use aes_gcm::KeyInit;
45
use aes_gcm::{Aes256Gcm, Key, Nonce};
56
use flate2::read::GzDecoder;
67
use std::fs;
78
use std::io::Read;
89

9-
pub fn decrypt(name: &str) -> String {
10-
let locker_dir = get_locker_dir();
10+
pub fn decrypt(name: &str) -> Result<String, SmartLockerError> {
11+
let locker_dir = get_locker_dir()?;
1112
let key_path = locker_dir.join("locker.key");
1213

1314
// Lire la clé symétrique
14-
let key_data = fs::read(&key_path).expect("Unable to read symmetric key");
15+
let key_data = fs::read(&key_path).map_err(|e| {
16+
SmartLockerError::FileSystemError(format!("Unable to read symmetric key: {}", e))
17+
})?;
1518
let key = Key::<Aes256Gcm>::from_slice(&key_data);
1619

1720
// Initialiser AES-GCM avec la clé
1821
let cipher = Aes256Gcm::new(key);
1922

2023
// Lire le fichier chiffré
2124
let input_path = locker_dir.join(format!("{}.slock", name));
22-
let encrypted_data = fs::read(&input_path).expect("Unable to read encrypted file");
25+
let encrypted_data = fs::read(&input_path).map_err(|e| {
26+
SmartLockerError::FileSystemError(format!("Unable to read encrypted file: {}", e))
27+
})?;
2328

2429
// Extraire le nonce et les données chiffrées
2530
let (nonce, ciphertext) = encrypted_data.split_at(12);
2631
let nonce = Nonce::from_slice(nonce);
2732

2833
// Déchiffrer les données
29-
let decrypted_data = cipher
30-
.decrypt(nonce, ciphertext)
31-
.expect("Error during decryption");
34+
let decrypted_data = cipher.decrypt(nonce, ciphertext).map_err(|e| {
35+
SmartLockerError::DecryptionError(format!("Error during decryption: {}", e))
36+
})?;
3237

3338
// Décompresser les données
3439
let mut decoder = GzDecoder::new(&decrypted_data[..]);
3540
let mut decompressed_data = String::new();
3641
decoder
3742
.read_to_string(&mut decompressed_data)
38-
.expect("Error during data decompression");
39-
decompressed_data
43+
.map_err(|e| {
44+
SmartLockerError::DecryptionError(format!("Error during data decompression: {}", e))
45+
})?;
46+
47+
Ok(decompressed_data)
4048
}

src/commands/encrypt.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::utils::toolbox::get_locker_dir;
2+
use crate::SmartLockerError;
23
use aes_gcm::aead::Aead;
34
use aes_gcm::KeyInit;
45
use aes_gcm::{Aes256Gcm, Key, Nonce};
@@ -7,31 +8,41 @@ use flate2::Compression;
78
use std::fs;
89
use std::io::Write;
910

10-
pub fn encrypt(secret: &str, name: &str) {
11-
let locker_dir = get_locker_dir();
11+
pub fn encrypt(secret: &str, name: &str) -> Result<(), SmartLockerError> {
12+
let locker_dir = get_locker_dir()?;
1213
let key_path = locker_dir.join("locker.key");
1314

14-
let key_data = fs::read(&key_path).expect("Unable to read symmetric key");
15+
let key_data = fs::read(&key_path).map_err(|e| {
16+
SmartLockerError::FileSystemError(format!("Unable to read symmetric key: {}", e))
17+
})?;
1518
let key = Key::<Aes256Gcm>::from_slice(&key_data);
1619
let cipher = Aes256Gcm::new(key);
1720
let random_bytes = rand::random::<[u8; 12]>();
1821
let nonce = Nonce::from_slice(&random_bytes);
1922

2023
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
21-
encoder
22-
.write_all(secret.as_bytes())
23-
.expect("Error during data compression");
24-
let compressed_data = encoder.finish().expect("Error when finalizing compression");
24+
encoder.write_all(secret.as_bytes()).map_err(|e| {
25+
SmartLockerError::EncryptionError(format!("Error during data compression: {}", e))
26+
})?;
27+
let compressed_data = encoder.finish().map_err(|e| {
28+
SmartLockerError::EncryptionError(format!("Error when finalizing compression: {}", e))
29+
})?;
2530

2631
let ciphertext = cipher
2732
.encrypt(nonce, compressed_data.as_ref())
28-
.expect("Error during encryption");
33+
.map_err(|e| {
34+
SmartLockerError::EncryptionError(format!("Error during encryption: {}", e))
35+
})?;
2936

3037
let output_path = locker_dir.join(format!("{}.slock", name));
3138
let mut output_data = Vec::new();
3239
output_data.extend_from_slice(nonce);
3340
output_data.extend_from_slice(&ciphertext);
3441

35-
fs::write(&output_path, output_data).expect("Error when writing encrypted file");
42+
fs::write(&output_path, output_data).map_err(|e| {
43+
SmartLockerError::FileSystemError(format!("Error when writing encrypted file: {}", e))
44+
})?;
3645
println!("✅ Secret encrypted and saved in: {:?}", output_path);
46+
47+
Ok(())
3748
}

src/commands/list.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use crate::utils::toolbox::get_locker_dir;
2+
use crate::SmartLockerError;
23
use std::fs;
34

45
/// Returns a list of available secrets in the secure folder.
5-
pub fn list_secrets() -> Vec<String> {
6-
let locker_dir = get_locker_dir();
6+
pub fn list_secrets() -> Result<Vec<String>, SmartLockerError> {
7+
let locker_dir = get_locker_dir()?;
78
let mut secrets = Vec::new();
9+
810
if let Ok(entries) = fs::read_dir(&locker_dir) {
911
for entry in entries.flatten() {
1012
if let Ok(file_type) = entry.file_type() {
@@ -17,5 +19,6 @@ pub fn list_secrets() -> Vec<String> {
1719
}
1820
}
1921
}
20-
secrets
22+
23+
Ok(secrets)
2124
}

src/commands/remove.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
use crate::utils::toolbox::get_locker_dir;
2+
use crate::SmartLockerError;
23
use std::fs;
34

4-
pub fn remove_secret(name: &str) {
5-
let locker_dir = get_locker_dir();
5+
pub fn remove_secret(name: &str) -> Result<(), SmartLockerError> {
6+
let locker_dir = get_locker_dir()?;
67

78
if !locker_dir.exists() {
89
println!("No secure folder found. Run `init` to create it.");
9-
return;
10+
return Ok(());
1011
}
1112

1213
let file_path = locker_dir.join(format!("{}.slock", name));
1314
if file_path.exists() {
14-
fs::remove_file(&file_path).expect("Error when deleting the file");
15+
fs::remove_file(&file_path).map_err(|e| {
16+
SmartLockerError::FileSystemError(format!("Error when deleting the file: {}", e))
17+
})?;
1518
println!("Secret '{}' has been successfully deleted.", name);
1619
} else {
1720
println!("Secret '{}' doesn't exist.", name);
1821
}
22+
23+
Ok(())
1924
}

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
use thiserror::Error;
2+
13
pub mod commands;
24
pub use crate::commands::{
35
decrypt::decrypt, encrypt::encrypt, list::list_secrets, remove::remove_secret,
46
};
57
pub mod utils;
68
pub use crate::utils::toolbox::{derive_key_from_passphrase, get_locker_dir, init_locker};
9+
10+
#[derive(Error, Debug)]
11+
pub enum SmartLockerError {
12+
#[error("File system error: {0}")]
13+
FileSystemError(String),
14+
#[error("Encryption error: {0}")]
15+
EncryptionError(String),
16+
#[error("Decryption error: {0}")]
17+
DecryptionError(String),
18+
#[error("Initialization error: {0}")]
19+
InitializationError(String),
20+
#[error("Unknown error: {0}")]
21+
UnknownError(String),
22+
}

0 commit comments

Comments
 (0)