use super::{Algorithm, AlgorithmError, AlgorithmErrorKind::TagInvalid};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[allow(non_camel_case_types)]
#[repr(u8)]
pub enum HmacAlg {
SHA1 = 0x13,
SHA256 = 0x14,
SHA384 = 0x15,
SHA512 = 0x16,
}
impl HmacAlg {
pub fn from_u8(tag: u8) -> Result<Self, AlgorithmError> {
Ok(match tag {
0x13 => HmacAlg::SHA1,
0x14 => HmacAlg::SHA256,
0x15 => HmacAlg::SHA384,
0x16 => HmacAlg::SHA512,
_ => fail!(TagInvalid, "unknown HMAC algorithm ID: 0x{:02x}", tag),
})
}
pub fn to_u8(self) -> u8 {
self as u8
}
pub fn key_len(self) -> usize {
match self {
HmacAlg::SHA1 => 20,
HmacAlg::SHA256 => 32,
HmacAlg::SHA384 => 48,
HmacAlg::SHA512 => 64,
}
}
pub fn max_key_len(self) -> usize {
match self {
HmacAlg::SHA1 => 64,
HmacAlg::SHA256 => 64,
HmacAlg::SHA384 => 128,
HmacAlg::SHA512 => 128,
}
}
}
impl From<HmacAlg> for Algorithm {
fn from(alg: HmacAlg) -> Algorithm {
Algorithm::Hmac(alg)
}
}
impl_algorithm_serializers!(HmacAlg);