Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
CKingX committed Jun 4, 2022
1 parent e3e7749 commit 4d88184
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use xorf::{BinaryFuse16, BinaryFuse32, BinaryFuse8, Filter as FuseFilter};

#[derive(Copy,Clone)]
#[derive(Copy, Clone)]
pub enum FilterSize {
Small,
Medium,
Expand Down
14 changes: 10 additions & 4 deletions src/filter_generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::filter::{FilterSize, self};
use crate::filter::{self, FilterSize};
use crate::password;
use siphasher::sip::SipHasher13;
use std::hash::Hash;
Expand Down Expand Up @@ -69,7 +69,10 @@ pub fn generate_filter(input: OsString, output: OsString) {
}

let input_file = std::fs::File::options().read(true).open(input);
let output_file = std::fs::File::options().write(true).create_new(true).open(output);
let output_file = std::fs::File::options()
.write(true)
.create_new(true)
.open(output);
let keys = SipHasher13::new().keys();

if let Err(error) = input_file {
Expand All @@ -96,15 +99,18 @@ pub fn generate_filter(input: OsString, output: OsString) {

let filter = filter::Filter::new(&input_file, keys, result);
if let Err(()) = filter {
eprintln!("Unable to generate filter. Please report this issue with diagnostics codes {} {}", keys.0, keys.1);
eprintln!(
"Unable to generate filter. Please report this issue with diagnostics codes {} {}",
keys.0, keys.1
);
return;
}
let filter = filter.unwrap();

drop(input_file);

let output = rmp_serde::to_vec(&filter);

if let Err(_) = output {
eprintln!("Unable to convert filter for output");
return;
Expand Down
1 change: 1 addition & 0 deletions src/interactive_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

34 changes: 12 additions & 22 deletions src/interactive_online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,21 @@ use crate::password::{self, PasswordWithUsage};
use num_format::{Locale, ToFormattedString};

pub fn interactive() {
loop {
let password = rpassword::prompt_password("Password to check: ");
if password.is_err() || password.as_ref().unwrap().is_empty() {
println!("No password given. Try again");
continue;
}
let password = password::get_password();

if let Ok(password) = password {
let result = password::check_password_online(password);
let result = password::check_password_online(password);

if let Ok(password_result) = result {
match password_result {
PasswordWithUsage::SafePassword => println!("Password not compromised"),
PasswordWithUsage::CompromisedPassword(num) => {
println!(
"The password is compromised. It has been seen {} times",
num.to_formatted_string(&Locale::en)
)
}
}
} else {
error::server_error()
if let Ok(password_result) = result {
match password_result {
PasswordWithUsage::SafePassword => println!("Password not compromised"),
PasswordWithUsage::CompromisedPassword(num) => {
println!(
"The password is compromised. It has been seen {} times",
num.to_formatted_string(&Locale::en)
)
}

break;
}
} else {
error::server_error()
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod downloader;
mod error;
mod filter;
mod filter_generator;
mod interactive_file;
mod interactive_online;
mod password;

Expand Down
12 changes: 12 additions & 0 deletions src/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,15 @@ pub fn strip_padding(line: &&str) -> bool {
pub fn remove_usage(line: &str) -> String {
line.split(':').next().unwrap().to_string()
}

pub fn get_password() -> String {
let password = loop {
let password = rpassword::prompt_password("Password to check: ");
if password.is_err() || password.as_ref().unwrap().is_empty() {
println!("No password given. Try again");
continue;
}
break password.unwrap();
};
password
}

0 comments on commit 4d88184

Please sign in to comment.