🚸 Ask for database password lazily

feature/database-encription
German Lashevich 7 years ago
parent 4c25bad63c
commit 58dea95431
No known key found for this signature in database
GPG Key ID: 3446FAE369C9A8B4

@ -20,8 +20,7 @@ struct Cli {}
impl Cli { impl Cli {
fn app() -> RusTOTPony<JsonDatabase> { fn app() -> RusTOTPony<JsonDatabase> {
let secret = Self::get_secret(); let db = JsonDatabase::new(Self::get_database_path(), &Self::get_secret);
let db = JsonDatabase::new(Self::get_database_path(), &secret);
RusTOTPony::new(db) RusTOTPony::new(db)
} }

@ -130,15 +130,15 @@ struct DatabaseContentSchema {
pub struct JsonDatabase { pub struct JsonDatabase {
file_path: PathBuf, file_path: PathBuf,
secret: String, secret_fn: &'static Fn() -> String,
} }
const IV_SIZE: usize = 16; const IV_SIZE: usize = 16;
impl JsonDatabase { impl JsonDatabase {
pub fn new(path: PathBuf, secret: &str) -> JsonDatabase { pub fn new(path: PathBuf, secret_fn: &'static Fn() -> String) -> JsonDatabase {
JsonDatabase { JsonDatabase {
file_path: path, 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(ref err) if err.kind() == ErrorKind::NotFound => return Self::get_empty_schema(),
Err(err) => panic!("There was a problem opening file: {:?}", err), 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()) serde_json::from_str(decrypted_data.as_str())
.expect("Couldn't parse JSON from database file") .expect("Couldn't parse JSON from database file")
} }
@ -184,7 +184,7 @@ impl JsonDatabase {
Err(err) => panic!("Couldn't open database file: {:?}", err), Err(err) => panic!("Couldn't open database file: {:?}", err),
}; };
let data = serde_json::to_string(&content).expect("Couldn't serialize data to JSON"); 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) file.write_all(&encrypted_data)
.expect("Couldn't write data to database file"); .expect("Couldn't write data to database file");
} }

Loading…
Cancel
Save