remove ask for pw in favor of a fn arg

Co-authored-by: Alpisc <Alpisc@users.noreply.github.com>
This commit is contained in:
Mystikfluu 2023-01-13 22:29:00 +01:00
parent 380ae20437
commit d2c9adefdc
3 changed files with 7 additions and 40 deletions

24
Cargo.lock generated
View File

@ -193,14 +193,13 @@ dependencies = [
[[package]] [[package]]
name = "ip_lib" name = "ip_lib"
version = "0.1.0" version = "1.0.0"
dependencies = [ dependencies = [
"aes-gcm", "aes-gcm",
"brotli", "brotli",
"hex", "hex",
"home", "home",
"rand", "rand",
"rpassword",
"sha2", "sha2",
] ]
@ -264,27 +263,6 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "rpassword"
version = "7.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6678cf63ab3491898c0d021b493c94c9b221d91295294a2a5746eacbe5928322"
dependencies = [
"libc",
"rtoolbox",
"winapi",
]
[[package]]
name = "rtoolbox"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "034e22c514f5c0cb8a10ff341b9b048b5ceb21591f31c8f44c43b960f9b3524a"
dependencies = [
"libc",
"winapi",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.10.6" version = "0.10.6"

View File

@ -1,13 +1,12 @@
[package] [package]
name = "ip_lib" name = "ip_lib"
version = "0.1.0" version = "1.0.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
home = "0.5.4" home = "0.5.4"
rpassword = "7.2"
rand = { features = ["std"], default-features = false, version = "0.8.5" } rand = { features = ["std"], default-features = false, version = "0.8.5" }
aes-gcm = { features = ["alloc", "aes"], default-features = false, version = "0.10.1" } aes-gcm = { features = ["alloc", "aes"], default-features = false, version = "0.10.1" }
sha2 = { default-features = false, version = "0.10.6" } sha2 = { default-features = false, version = "0.10.6" }

View File

@ -198,11 +198,10 @@ pub fn get_ipass_folder() -> String {
return path; return path;
} }
pub fn create_entry(name: &String, pw: String) -> bool { pub fn create_entry(name: &String, pw: 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 false; return false;
} }
let mpw = ask_for_pw();
// println!("{pw}"); // println!("{pw}");
let pw = encrypt_pass(name.to_owned(), pw,mpw); let pw = encrypt_pass(name.to_owned(), pw,mpw);
let mut file = File::create(get_ipass_folder()+name+".ipass").unwrap(); let mut file = File::create(get_ipass_folder()+name+".ipass").unwrap();
@ -215,13 +214,11 @@ fn read_entry(name:&String,mpw:String) -> String {
return decrypt_pass(name.to_owned(),hex::decode(content).unwrap(),mpw).to_owned(); return decrypt_pass(name.to_owned(),hex::decode(content).unwrap(),mpw).to_owned();
} }
pub fn get_entry(name:&String) -> String { pub fn get_entry(name:&String, mpw: String) -> String {
let mpw = ask_for_pw();
return read_entry(name,mpw); return read_entry(name,mpw);
} }
pub fn edit_password(name:&String, password:String) { pub fn edit_password(name:&String, password:String, mpw: String) {
let mpw = ask_for_pw();
let entry = read_entry(name, mpw.clone()); let entry = read_entry(name, mpw.clone());
// println!("entry: {entry}"); // println!("entry: {entry}");
let mut parts = entry.split(";"); let mut parts = entry.split(";");
@ -232,8 +229,7 @@ pub fn edit_password(name:&String, password:String) {
file.write_all(data.as_bytes()).unwrap(); file.write_all(data.as_bytes()).unwrap();
} }
pub fn edit_username(name:&String, username: String) { pub fn edit_username(name:&String, username: String, mpw: String) {
let mpw = ask_for_pw();
let entry = read_entry(name, mpw.clone()); let entry = read_entry(name, mpw.clone());
// println!("entry: {entry}"); // println!("entry: {entry}");
let mut parts = entry.split(";"); let mut parts = entry.split(";");
@ -244,11 +240,6 @@ pub fn edit_username(name:&String, username: String) {
file.write_all(data.as_bytes()).unwrap(); file.write_all(data.as_bytes()).unwrap();
} }
fn ask_for_pw() -> String {
let output = rpassword::prompt_password("Please enter the master password: ").unwrap();
return output.replace("\n", "").replace("\r","");
}
pub fn prompt_answer(toprint: String) -> String { pub fn prompt_answer(toprint: String) -> String {
prompt_answer_nolower(toprint).to_lowercase() prompt_answer_nolower(toprint).to_lowercase()
} }
@ -262,7 +253,7 @@ 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) { pub fn rename(name: &String, new_name: &String, mpw: String) {
if !std::path::Path::new(&(get_ipass_folder()+name+".ipass")).exists() { if !std::path::Path::new(&(get_ipass_folder()+name+".ipass")).exists() {
return; return;
} }
@ -270,7 +261,6 @@ pub fn rename(name: &String, new_name: &String) {
return; return;
} }
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 mpw = ask_for_pw();
let mut pw = decrypt_pass(name.to_owned(),hex::decode(content).unwrap(),mpw.clone()).to_owned(); let mut pw = decrypt_pass(name.to_owned(),hex::decode(content).unwrap(),mpw.clone()).to_owned();
pw = encrypt_pass(new_name.to_owned(), pw,mpw); pw = encrypt_pass(new_name.to_owned(), pw,mpw);