Skip to content

Commit

Permalink
password now stored in own file
Browse files Browse the repository at this point in the history
  • Loading branch information
gwirn committed Apr 29, 2023
1 parent ab2af0c commit 379d4ae
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
12 changes: 6 additions & 6 deletions src/date_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::encrypt::{gen_key_pwd, read_bin, write_bin};
use crate::{PASSWORD, SALT_LOC};
use crate::encrypt::{gen_key_pwd, get_pwd_file, read_bin, write_bin};
use crate::{PWD_LOC, SALT_LOC};
use chrono::{DateTime, Local, NaiveDateTime, Utc};
use notify_rust::Notification;
use orion::{aead, kdf};
Expand Down Expand Up @@ -91,7 +91,7 @@ pub fn append_file(filepath: &str, line: &str) {
dec_d
} else {
let (_, key) = gen_key_pwd(
&PASSWORD,
&get_pwd_file(&PWD_LOC),
orion::kdf::Salt::from_slice(read_bin(&SALT_LOC).as_ref())
.expect("Couldn't retrieve salt from file"),
);
Expand All @@ -106,7 +106,7 @@ pub fn append_file(filepath: &str, line: &str) {
for i in file_line.as_bytes() {
dec_data.push(*i);
}
let (salt, key) = gen_key_pwd(&PASSWORD, orion::kdf::Salt::default());
let (salt, key) = gen_key_pwd(&get_pwd_file(&PWD_LOC), orion::kdf::Salt::default());
write_bin(&salt.as_ref().to_vec(), &SALT_LOC);
let cipher_text = aead::seal(&key, &dec_data).expect("Couldn't encrypt the data");
write_bin(&cipher_text, &filepath);
Expand All @@ -123,7 +123,7 @@ pub fn read_file(filepath: &str) -> Vec<SavedDate> {
.expect("Couldn't check salt file for existance")
{
let (_, key) = gen_key_pwd(
&PASSWORD,
&get_pwd_file(&PWD_LOC),
orion::kdf::Salt::from_slice(read_bin(&SALT_LOC).as_ref())
.expect("Couldn't retrieve salt from file"),
);
Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn remove_entry(rm_id: &str, file_path: &str) {
}
}
}
let (salt, key) = gen_key_pwd(&PASSWORD, orion::kdf::Salt::default());
let (salt, key) = gen_key_pwd(&get_pwd_file(&PWD_LOC), orion::kdf::Salt::default());
write_bin(&salt.as_ref().to_vec(), &SALT_LOC);
let cipher_text = aead::seal(&key, &removed).expect("Couldn't encrypt the data");
write_bin(&cipher_text, &file_path);
Expand Down
9 changes: 9 additions & 0 deletions src/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ use orion::{aead, kdf};
use std::fs;
use std::fs::File;
use std::io::prelude::*;

pub fn get_pwd_file(file_path: &str) -> String {
let pwd: String = fs::read_to_string(file_path)
.expect("Couldn't read pwd file")
.trim()
.to_string();
pwd
}

pub fn gen_key_pwd(
my_pwd: &str,
salt: orion::kdf::Salt,
Expand Down
16 changes: 3 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(dead_code)]
use chrono::Datelike;
use chrono::{Local, NaiveDate};
use regex::Regex;
use chrono::Local;
use std::fs;
use std::io;
use std::io::prelude::*;
mod argparse;
mod date_utils;
mod encrypt;
mod view_dates;
mod view_month;
use crate::argparse::argparse;
use crate::date_utils::{
append_file, argsort, check_dates, offset_and_time, read_file, remove_entry, saved_data_header,
time_date_lef, SavedDate,
};
use crate::view_dates::{get_next_n, grep_by_date, grep_by_description, last_added};
use crate::view_month::{appointment_check, month_len, month_view};
use crate::date_utils::read_file;

const DATE_FILE_PATH: &str = "./src/dates/date.file";
const PASSWORD: &str = "mysecretpassword";
const SALT_LOC: &str = "./src/dates/.salt";
const PWD_LOC: &str = "./src/dates/.pwd";

fn main() -> io::Result<()> {
let now = Local::now().date_naive();
Expand Down

0 comments on commit 379d4ae

Please sign in to comment.