From 58dea954312d8c3ce46e4159eacbd243b11c2729 Mon Sep 17 00:00:00 2001 From: German Lashevich Date: Tue, 3 Apr 2018 20:53:21 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Ask=20for=20database=20password?= =?UTF-8?q?=20lazily?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bin/totp.rs | 3 +-- src/lib.rs | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/bin/totp.rs b/src/bin/totp.rs index e5df92d..7e63d22 100644 --- a/src/bin/totp.rs +++ b/src/bin/totp.rs @@ -20,8 +20,7 @@ struct Cli {} impl Cli { fn app() -> RusTOTPony { - let secret = Self::get_secret(); - let db = JsonDatabase::new(Self::get_database_path(), &secret); + let db = JsonDatabase::new(Self::get_database_path(), &Self::get_secret); RusTOTPony::new(db) } diff --git a/src/lib.rs b/src/lib.rs index 6d04bfd..4f9150e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,15 +130,15 @@ struct DatabaseContentSchema { pub struct JsonDatabase { file_path: PathBuf, - secret: String, + secret_fn: &'static Fn() -> String, } const IV_SIZE: usize = 16; impl JsonDatabase { - pub fn new(path: PathBuf, secret: &str) -> JsonDatabase { + pub fn new(path: PathBuf, secret_fn: &'static Fn() -> String) -> JsonDatabase { JsonDatabase { file_path: path, - secret: String::from(secret), + secret_fn: secret_fn, } } @@ -148,7 +148,7 @@ impl JsonDatabase { Err(ref err) if err.kind() == ErrorKind::NotFound => return Self::get_empty_schema(), Err(err) => panic!("There was a problem opening file: {:?}", err), }; - let decrypted_data = Self::decrypt_data(&data, self.secret.as_str()); + let decrypted_data = Self::decrypt_data(&data, (self.secret_fn)().as_str()); serde_json::from_str(decrypted_data.as_str()) .expect("Couldn't parse JSON from database file") } @@ -184,7 +184,7 @@ impl JsonDatabase { Err(err) => panic!("Couldn't open database file: {:?}", err), }; let data = serde_json::to_string(&content).expect("Couldn't serialize data to JSON"); - let encrypted_data = Self::encrypt_data(&data, &self.secret); + let encrypted_data = Self::encrypt_data(&data, &(self.secret_fn)()); file.write_all(&encrypted_data) .expect("Couldn't write data to database file"); }