Added password encryption
This commit is contained in:
parent
4b54bf1725
commit
cd8a57669f
3 changed files with 33 additions and 14 deletions
11
src/auth.rs
11
src/auth.rs
|
@ -4,7 +4,8 @@ use crate::{resource, secure};
|
|||
// Structures
|
||||
pub struct Auth {
|
||||
pub verified: bool,
|
||||
pub username: String
|
||||
pub username: String,
|
||||
pub key: String
|
||||
}
|
||||
|
||||
// Implementations
|
||||
|
@ -17,22 +18,22 @@ impl Auth {
|
|||
let file_auth: resource::ResourceFile = resource::ResourceFile::read("res/auth.dat");
|
||||
|
||||
// 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:
|
||||
let auth_status: bool = auth_plaintext == username;
|
||||
|
||||
// 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 {
|
||||
// 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
|
||||
let _: resource::ResourceFile = resource::ResourceFile::write("res/auth.dat", cipher_username);
|
||||
|
||||
// Returning the result
|
||||
return Auth {verified: true, username: username};
|
||||
return Auth {verified: true, username: username, key: password};
|
||||
}
|
||||
}
|
|
@ -24,7 +24,7 @@ fn stage_authorize(auth_profile: &mut auth::Auth) {
|
|||
}
|
||||
fn stage_manager(auth_profile: &mut auth::Auth) {
|
||||
// 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 {
|
||||
|
@ -57,7 +57,7 @@ fn stage_manager(auth_profile: &mut auth::Auth) {
|
|||
// Entry-Point
|
||||
fn main() {
|
||||
// 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 {
|
||||
stage_authorize(&mut auth_profile);
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
use bincode;
|
||||
use rand::Rng;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::{interface, resource};
|
||||
use crate::{interface, resource, secure};
|
||||
|
||||
|
||||
// Structs
|
||||
|
@ -14,14 +14,15 @@ pub struct Password {
|
|||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Manager{
|
||||
passwords: Vec<Password>
|
||||
passwords: Vec<Password>,
|
||||
key: String
|
||||
}
|
||||
|
||||
// Implementations
|
||||
impl Manager {
|
||||
// Constructors
|
||||
pub fn init() -> Self {
|
||||
let mut manager: Manager = Manager { passwords: vec![] };
|
||||
pub fn init(key: String) -> Self {
|
||||
let mut manager: Manager = Manager { passwords: vec![], key: key };
|
||||
|
||||
if resource::ResourceFileBin::exists("res/manager.dat") {
|
||||
manager.load();
|
||||
|
@ -32,8 +33,19 @@ impl Manager {
|
|||
|
||||
// Functions
|
||||
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
|
||||
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
|
||||
resource::ResourceFileBin::write("res/manager.dat", manager_binary);
|
||||
|
@ -48,8 +60,14 @@ impl Manager {
|
|||
// Turning the manager into binary data
|
||||
let manager: Manager = bincode::deserialize(&content.content).expect("Failed to serialize manager");
|
||||
|
||||
// Setting values
|
||||
self.passwords = manager.passwords;
|
||||
// Decrypt all 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:
|
||||
println!("{}[Loaded]{}", interface::COLOR_GREEN, interface::COLOR_RESET);
|
||||
|
|
Loading…
Reference in a new issue