use super::*;
pub struct NullHash {
hash: BITS256,
}
impl Hasher for NullHash {
fn new() -> Self {
NullHash { hash: [0; 32] }
}
fn digest(&mut self, bytes: impl AsRef<[u8]>) {
let bytes = bytes.as_ref();
let start_point = if bytes.len() < 32 {
0
} else {
bytes.len() - 32
};
let bytes = &bytes[start_point..];
for (loc, byte) in self.hash.iter_mut().zip(bytes) {
*loc = *byte;
}
}
fn complete(self) -> HashReturn {
HashReturn::RAW(Arc::new(self.hash))
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_null_hash() {
let mut hasher = NullHash::new();
for i in 0..32u8 {
hasher.digest([i; 1]);
}
assert_eq!(*hasher.complete().into_bytes().first().unwrap(), 31);
}
#[test]
fn test_null_hash_oversized() {
let mut hasher = NullHash::new();
let mut source_bytes = [0u8; 255];
for (byte, i) in source_bytes.iter_mut().zip(0..=u8::MAX) {
*byte = i;
}
hasher.digest(source_bytes);
let result = hasher.complete().into_bytes();
let start = 255 - 32u8;
for (byte, i) in result.iter().zip(start..=255) {
assert_eq!(*byte, i);
}
}
}