ezidam/crates/hash/src/paper_key.rs

26 lines
695 B
Rust

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)
}
}