use crate::algorithm;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[allow(non_camel_case_types)]
#[repr(u8)]
pub enum Algorithm {
SHA1 = 0x13,
SHA256 = 0x14,
SHA384 = 0x15,
SHA512 = 0x16,
}
impl Algorithm {
pub fn from_u8(tag: u8) -> Result<Self, algorithm::Error> {
Ok(match tag {
0x13 => Algorithm::SHA1,
0x14 => Algorithm::SHA256,
0x15 => Algorithm::SHA384,
0x16 => Algorithm::SHA512,
_ => fail!(
algorithm::ErrorKind::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 {
Algorithm::SHA1 => 20,
Algorithm::SHA256 => 32,
Algorithm::SHA384 => 48,
Algorithm::SHA512 => 64,
}
}
pub fn max_key_len(self) -> usize {
match self {
Algorithm::SHA1 => 64,
Algorithm::SHA256 => 64,
Algorithm::SHA384 => 128,
Algorithm::SHA512 => 128,
}
}
}
impl_algorithm_serializers!(Algorithm);