use rustls::crypto::hash::{Context, Hash, HashAlgorithm, Output};
use symcrypt::hash::{sha256, sha384, HashState, Sha256State, Sha384State};
pub struct Sha256;
pub struct Sha256Context(Sha256State);
pub struct Sha384;
struct Sha384Context(Sha384State);
impl Hash for Sha256 {
fn algorithm(&self) -> HashAlgorithm {
HashAlgorithm::SHA256
}
fn output_len(&self) -> usize {
32
}
fn start(&self) -> Box<dyn Context> {
Box::new(Sha256Context(Sha256State::new()))
}
fn hash(&self, data: &[u8]) -> Output {
Output::new(&sha256(data)[..])
}
}
impl Context for Sha256Context {
fn fork_finish(&self) -> Output {
let mut new_context = self.0.clone();
Output::new(&new_context.result()[..])
}
fn fork(&self) -> Box<dyn Context> {
Box::new(Sha256Context(self.0.clone()))
}
fn finish(mut self: Box<Self>) -> Output {
Output::new(&self.0.result()[..])
}
fn update(&mut self, data: &[u8]) {
self.0.append(data);
}
}
impl Hash for Sha384 {
fn algorithm(&self) -> HashAlgorithm {
HashAlgorithm::SHA384
}
fn output_len(&self) -> usize {
48
}
fn start(&self) -> Box<dyn Context> {
Box::new(Sha384Context(Sha384State::new()))
}
fn hash(&self, data: &[u8]) -> Output {
Output::new(&sha384(data)[..])
}
}
impl Context for Sha384Context {
fn fork_finish(&self) -> Output {
let mut new_context = self.0.clone();
Output::new(&new_context.result()[..])
}
fn fork(&self) -> Box<dyn Context> {
Box::new(Sha384Context(self.0.clone()))
}
fn finish(mut self: Box<Self>) -> Output {
Output::new(&self.0.result()[..])
}
fn update(&mut self, data: &[u8]) {
self.0.append(data);
}
}