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