Added Home Screen & Password Generator

This commit is contained in:
Maddox Werts 2024-06-15 14:23:52 -04:00
parent c136b491e4
commit a902cdd0ae
4 changed files with 136 additions and 10 deletions

View file

@ -5,4 +5,5 @@ edition = "2021"
[dependencies]
base64 = "0.22.1"
rand = "0.8.5"
ring = "0.17.8"

View file

@ -41,4 +41,61 @@ pub fn int_reg() -> (String, String) {
} else {
return (username, password);
}
}
pub fn int_welcome(username: &String) -> String {
// Variables
let mut user_input: String = String::new();
// Welcome message
println!("\n - RUSTYPASS - ");
println!("Welcome, {}!", username);
println!("Please select one of the following options: ");
println!("\n1) View a Password");
println!("2) Create a Password");
println!("3) Generate a Password");
println!("4) Quit");
io::stdin().read_line(&mut user_input).expect("Failed to read line");
// Returning what the user said
return String::from(user_input.trim());
}
pub fn int_gen() -> (u32, bool, bool) {
// Variables
let mut raw_pass_len: String = String::new();
let mut raw_pass_num: String = String::new();
let mut raw_pass_spe: String = String::new();
// Asking the user the big questions
println!(" - Password Generator - ");
println!("Before we give you a password, just a few questions!");
println!("How long should your password be?");
println!("Select size[number]: ");
io::stdin().read_line(&mut raw_pass_len).expect("Failed to read line.");
println!("Would you like numbers to be included in your password?");
println!("Select option[y/n]: ");
io::stdin().read_line(&mut raw_pass_num).expect("Failed to read line.");
println!("Would you like special characters to be included in your password?");
println!("Select option[y/n]: ");
io::stdin().read_line(&mut raw_pass_spe).expect("Failed to read line.");
// Converting the stuff
let pass_len: u32 = raw_pass_len.trim().parse().unwrap();
let str_pass_num: String = String::from(raw_pass_num.to_lowercase().trim());
let str_pass_spe: String = String::from(raw_pass_spe.to_lowercase().trim());
// Y/N to bool:
let pass_num: bool = if str_pass_num == "y" {
true
} else{
false
};
let pass_spe: bool = if str_pass_spe == "y" {
true
} else {
false
};
// Returning all of the values
return (pass_len, pass_num, pass_spe);
}

View file

@ -1,31 +1,63 @@
// Libraries
mod interface;
mod resource;
mod manager;
mod secure;
mod auth;
// Functions
// Entry-Point
fn main() {
// Variables
let auth_profile: auth::Auth;
fn stage_authorize(auth_profile: &mut auth::Auth) {
// Checking if we need to register or log in
if auth::Auth::exists() {
// Asking user for login credentials
let (username, password) = interface::int_auth();
// Verify the user
auth_profile = auth::Auth::authenticate(username, password);
*auth_profile = auth::Auth::authenticate(username, password);
} else {
// Asking user for register credentials
let (username, password) = interface::int_reg();
// Creating the user
auth_profile = auth::Auth::create(username, password);
*auth_profile = auth::Auth::create(username, password);
}
}
fn stage_manager(auth_profile: &mut auth::Auth) {
// Loop
loop {
// What page shall we go to?
let page: String = interface::int_welcome(&auth_profile.username);
// New line
println!("\n");
// Deciding what page we are on
if page == "4" {
println!("Goodbye, {}!", auth_profile.username);
break;
} else if page == "3" {
let (pass_len, pass_num, pass_spe) = interface::int_gen();
let gen_pass: String = manager::Manager::password_generate(pass_len, pass_num, pass_spe);
println!("Your password is: {}", gen_pass);
}
}
}
// Entry-Point
fn main() {
// Authorizing User
let mut auth_profile: auth::Auth = auth::Auth{verified: false, username: String::from("ERROR")};
while !auth_profile.verified {
stage_authorize(&mut auth_profile);
// Before going to the accounts page, check if the auth failed
if !auth_profile.verified {
println!("\nPlease try again!\n");
} else {break;}
}
// DEBUG: Testing if the user is authenticated
println!("Authenticated Status: {}", auth_profile.verified);
println!("");
// Passwords interface
stage_manager(&mut auth_profile);
}

36
src/manager.rs Normal file
View file

@ -0,0 +1,36 @@
// Libraries
use rand::Rng;
// Structs
pub struct Manager{
}
// Implementations
impl Manager {
pub fn password_generate(length: u32, numbers: bool, special: bool) -> String {
// Variables
let mut result: String = String::new();
let mut possible_characters: String = String::new();
// Making lists of possibilities
let ascii_letters: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let ascii_numbers: &str = "1234567890";
let ascii_special: &str = "!\"#$%&'()*+,-./:;<=>?@[]^_`{|}~";
// Putting everything together!
possible_characters += ascii_letters;
if numbers {possible_characters += ascii_numbers;}
if special {possible_characters += ascii_special;}
// Creating the password!
for _ in 0..length {
let rand_index: usize = rand::thread_rng().gen_range(0..=possible_characters.len());
let rand_character: char = possible_characters.chars().nth(rand_index).unwrap();
result += &rand_character.to_string();
}
// Giving back the result
return result;
}
}