Compare commits

..

10 commits

8 changed files with 399 additions and 20 deletions

1
.gitignore vendored
View file

@ -3,6 +3,7 @@
# will have compiled files and executables # will have compiled files and executables
debug/ debug/
target/ target/
build/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html

View file

@ -5,4 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
base64 = "0.22.1" base64 = "0.22.1"
bincode = "1.3.3"
rand = "0.8.5"
ring = "0.17.8" ring = "0.17.8"
serde = { version = "1.0.203", features = ["derive"] }

10
Dockerfile Normal file
View file

@ -0,0 +1,10 @@
## Setup
# Use the Rust container as a base
FROM rust
# Setup directories
RUN mkdir /src /build /comp
WORKDIR /comp
## Run
CMD cp -r /src/* ./ && cargo build && cp ./target/debug/RustyPass /build/

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

@ -1,5 +1,14 @@
// Libraries // Libraries
use std::{io, process::exit}; use std::{io, process::exit};
use crate::manager;
// Constants
pub const COLOR_RESET: &str = "\x1B[0m";
pub const COLOR_RED: &str = "\x1B[31m";
pub const COLOR_GREEN: &str = "\x1B[32m";
pub const COLOR_YELLOW: &str = "\x1B[33m";
pub const COLOR_MAGENTA: &str = "\x1B[35m";
pub const COLOR_CYAN: &str = "\x1B[36m";
// Functions // Functions
fn get_credentials() -> (String, String) { fn get_credentials() -> (String, String) {
@ -20,12 +29,12 @@ fn get_credentials() -> (String, String) {
return (username, password); return (username, password);
} }
pub fn int_auth() -> (String, String) { pub fn int_auth() -> (String, String) {
println!(" - LOGIN - "); println!(" - {} LOGIN {} - ", COLOR_YELLOW, COLOR_RESET);
return get_credentials(); return get_credentials();
} }
pub fn int_reg() -> (String, String) { pub fn int_reg() -> (String, String) {
// Register form // Register form
println!(" - REGISTER - "); println!(" - {} REGISTER {} - ", COLOR_YELLOW, COLOR_RESET);
let (username, password) = get_credentials(); let (username, password) = get_credentials();
// Confirming password // Confirming password
@ -42,3 +51,141 @@ pub fn int_reg() -> (String, String) {
return (username, password); return (username, password);
} }
} }
pub fn int_welcome(username: &String) -> String {
// Variables
let mut user_input: String = String::new();
// Welcome message
println!("\n - {} RUSTYPASS {} - ", COLOR_YELLOW, COLOR_RESET);
println!("Welcome, {}{}{}!", COLOR_CYAN, username, COLOR_RESET);
println!("Please select one of the following options: ");
println!("\n{}1{}) List passwords", COLOR_GREEN, COLOR_RESET);
println!("{}2{}) View a Password", COLOR_GREEN, COLOR_RESET);
println!("{}3{}) Create a Password", COLOR_GREEN, COLOR_RESET);
println!("{}4{}) Generate a Password", COLOR_GREEN, COLOR_RESET);
println!("{}5{}) Quit", COLOR_GREEN, COLOR_RESET);
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!("\n - {} Password Generator {} - ", COLOR_YELLOW, COLOR_RESET);
println!("Before we give you a password, just a few questions!");
println!("How long should your password be?");
println!("Select size[{}number{}]: ", COLOR_CYAN, COLOR_RESET);
io::stdin().read_line(&mut raw_pass_len).expect("Failed to read line.");
println!("\nWould you like numbers to be included in your password?");
println!("Select option[{0}y{1}/{0}n{1}]: ", COLOR_CYAN, COLOR_RESET);
io::stdin().read_line(&mut raw_pass_num).expect("Failed to read line.");
println!("\nWould you like special characters to be included in your password?");
println!("Select option[{0}y{1}/{0}n{1}]: ", COLOR_CYAN, COLOR_RESET);
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
};
println!("");
// Returning all of the values
return (pass_len, pass_num, pass_spe);
}
pub fn int_create() -> (String, String, String) {
// Variables
let mut raw_pass_name: String = String::new();
let mut raw_pass_user: String = String::new();
let mut raw_pass_type: String = String::new();
let mut raw_pass_phrase: String = String::new();
// Asking the user some questions
println!(" - {} Create a Password {} - ", COLOR_YELLOW, COLOR_RESET);
println!("To create a password, we need to ask you a few simple questions.");
println!("First, what would you like to call your Password?");
println!("Select {}Name{}: ", COLOR_CYAN, COLOR_RESET);
io::stdin().read_line(&mut raw_pass_name).expect("Failed to read line");
println!("\nNext, What is the username for this password?");
println!("Select {}User{}: ", COLOR_CYAN, COLOR_RESET);
io::stdin().read_line(&mut raw_pass_user).expect("Failed to read line");
println!("\nFinally, Would you like to use your own passphrase or a generated one?");
println!("Select passphrase [{0}generate{1}/{0}custom{1}]: ", COLOR_CYAN, COLOR_RESET);
io::stdin().read_line(&mut raw_pass_type).expect("Failed to read line");
// Formatting inputs
let pass_name: String = String::from(raw_pass_name.trim());
let pass_user: String = String::from(raw_pass_user.trim());
let pass_type: String = String::from(raw_pass_type.to_lowercase().trim());
let pass_phrase: String;
// Are we generating or using a prior one
if pass_type == "generate" {
let (pass_len, pass_num, pass_spe) = int_gen();
pass_phrase = manager::Manager::password_generate(pass_len, pass_num, pass_spe);
} else if pass_type == "custom" {
println!("\nEnter your custom password");
println!("Select {}Passphrase{}: ", COLOR_CYAN, COLOR_RESET);
io::stdin().read_line(&mut raw_pass_phrase).expect("Failed to read line");
pass_phrase = String::from(raw_pass_phrase.trim());
} else {
println!("ERROR: Incorrect passphrase type!");
exit(0);
}
// Returning values
return (pass_name, pass_user, pass_phrase);
}
pub fn int_view(manager: &mut manager::Manager) {
// Variables
let mut pass_name: String = String::new();
// Asking the user some questions
println!(" - {} Password View {} - ", COLOR_YELLOW, COLOR_RESET);
println!("What is your passwords name?");
println!("Select {}Name{}: ", COLOR_CYAN, COLOR_RESET);
io::stdin().read_line(&mut pass_name).expect("Failed to read line");
// Query password
let (success, pass_obj) = manager.password_view(String::from(pass_name.trim()));
// Was it successful?
if !success {
println!("{}Failed to get password of name {}!{}", COLOR_RED, pass_name, COLOR_RESET);
return;
}
// Show us that beautiful password!
println!("\n - {0}Password{3}: {1}{2}{3} - ", COLOR_YELLOW, COLOR_CYAN, pass_obj.name, COLOR_RESET);
println!("Username:\t{}{}{}", COLOR_MAGENTA, pass_obj.user, COLOR_RESET);
println!("Passphrase:\t{}{}{}", COLOR_GREEN, pass_obj.phrase, COLOR_RESET);
println!("");
}
pub fn int_list(manager: &mut manager::Manager){
// Getting password names
let passwords: Vec<manager::Password> = manager.password_list();
// Listing out passwords
println!(" - {} All Passwords {} - ", COLOR_YELLOW, COLOR_RESET);
for i in 0..passwords.len() {
println!("{0}{1}{2}) {3}{4}{2}\t({5}{6}{2})", COLOR_GREEN, i+1, COLOR_RESET, COLOR_CYAN, passwords[i].name, COLOR_MAGENTA, passwords[i].user);
}
}

View file

@ -1,31 +1,76 @@
// Libraries // Libraries
mod interface; mod interface;
mod resource; mod resource;
mod manager;
mod secure; mod secure;
mod auth; mod auth;
// Functions // Functions
fn stage_authorize(auth_profile: &mut auth::Auth) {
// Entry-Point
fn main() {
// Variables
let auth_profile: auth::Auth;
// Checking if we need to register or log in // Checking if we need to register or log in
if auth::Auth::exists() { if auth::Auth::exists() {
// Asking user for login credentials // Asking user for login credentials
let (username, password) = interface::int_auth(); let (username, password) = interface::int_auth();
// Verify the user // Verify the user
auth_profile = auth::Auth::authenticate(username, password); *auth_profile = auth::Auth::authenticate(username, password);
} else { } else {
// Asking user for register credentials // Asking user for register credentials
let (username, password) = interface::int_reg(); let (username, password) = interface::int_reg();
// Creating the user // 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) {
// Loading the manager
let mut manager: manager::Manager = manager::Manager::init(auth_profile.key.clone());
// 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 == "5" {
println!("Goodbye, {}{}{}!", interface::COLOR_CYAN, auth_profile.username, interface::COLOR_RESET);
break;
} else if page == "4" {
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: {}{}{}", interface::COLOR_GREEN, gen_pass, interface::COLOR_RESET);
} else if page == "3" {
let (pass_name, pass_user, pass_phrase) = interface::int_create();
manager.password_create(pass_name.clone(), pass_user, pass_phrase);
println!("\nSuccessfully added {}{}{}!", interface::COLOR_GREEN, pass_name, interface::COLOR_RESET);
} else if page == "2" {
interface::int_view(&mut manager);
} else if page == "1" {
interface::int_list(&mut manager);
} else {
println!("ERROR: Unrecognized Page!");
}
}
}
// Entry-Point
fn main() {
// Authorizing User
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);
// Before going to the accounts page, check if the auth failed
if !auth_profile.verified {
println!("\n{}Invalid Credentials{}, Please try again!\n", interface::COLOR_RED, interface::COLOR_RESET);
} else {break;}
} }
// DEBUG: Testing if the user is authenticated println!("");
println!("Authenticated Status: {}", auth_profile.verified);
// Passwords interface
stage_manager(&mut auth_profile);
} }

146
src/manager.rs Normal file
View file

@ -0,0 +1,146 @@
// Libraries
use bincode;
use rand::Rng;
use serde::{Serialize, Deserialize};
use crate::{interface, resource, secure};
// 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>,
key: String
}
// Implementations
impl Manager {
// Constructors
pub fn init(key: String) -> Self {
let mut manager: Manager = Manager { passwords: vec![], key: key };
if resource::ResourceFileBin::exists("res/manager.dat") {
manager.load();
}
return 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(&enc_manager).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");
// 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);
}
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()-1);
let rand_character: char = possible_characters.chars().nth(rand_index).unwrap();
result += &rand_character.to_string();
}
// Giving back the result
return result;
}
pub fn password_create(&mut self, name: String, user: String, pass: String) {
// Create the password object
let new_password: Password = Password{name: name, user: user, phrase: pass};
// 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
let mut success: bool = false;
let mut selected_password: Password = Password { name: String::new(), user: String::new(), phrase: String::new() };
// Getting the right password
for c_pass in &self.passwords {
success = name.to_lowercase() == c_pass.name.to_lowercase();
if success {
selected_password.name = c_pass.name.clone();
selected_password.user = c_pass.user.clone();
selected_password.phrase = c_pass.phrase.clone();
break;
}
}
// Returning the password
return (success, selected_password);
}
pub fn password_list(&mut self) -> Vec<Password> {
// Result variable
let mut result: Vec<Password> = Vec::new();
// Going through all passwords and adding them to the list
for password in &self.passwords {
result.push(Password {
name: password.name.clone(),
user: password.user.clone(),
phrase: String::from("[REDACTED]")
});
}
// Giving back result
return result;
}
}

View file

@ -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};
}
}