From bcd2e18eee594fbe4c96447c98a4091b0f817c00 Mon Sep 17 00:00:00 2001 From: Arkaprabha Chakraborty Date: Tue, 28 Nov 2023 09:29:35 +0530 Subject: [PATCH] Changes commited: Fix multiple bugs when printing error messages Bump the version number to 0.1.3-alpha --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- src/main.rs | 78 ++++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 62 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e9b90ea..44f05fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,7 +135,7 @@ dependencies = [ [[package]] name = "rustcm-cli" -version = "0.1.2-alpha" +version = "0.1.3-alpha" dependencies = [ "colored", "orion", diff --git a/Cargo.toml b/Cargo.toml index f4cefcc..60c5b89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustcm-cli" -version = "0.1.2-alpha" +version = "0.1.3-alpha" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 7c89d09..3e48467 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ It does precisely what the name indicates, i.e., encryption and decryption of UT ## Usage ``` -rustcm-cli 0.1.2-alpha +rustcm-cli 0.1.3-alpha Rust Simple Text Cipher Machine. USAGE: diff --git a/src/main.rs b/src/main.rs index 38a4bd1..73641ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -// Program: rustcm-cli (0.1.2-alpha) +// Program: rustcm-cli (0.1.3-alpha) // License: GNU GPL version 3 // Author: Arkaprabha Chakraborty // @@ -13,15 +13,18 @@ use std::io::{stdout, Read, Write}; use std::process::exit; const PROGRAM_NAME: &str = "rustcm-cli"; -const PROGRAM_VERSION: &str = "0.1.2-alpha"; +const PROGRAM_VERSION: &str = "0.1.3-alpha"; pub fn get_password(prompt: &str) -> String { - into_match(stdout().flush(), "{} Could not flush date to stdout"); + into_match( + stdout().flush(), + format!("{} Could not flush date to stdout", "Error".bright_red()).as_str(), + ); let password = into_match( prompt_password_tty(Some(prompt)), - "{} Could not read the password", + format!("{} Could not read the password", "Error".bright_red()).as_str(), ); - println!(); + //println!(); password } @@ -43,15 +46,19 @@ pub fn encrypt(plaintext: String, secret_key: orion::kdf::SecretKey) -> Vec pub fn get_secret_key(salt_bytes: [u8; 32], password: String) -> orion::kdf::SecretKey { let password = into_match( kdf::Password::from_slice(password.as_bytes()), - "{} Could not create the password", + format!("{} Could not use the password", "Error:".bright_red()).as_str(), ); let salt = into_match( kdf::Salt::from_slice(&salt_bytes), - "{} Could not create the salt", + format!("{} Could not create the salt", "Error:".bright_red()).as_str(), ); into_match( kdf::derive_key(&password, &salt, 3, 8, 32), - "{} Could not generate the secret key", + format!( + "{} Could not generate the secret key", + "Error:".bright_red() + ) + .as_str(), ) } @@ -86,40 +93,73 @@ pub fn into_match(res: Result, estr: &str) -> T { pub fn get_salt(salt_bytes: [u8; 32]) -> orion::kdf::Salt { let salt = into_match( kdf::Salt::from_slice(&salt_bytes), - "{} Could not generate the salt", + format!("{} Could not generate the salt", "Error:".bright_red()).as_str(), ); salt } pub fn write_plain(path: String, plaintext: String) { - let mut file = into_match(File::create(&path), "{} Could not create {path}"); + let mut file = into_match( + File::create(&path), + format!("{} Could not create {path}", "Error".bright_red()).as_str(), + ); into_match( file.write(&plaintext.into_bytes()), - "{} Could not write the data to {path}", + format!( + "{} Could not write the data to {path}", + "Error".bright_red() + ) + .as_str(), + ); + into_match( + file.flush(), + format!("{} Could not flush data to {path}", "Error".bright_red()).as_str(), ); - into_match(file.flush(), "{} Could not flush data to {path}"); } pub fn write_cipher(path: String, salt_bytes: [u8; 32], ciphertext: Vec) { - let mut file = into_match(File::create(&path), "{} Could not create {path}"); - into_match(file.write(&salt_bytes), "{} Could write the salt to {path}"); + let mut file = into_match( + File::create(&path), + format!("{} Could not create {path}", "Error".bright_red()).as_str(), + ); + into_match( + file.write(&salt_bytes), + format!("{} Could write the salt to {path}", "Error".bright_red()).as_str(), + ); into_match( file.write(&ciphertext), - "{} Could not write the ciphertext to {path}", + format!( + "{} Could not write the ciphertext to {path}", + "Error:".bright_red() + ) + .as_str(), + ); + into_match( + file.flush(), + format!("{} Could not flush data to {path}", "Error:".bright_red()).as_str(), ); - into_match(file.flush(), "{} Could not flush data to {path}"); } pub fn read_plain(path: String) -> String { - into_match(read_to_string(&path), "{} Could not read the {path}") + into_match( + read_to_string(&path), + format!("{} Could not read {path}", "Error:".bright_red()).as_str(), + ) } pub fn read_cipher(path: String) -> (Vec, Vec) { - let mut file = into_match(File::open(&path), "{} Could not open {path}"); + let mut file = into_match( + File::open(&path), + format!("{} Could not open {path}", "Error:".bright_red()).as_str(), + ); let metadata = into_match( File::metadata(&file), - "{} Could not read the metadata off of {path}", + format!( + "{} Could not read the metadata off of {path}", + "Error:".bright_red() + ) + .as_str(), ); let mut data: Vec = vec![0u8; metadata.len() as usize]; match file.read(&mut data) {