chore(vault): provide default for TotpAlgorithm

The default is "None", which will fail any conversion or usage
This commit is contained in:
Gianguido Sorà 2022-07-18 17:31:32 +02:00
parent 222a912603
commit 5f7fb22317

View file

@ -17,13 +17,22 @@ pub enum TotpAlgorithm {
HmacSha1,
HmacSha256,
HmacSha512,
None,
}
impl Default for TotpAlgorithm {
fn default() -> Self {
Self::None
}
}
impl std::fmt::Debug for TotpAlgorithm {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
TotpAlgorithm::HmacSha1 => write!(f, "SHA1"),
TotpAlgorithm::HmacSha256 => write!(f, "SHA256"),
TotpAlgorithm::HmacSha512 => write!(f, "SHA512"),
TotpAlgorithm::None => write!(f, "None"),
}
}
}
@ -45,6 +54,7 @@ impl Into<String> for TotpAlgorithm {
TotpAlgorithm::HmacSha1 => "SHA1".to_string(),
TotpAlgorithm::HmacSha256 => "SHA256".to_string(),
TotpAlgorithm::HmacSha512 => "SHA512".to_string(),
TotpAlgorithm::None => "None".to_string(),
}
}
}
@ -112,6 +122,9 @@ fn generate_hmac_bytes(unix_timestamp: u64, totp_entry: &TotpEntry) -> Result<Ve
let hash: &[u8] = &mac.finalize().into_bytes();
computed_hmac.extend_from_slice(hash);
}
TotpAlgorithm::None => {
panic!("cannot generate hmac bytes for None algorithm")
}
}
Ok(computed_hmac)