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]
|
[dependencies]
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
|
bincode = "1.3.3"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
ring = "0.17.8"
|
ring = "0.17.8"
|
||||||
|
serde = { version = "1.0.203", features = ["derive"] }
|
|
@ -1,12 +1,18 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
|
use bincode;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
use crate::{interface, resource};
|
||||||
|
|
||||||
|
|
||||||
// Structs
|
// Structs
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Password {
|
pub struct Password {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub user: String,
|
pub user: String,
|
||||||
pub phrase: String,
|
pub phrase: String,
|
||||||
}
|
}
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Manager{
|
pub struct Manager{
|
||||||
passwords: Vec<Password>
|
passwords: Vec<Password>
|
||||||
}
|
}
|
||||||
|
@ -15,10 +21,40 @@ pub struct Manager{
|
||||||
impl Manager {
|
impl Manager {
|
||||||
// Constructors
|
// Constructors
|
||||||
pub fn init() -> Self {
|
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
|
// 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 {
|
pub fn password_generate(length: u32, numbers: bool, special: bool) -> String {
|
||||||
// Variables
|
// Variables
|
||||||
let mut result: String = String::new();
|
let mut result: String = String::new();
|
||||||
|
@ -50,6 +86,9 @@ impl Manager {
|
||||||
|
|
||||||
// Adding it to our list
|
// Adding it to our list
|
||||||
self.passwords.push(new_password);
|
self.passwords.push(new_password);
|
||||||
|
|
||||||
|
// Saving the password manager
|
||||||
|
self.save();
|
||||||
}
|
}
|
||||||
pub fn password_view(&mut self, name: String) -> (bool, Password) {
|
pub fn password_view(&mut self, name: String) -> (bool, Password) {
|
||||||
// Variables
|
// Variables
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
// Libraries
|
// Libraries
|
||||||
use std::{fs, path::Path};
|
use std::{fs::{read_to_string, write, File}, io::{self, Read}, path::Path};
|
||||||
|
|
||||||
// Structures
|
// Structures
|
||||||
pub struct ResourceFile {
|
pub struct ResourceFile {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
pub content: String
|
pub content: String
|
||||||
}
|
}
|
||||||
|
pub struct ResourceFileBin {
|
||||||
|
pub path: String,
|
||||||
|
pub content: Vec<u8>
|
||||||
|
}
|
||||||
|
|
||||||
// Implementations
|
// Implementations
|
||||||
impl ResourceFile {
|
impl ResourceFile {
|
||||||
|
@ -14,16 +18,38 @@ impl ResourceFile {
|
||||||
}
|
}
|
||||||
pub fn read(file_path: &str) -> Self {
|
pub fn read(file_path: &str) -> Self {
|
||||||
// Reading from a file:
|
// 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:
|
// Returning the value:
|
||||||
return ResourceFile {path: String::from(file_path), content};
|
return ResourceFile {path: String::from(file_path), content};
|
||||||
}
|
}
|
||||||
pub fn write(file_path: &str, content: String) -> Self {
|
pub fn write(file_path: &str, content: String) -> Self {
|
||||||
// Writing to file
|
// 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:
|
// Returning the value:
|
||||||
return ResourceFile {path: String::from(file_path), content: content};
|
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