settings/security: generate paper key

This commit is contained in:
Philippe Loctaux 2023-04-10 15:17:08 +02:00
parent c1daa34f2c
commit a67c7559b9
12 changed files with 183 additions and 4 deletions

View file

@ -1,8 +1,10 @@
mod error;
mod hash;
mod paper_key;
mod password;
mod secret;
pub use error::Error;
pub use paper_key::PaperKey;
pub use password::Password;
pub use secret::{Secret, SecretString};

View file

@ -0,0 +1,26 @@
use crate::error::Error;
use crate::hash::Hash;
#[derive(Debug)]
pub struct PaperKey(Hash);
impl PaperKey {
pub fn generate() -> Result<Self, Error> {
use gen_passphrase::{dictionary::EFF_SHORT_2, generate};
let passphrase = generate(&[EFF_SHORT_2], 7, Some("-"));
Ok(Self(Hash::from_plain(passphrase)?))
}
pub fn from_hash(hash: impl Into<String>) -> Self {
Self(Hash::from_hash(hash))
}
pub fn plain(&self) -> Option<&str> {
self.0.plain()
}
pub fn hash(&self) -> &str {
self.0.hash()
}
pub fn compare(&self, plain: &str) -> Result<bool, Error> {
self.0.compare(plain).map_err(Error::from)
}
}