Added password encryption

This commit is contained in:
Maddox Werts 2024-06-15 16:35:24 -04:00
parent 4b54bf1725
commit cd8a57669f
3 changed files with 33 additions and 14 deletions

View file

@ -4,7 +4,8 @@ use crate::{resource, secure};
// Structures // Structures
pub struct Auth { pub struct Auth {
pub verified: bool, pub verified: bool,
pub username: String pub username: String,
pub key: String
} }
// Implementations // Implementations
@ -17,22 +18,22 @@ impl Auth {
let file_auth: resource::ResourceFile = resource::ResourceFile::read("res/auth.dat"); let file_auth: resource::ResourceFile = resource::ResourceFile::read("res/auth.dat");
// Decrypting the content of the file with the password // Decrypting the content of the file with the password
let auth_plaintext: String = secure::Secure::decrypt(file_auth.content, password); let auth_plaintext: String = secure::Secure::decrypt(file_auth.content, password.clone());
// Testing if the user is authenticated: // Testing if the user is authenticated:
let auth_status: bool = auth_plaintext == username; let auth_status: bool = auth_plaintext == username;
// Returning the result // Returning the result
return Auth {verified: auth_status, username: username}; return Auth {verified: auth_status, username: username, key: password};
} }
pub fn create(username: String, password: String) -> Self { pub fn create(username: String, password: String) -> Self {
// Encrypting our username based on our password // Encrypting our username based on our password
let cipher_username: String = secure::Secure::encrypt(username.clone(), password); let cipher_username: String = secure::Secure::encrypt(username.clone(), password.clone());
// Saving that to a file // Saving that to a file
let _: resource::ResourceFile = resource::ResourceFile::write("res/auth.dat", cipher_username); let _: resource::ResourceFile = resource::ResourceFile::write("res/auth.dat", cipher_username);
// Returning the result // Returning the result
return Auth {verified: true, username: username}; return Auth {verified: true, username: username, key: password};
} }
} }

View file

@ -24,7 +24,7 @@ fn stage_authorize(auth_profile: &mut auth::Auth) {
} }
fn stage_manager(auth_profile: &mut auth::Auth) { fn stage_manager(auth_profile: &mut auth::Auth) {
// Loading the manager // Loading the manager
let mut manager: manager::Manager = manager::Manager::init(); let mut manager: manager::Manager = manager::Manager::init(auth_profile.key.clone());
// Loop // Loop
loop { loop {
@ -57,7 +57,7 @@ fn stage_manager(auth_profile: &mut auth::Auth) {
// Entry-Point // Entry-Point
fn main() { fn main() {
// Authorizing User // Authorizing User
let mut auth_profile: auth::Auth = auth::Auth{verified: false, username: String::from("ERROR")}; let mut auth_profile: auth::Auth = auth::Auth{verified: false, username: String::from("ERROR"), key: String::from("ERROR")};
while !auth_profile.verified { while !auth_profile.verified {
stage_authorize(&mut auth_profile); stage_authorize(&mut auth_profile);

View file

@ -2,7 +2,7 @@
use bincode; use bincode;
use rand::Rng; use rand::Rng;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::{interface, resource}; use crate::{interface, resource, secure};
// Structs // Structs
@ -14,14 +14,15 @@ pub struct Password {
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Manager{ pub struct Manager{
passwords: Vec<Password> passwords: Vec<Password>,
key: String
} }
// Implementations // Implementations
impl Manager { impl Manager {
// Constructors // Constructors
pub fn init() -> Self { pub fn init(key: String) -> Self {
let mut manager: Manager = Manager { passwords: vec![] }; let mut manager: Manager = Manager { passwords: vec![], key: key };
if resource::ResourceFileBin::exists("res/manager.dat") { if resource::ResourceFileBin::exists("res/manager.dat") {
manager.load(); manager.load();
@ -32,8 +33,19 @@ impl Manager {
// Functions // Functions
fn save(&self) { fn save(&self) {
// Going through each password and encrypting them
let mut enc_manager: Manager = Manager { passwords: vec![], key: String::new() };
for pla_password in &self.passwords {
enc_manager.passwords.push(Password {
name: secure::Secure::encrypt(pla_password.name.clone(), self.key.clone()),
user: secure::Secure::encrypt(pla_password.user.clone(), self.key.clone()),
phrase: secure::Secure::encrypt(pla_password.phrase.clone(), self.key.clone())
});
}
// Turning the manager into binary data // Turning the manager into binary data
let manager_binary: Vec<u8> = bincode::serialize(self).expect("Failed to serialize manager"); let manager_binary: Vec<u8> = bincode::serialize(&enc_manager).expect("Failed to serialize manager");
// Saving via Binary Reader // Saving via Binary Reader
resource::ResourceFileBin::write("res/manager.dat", manager_binary); resource::ResourceFileBin::write("res/manager.dat", manager_binary);
@ -48,8 +60,14 @@ impl Manager {
// Turning the manager into binary data // Turning the manager into binary data
let manager: Manager = bincode::deserialize(&content.content).expect("Failed to serialize manager"); let manager: Manager = bincode::deserialize(&content.content).expect("Failed to serialize manager");
// Setting values // Decrypt all passwords
self.passwords = manager.passwords; for enc_password in manager.passwords {
self.passwords.push(Password {
name: secure::Secure::decrypt(enc_password.name.clone(), self.key.clone()),
user: secure::Secure::decrypt(enc_password.user.clone(), self.key.clone()),
phrase: secure::Secure::decrypt(enc_password.phrase.clone(), self.key.clone())
});
}
// Message: // Message:
println!("{}[Loaded]{}", interface::COLOR_GREEN, interface::COLOR_RESET); println!("{}[Loaded]{}", interface::COLOR_GREEN, interface::COLOR_RESET);