Added password saving and loading
This commit is contained in:
parent
0330cea7f5
commit
4b54bf1725
3 changed files with 71 additions and 4 deletions
|
@ -5,5 +5,7 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
bincode = "1.3.3"
|
||||
rand = "0.8.5"
|
||||
ring = "0.17.8"
|
||||
serde = { version = "1.0.203", features = ["derive"] }
|
|
@ -1,12 +1,18 @@
|
|||
// Libraries
|
||||
use bincode;
|
||||
use rand::Rng;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::{interface, resource};
|
||||
|
||||
|
||||
// Structs
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Password {
|
||||
pub name: String,
|
||||
pub user: String,
|
||||
pub phrase: String,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Manager{
|
||||
passwords: Vec<Password>
|
||||
}
|
||||
|
@ -15,10 +21,40 @@ pub struct Manager{
|
|||
impl Manager {
|
||||
// Constructors
|
||||
pub fn init() -> Self {
|
||||
return Manager { passwords: vec![] };
|
||||
let mut manager: Manager = Manager { passwords: vec![] };
|
||||
|
||||
if resource::ResourceFileBin::exists("res/manager.dat") {
|
||||
manager.load();
|
||||
}
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
// Functions
|
||||
fn save(&self) {
|
||||
// Turning the manager into binary data
|
||||
let manager_binary: Vec<u8> = bincode::serialize(self).expect("Failed to serialize manager");
|
||||
|
||||
// Saving via Binary Reader
|
||||
resource::ResourceFileBin::write("res/manager.dat", manager_binary);
|
||||
|
||||
// Message:
|
||||
println!("{}[Saved]{}", interface::COLOR_GREEN, interface::COLOR_RESET);
|
||||
}
|
||||
fn load(&mut self) {
|
||||
// Saving via Binary Reader
|
||||
let content: resource::ResourceFileBin = resource::ResourceFileBin::read("res/manager.dat");
|
||||
|
||||
// Turning the manager into binary data
|
||||
let manager: Manager = bincode::deserialize(&content.content).expect("Failed to serialize manager");
|
||||
|
||||
// Setting values
|
||||
self.passwords = manager.passwords;
|
||||
|
||||
// Message:
|
||||
println!("{}[Loaded]{}", interface::COLOR_GREEN, interface::COLOR_RESET);
|
||||
}
|
||||
|
||||
pub fn password_generate(length: u32, numbers: bool, special: bool) -> String {
|
||||
// Variables
|
||||
let mut result: String = String::new();
|
||||
|
@ -50,6 +86,9 @@ impl Manager {
|
|||
|
||||
// Adding it to our list
|
||||
self.passwords.push(new_password);
|
||||
|
||||
// Saving the password manager
|
||||
self.save();
|
||||
}
|
||||
pub fn password_view(&mut self, name: String) -> (bool, Password) {
|
||||
// Variables
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
// Libraries
|
||||
use std::{fs, path::Path};
|
||||
use std::{fs::{read_to_string, write, File}, io::{self, Read}, path::Path};
|
||||
|
||||
// Structures
|
||||
pub struct ResourceFile {
|
||||
pub path: String,
|
||||
pub content: String
|
||||
}
|
||||
pub struct ResourceFileBin {
|
||||
pub path: String,
|
||||
pub content: Vec<u8>
|
||||
}
|
||||
|
||||
// Implementations
|
||||
impl ResourceFile {
|
||||
|
@ -14,16 +18,38 @@ impl ResourceFile {
|
|||
}
|
||||
pub fn read(file_path: &str) -> Self {
|
||||
// Reading from a file:
|
||||
let content: String = fs::read_to_string(String::from(file_path)).expect("Failed to read file.");
|
||||
let content: String = read_to_string(String::from(file_path)).expect("Failed to read file.");
|
||||
|
||||
// Returning the value:
|
||||
return ResourceFile {path: String::from(file_path), content};
|
||||
}
|
||||
pub fn write(file_path: &str, content: String) -> Self {
|
||||
// Writing to file
|
||||
fs::write(file_path, &content).expect("Failed writing to file");
|
||||
write(file_path, &content).expect("Failed writing to file");
|
||||
|
||||
// Returning the value:
|
||||
return ResourceFile {path: String::from(file_path), content: content};
|
||||
}
|
||||
}
|
||||
impl ResourceFileBin {
|
||||
pub fn exists(file_path: &str) -> bool{
|
||||
return Path::new(file_path).exists();
|
||||
}
|
||||
pub fn read(file_path: &str) -> Self {
|
||||
// Reading from a file:
|
||||
let file: File = File::open(String::from(file_path)).expect("Failed to read file.");
|
||||
let mut reader: io::BufReader<File> = io::BufReader::new(file);
|
||||
let mut content: Vec<u8> = Vec::new();
|
||||
reader.read_to_end(&mut content).expect("Failed to buffer file.");
|
||||
|
||||
// Returning the value:
|
||||
return ResourceFileBin {path: String::from(file_path), content};
|
||||
}
|
||||
pub fn write(file_path: &str, content: Vec<u8>) -> Self {
|
||||
// Writing to file
|
||||
write(file_path, &content).expect("Failed writing to file");
|
||||
|
||||
// Returning the value:
|
||||
return ResourceFileBin {path: String::from(file_path), content: content};
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue