blake3_hasher/
lib.rs

1//! Hasher implementation for the Blake3-256 hash
2
3use hash256_std_hasher::Hash256StdHasher;
4use hash_db::Hasher;
5
6/// The `Blake3` hash output type.
7pub type Blake3Hash = [u8; 32];
8
9/// Concrete `Hasher` impl for the Blake3-256 hash
10#[derive(Default, Debug, Clone, PartialEq)]
11pub struct Blake3Hasher;
12impl Hasher for Blake3Hasher {
13	type Out = Blake3Hash;
14
15	type StdHasher = Hash256StdHasher;
16
17	const LENGTH: usize = 32;
18
19	fn hash(x: &[u8]) -> Self::Out {
20		blake3::hash(x).into()
21	}
22}
23
24#[cfg(test)]
25mod tests {
26	use super::*;
27	use std::collections::HashMap;
28
29	#[test]
30	fn hash256_std_hasher_works() {
31		let hello_bytes = b"Hello world!";
32		let hello_key = Blake3Hasher::hash(hello_bytes);
33
34		let mut h: HashMap<<Blake3Hasher as Hasher>::Out, Vec<u8>> = Default::default();
35		h.insert(hello_key, hello_bytes.to_vec());
36		h.remove(&hello_key);
37
38		let mut h: HashMap<
39			<Blake3Hasher as Hasher>::Out,
40			Vec<u8>,
41			std::hash::BuildHasherDefault<Hash256StdHasher>,
42		> = Default::default();
43		h.insert(hello_key, hello_bytes.to_vec());
44		h.remove(&hello_key);
45	}
46}