use crate::error::TryFromError;
use std::fmt;
use std::convert::{TryFrom, TryInto};
use poly1305::Tag;
#[derive(Clone, PartialEq, Eq)]
pub struct Mac {
tag: Tag
}
impl Mac {
pub const LEN: usize = 16;
pub(crate) fn new(tag: Tag) -> Self {
Self { tag }
}
pub fn from_slice(slice: &[u8]) -> Self {
slice.try_into().unwrap()
}
pub fn into_bytes(self) -> [u8; 16] {
self.tag.into()
}
}
impl fmt::Debug for Mac {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Mac")
}
}
impl From<[u8; 16]> for Mac {
fn from(bytes: [u8; 16]) -> Self {
Self { tag: bytes.into() }
}
}
impl TryFrom<&[u8]> for Mac {
type Error = TryFromError;
fn try_from(s: &[u8]) -> Result<Self, Self::Error> {
<[u8; 16]>::try_from(s)
.map_err(TryFromError::from_any)
.map(Mac::from)
}
}