You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1002 B
37 lines
1002 B
use super::*;
|
|
use std::hash::{BuildHasherDefault, Hasher};
|
|
use smallvec::SmallVec;
|
|
use cryptohelpers::sha256;
|
|
|
|
/// A hasher that takes the first 8 bytes from SHA256 hash as its output.
|
|
///
|
|
/// # Notes
|
|
/// Intended for use for `HashSet` with a SHA256 key.
|
|
/// Hashing anything other than a SHA256 hash with this hasher is undefined.
|
|
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Default)]
|
|
pub struct Sha256TopHasher(SmallVec<[u8; sha256::SIZE]>);
|
|
|
|
/// A `BuildHasher` for [`Sha256TopHasher`].
|
|
pub type Sha256TopBuildHasher = BuildHasherDefault<Sha256TopHasher>;
|
|
|
|
impl Sha256TopHasher
|
|
{
|
|
/// Create a new hasher
|
|
#[inline] fn new() -> Self
|
|
{
|
|
Self(SmallVec::new())
|
|
}
|
|
}
|
|
|
|
impl Hasher for Sha256TopHasher
|
|
{
|
|
#[inline] fn finish(&self) -> u64 {
|
|
let mut bytes = [0u8; std::mem::size_of::<u64>()];
|
|
crate::slice::copy_bytes(self.0.as_ref(), &mut bytes[..]);
|
|
u64::from_le_bytes(bytes)
|
|
}
|
|
#[inline] fn write(&mut self, bytes: &[u8]) {
|
|
self.0.extend_from_slice(bytes);
|
|
}
|
|
}
|