use std::hash::Hasher;
use sha2::{Digest, Sha256 as Sha256Hasher};
use crate::{Hash, HashedKey};
pub trait HashAlgorithm {
fn hash<X: ?Sized>(key: &X) -> HashedKey
where
X: Hash;
}
#[derive(Default)]
struct Sha2HasherWrapper(Sha256Hasher);
impl Hasher for Sha2HasherWrapper {
fn finish(&self) -> u64 {
0
}
fn write(&mut self, bytes: &[u8]) {
self.0.update(bytes);
}
}
#[derive(Debug)]
pub enum Sha256 {}
impl HashAlgorithm for Sha256 {
fn hash<X: ?Sized>(key: &X) -> HashedKey
where
X: Hash,
{
let mut hasher = Sha2HasherWrapper::default();
key.hash(&mut hasher);
hasher.0.finalize().into()
}
}
#[cfg(feature = "identity")]
#[derive(Default)]
struct IdentityHasher {
bz: HashedKey,
}
#[cfg(feature = "identity")]
impl Hasher for IdentityHasher {
fn finish(&self) -> u64 {
0
}
fn write(&mut self, bytes: &[u8]) {
for (i, byte) in bytes.iter().take(self.bz.len()).enumerate() {
self.bz[i] = *byte;
}
}
}
#[cfg(feature = "identity")]
#[derive(Debug)]
pub enum Identity {}
#[cfg(feature = "identity")]
impl HashAlgorithm for Identity {
fn hash<X: ?Sized>(key: &X) -> HashedKey
where
X: Hash,
{
let mut ident_hasher = IdentityHasher::default();
key.hash(&mut ident_hasher);
ident_hasher.bz
}
}