Skip to content

Commit 5e7bfc3

Browse files
author
replydev
committed
Warn user if he inserts a weak password
1 parent fa44bb4 commit 5e7bfc3

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/cryptograpy.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ fn header_vec_to_header_array(byte_vec: Vec<u8>) -> [u8;24]{
9191
.unwrap_or_else(|v: Vec<u8>| panic!("Expected a Vec of length {} but it was {}", 24, v.len()))
9292
}
9393

94-
pub fn prompt_for_passwords(message: &str) -> String{
94+
pub fn prompt_for_passwords(message: &str,minimum_password_legth: usize) -> String{
9595
let mut password;
9696
loop{
9797
password = rpassword::prompt_password_stdout(message).unwrap();
98-
if password.len() >= 8 {
98+
if password.len() >= minimum_password_legth {
9999
break;
100100
}
101+
println!("Please insert a password with at least {} digits.",minimum_password_legth);
101102
}
102103
password
103104
}

src/database_loader.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl OTPElement {
6969
pub fn read_from_file() -> Result<Vec<OTPElement>,String>{
7070
let encrypted_contents = read_to_string(&get_db_path()).unwrap();
7171
//rust close files at the end of the function
72-
let contents = cryptograpy::decrypt_string(&encrypted_contents, &cryptograpy::prompt_for_passwords("Password: "));
72+
let contents = cryptograpy::decrypt_string(&encrypted_contents, &cryptograpy::prompt_for_passwords("Password: ",8));
7373
match contents {
7474
Ok(contents) => {
7575
let vector: Vec<OTPElement> = serde_json::from_str(&contents).unwrap();
@@ -174,7 +174,7 @@ pub fn export_database() -> Result<String, String> {
174174
exported_path.push_str("/exported.cotp");
175175
let mut file = File::create(&exported_path).expect("Cannot create file");
176176
let encrypted_contents = read_to_string(&get_db_path()).unwrap();
177-
let contents = cryptograpy::decrypt_string(&encrypted_contents, &cryptograpy::prompt_for_passwords("Password: "));
177+
let contents = cryptograpy::decrypt_string(&encrypted_contents, &cryptograpy::prompt_for_passwords("Password: ",8));
178178
match contents {
179179
Ok(contents) => {
180180
if contents == "[]"{
@@ -195,7 +195,7 @@ pub fn overwrite_database(elements: Vec<OTPElement>){
195195
}
196196

197197
pub fn overwrite_database_json(json: &str){
198-
let encrypted = cryptograpy::encrypt_string(json.to_string(), &cryptograpy::prompt_for_passwords("Insert password for database encryption: "));
198+
let encrypted = cryptograpy::encrypt_string(json.to_string(), &cryptograpy::prompt_for_passwords("Insert password for database encryption: ",8));
199199
utils::write_to_file(&encrypted, &mut File::create(utils::get_db_path()).expect("Failed to open file"));
200200
}
201201

0 commit comments

Comments
 (0)