use rustls::crypto::hmac::{Hmac, Key, Tag};
use symcrypt::hmac::{HmacSha256State, HmacSha384State, HmacState};
pub struct HmacSha256;
pub struct HmacSha256Key(HmacSha256State);
pub struct HmacSha384;
pub struct HmacSha384Key(HmacSha384State);
impl Hmac for HmacSha256 {
fn with_key(&self, key: &[u8]) -> Box<dyn Key> {
Box::new(HmacSha256Key(HmacSha256State::new(key).unwrap()))
}
fn hash_output_len(&self) -> usize {
32
}
}
impl Key for HmacSha256Key {
fn sign(&self, data: &[&[u8]]) -> Tag {
self.sign_concat(&[], data, &[])
}
fn sign_concat(&self, first: &[u8], middle: &[&[u8]], last: &[u8]) -> Tag {
let mut new_state = self.0.clone();
new_state.append(first);
for d in middle {
new_state.append(d);
}
new_state.append(last);
let result = new_state.result();
Tag::new(&result)
}
fn tag_len(&self) -> usize {
32
}
}
impl Hmac for HmacSha384 {
fn with_key(&self, key: &[u8]) -> Box<dyn Key> {
Box::new(HmacSha384Key(HmacSha384State::new(key).unwrap()))
}
fn hash_output_len(&self) -> usize {
48
}
}
impl Key for HmacSha384Key {
fn sign(&self, data: &[&[u8]]) -> Tag {
self.sign_concat(&[], data, &[])
}
fn sign_concat(&self, first: &[u8], middle: &[&[u8]], last: &[u8]) -> Tag {
let mut new_state = self.0.clone();
new_state.append(first);
for d in middle {
new_state.append(d);
}
new_state.append(last);
let result = new_state.result();
Tag::new(&result)
}
fn tag_len(&self) -> usize {
48
}
}