change from just exiting to returning a result

Co-authored-by: Alpisc <Alpisc@users.noreply.github.com>
This commit is contained in:
Mystikfluu 2023-01-14 16:37:47 +01:00
parent d2c9adefdc
commit 31b8e5bf0a

View File

@ -146,7 +146,9 @@ fn encrypt_pass(nonce_arg:String, pass: String,mpw: String) -> String {
return hex::encode(ciphertext); return hex::encode(ciphertext);
} }
fn decrypt_pass(nonce_arg:String, pass: Vec<u8>,mpw: String) -> String {
fn decrypt_pass(nonce_arg:String, pass: Vec<u8>,mpw: String) -> Result<String,String> {
let mut nonce_argument = String::new(); let mut nonce_argument = String::new();
if nonce_arg.len() < 12 { if nonce_arg.len() < 12 {
nonce_argument = nonce_arg.clone() + &" ".repeat(12-nonce_arg.len()); nonce_argument = nonce_arg.clone() + &" ".repeat(12-nonce_arg.len());
@ -170,11 +172,10 @@ fn decrypt_pass(nonce_arg:String, pass: Vec<u8>,mpw: String) -> String {
let plaintext = cipher.decrypt(nonce, pass.as_ref()); let plaintext = cipher.decrypt(nonce, pass.as_ref());
match plaintext { match plaintext {
Ok(res) => { Ok(res) => {
return vecu8_to_string(res); return Ok(vecu8_to_string(res));
} }
Err(_) => { Err(_) => {
eprintln!("[ERROR] Error decrypting data, check your master password"); return Err("[ERROR] Error decrypting data, check your master password".to_string());
std::process::exit(1);
} }
} }
} }
@ -209,35 +210,43 @@ pub fn create_entry(name: &String, pw: String, mpw: String) -> bool {
return true; return true;
} }
fn read_entry(name:&String,mpw:String) -> String { fn read_entry(name:&String,mpw:String) -> Result<String,String> {
let content = &mut read_to_string(get_ipass_folder()+name+".ipass").expect("Should have been able to read the file"); let content = &mut read_to_string(get_ipass_folder()+name+".ipass").expect("Should have been able to read the file");
return decrypt_pass(name.to_owned(),hex::decode(content).unwrap(),mpw).to_owned(); decrypt_pass(name.to_owned(),hex::decode(content).unwrap(),mpw)
} }
pub fn get_entry(name:&String, mpw: String) -> String { pub fn get_entry(name:&String, mpw: String) -> Result<String,String> {
return read_entry(name,mpw); return read_entry(name,mpw);
} }
pub fn edit_password(name:&String, password:String, mpw: String) { pub fn edit_password(name:&String, password:String, mpw: String) -> bool {
let entry = read_entry(name, mpw.clone()); let entry_result = read_entry(name, mpw.clone());
// println!("entry: {entry}"); if let Ok(entry) = entry_result {
let mut parts = entry.split(";"); // println!("entry: {entry}");
let username = parts.next().unwrap().to_string(); let mut parts = entry.split(";");
let _old_password = parts.next().unwrap(); let username = parts.next().unwrap().to_string();
let data = encrypt_pass(name.to_owned(), username+";"+password.as_str(),mpw); let _old_password = parts.next().unwrap();
let mut file = File::create(get_ipass_folder()+name+".ipass").unwrap(); let data = encrypt_pass(name.to_owned(), username+";"+password.as_str(),mpw);
file.write_all(data.as_bytes()).unwrap(); let mut file = File::create(get_ipass_folder()+name+".ipass").unwrap();
file.write_all(data.as_bytes()).unwrap();
return true;
}
return false;
} }
pub fn edit_username(name:&String, username: String, mpw: String) { pub fn edit_username(name:&String, username: String, mpw: String) -> bool {
let entry = read_entry(name, mpw.clone()); let entry_result = read_entry(name, mpw.clone());
// println!("entry: {entry}"); if let Ok(entry) = entry_result {
let mut parts = entry.split(";"); // println!("entry: {entry}");
let _old_username = parts.next().unwrap(); let mut parts = entry.split(";");
let password = parts.next().unwrap(); let _old_username = parts.next().unwrap();
let data = encrypt_pass(name.to_owned(), username+";"+password,mpw); let password = parts.next().unwrap();
let mut file = File::create(get_ipass_folder()+name+".ipass").unwrap(); let data = encrypt_pass(name.to_owned(), username+";"+password,mpw);
file.write_all(data.as_bytes()).unwrap(); let mut file = File::create(get_ipass_folder()+name+".ipass").unwrap();
file.write_all(data.as_bytes()).unwrap();
return true;
}
return false;
} }
pub fn prompt_answer(toprint: String) -> String { pub fn prompt_answer(toprint: String) -> String {
@ -253,19 +262,22 @@ pub fn prompt_answer_nolower(toprint: String) -> String {
return choice.trim().to_string(); return choice.trim().to_string();
} }
pub fn rename(name: &String, new_name: &String, mpw: String) { pub fn rename(name: &String, new_name: &String, mpw: String) -> bool {
if !std::path::Path::new(&(get_ipass_folder()+name+".ipass")).exists() { if !std::path::Path::new(&(get_ipass_folder()+name+".ipass")).exists() {
return; return false;
} }
if std::path::Path::new(&(get_ipass_folder()+new_name+".ipass")).exists() { if std::path::Path::new(&(get_ipass_folder()+new_name+".ipass")).exists() {
return; return false;
} }
let content = &mut read_to_string(get_ipass_folder()+name+".ipass").expect("Should have been able to read the file"); let content = &mut read_to_string(get_ipass_folder()+name+".ipass").expect("Should have been able to read the file");
let mut pw = decrypt_pass(name.to_owned(),hex::decode(content).unwrap(),mpw.clone()).to_owned(); let data_result = decrypt_pass(name.to_owned(),hex::decode(content).unwrap(),mpw.clone()).to_owned();
if let Ok(mut data) = data_result {
pw = encrypt_pass(new_name.to_owned(), pw,mpw); data = encrypt_pass(new_name.to_owned(), data,mpw);
let mut file = File::create(get_ipass_folder()+new_name+".ipass").unwrap(); let mut file = File::create(get_ipass_folder()+new_name+".ipass").unwrap();
file.write_all(pw.as_bytes()).unwrap(); file.write_all(data.as_bytes()).unwrap();
return true;
}
return false;
} }
pub fn get_entries() -> std::fs::ReadDir { pub fn get_entries() -> std::fs::ReadDir {