use alloc::boxed::Box;
use rustls::crypto::{self, HashAlgorithm};
use crate::sm3::Sm3Hasher;
pub(crate) static SM3: Sm3Hash = Sm3Hash;
pub(crate) struct Sm3Hash;
impl crypto::hash::Hash for Sm3Hash {
fn start(&self) -> Box<dyn crypto::hash::Context> {
Box::new(Sm3Context(Sm3Hasher::new()))
}
fn hash(&self, data: &[u8]) -> crypto::hash::Output {
crypto::hash::Output::new(&Sm3Hasher::digest(data))
}
fn output_len(&self) -> usize {
32
}
fn algorithm(&self) -> HashAlgorithm {
HashAlgorithm::Unknown(0x07)
}
}
struct Sm3Context(Sm3Hasher);
impl crypto::hash::Context for Sm3Context {
fn fork_finish(&self) -> crypto::hash::Output {
crypto::hash::Output::new(&self.0.clone().finalize())
}
fn fork(&self) -> Box<dyn crypto::hash::Context> {
Box::new(Sm3Context(self.0.clone()))
}
fn finish(self: Box<Self>) -> crypto::hash::Output {
crypto::hash::Output::new(&self.0.finalize())
}
fn update(&mut self, data: &[u8]) {
self.0.update(data);
}
}