use anubis_core::secrecy::{ExposeSecret, SecretBox};
use hmac::{
digest::{CtOutput, MacError},
Hmac, Mac,
};
use sha2::Sha512;
use std::io::{self, Write};
#[cfg(feature = "armor")]
#[cfg_attr(docsrs, doc(cfg(feature = "armor")))]
pub mod armor;
pub mod stream;
pub mod aead;
pub(crate) struct HmacKey(pub(crate) SecretBox<[u8; 64]>);
pub(crate) struct HmacWriter {
inner: Hmac<Sha512>,
}
impl HmacWriter {
pub(crate) fn new(key: HmacKey) -> Self {
HmacWriter {
inner: Hmac::new_from_slice(key.0.expose_secret()).expect("key is the correct length"),
}
}
pub(crate) fn finalize(self) -> CtOutput<Hmac<Sha512>> {
self.inner.finalize()
}
pub(crate) fn verify(self, mac: &[u8]) -> Result<(), MacError> {
self.inner.verify_slice(mac)
}
}
impl Write for HmacWriter {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.inner.update(data);
Ok(data.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}