1use alloy_primitives::{keccak256, B256};
2
3pub trait KeyHasher: Default + Clone + Send + Sync + 'static {
5 fn hash_key<T: AsRef<[u8]>>(bytes: T) -> B256;
7}
8
9#[derive(Clone, Debug, Default)]
11pub struct KeccakKeyHasher;
12
13impl KeyHasher for KeccakKeyHasher {
14 #[inline]
15 fn hash_key<T: AsRef<[u8]>>(bytes: T) -> B256 {
16 keccak256(bytes)
17 }
18}
19
20#[cfg(test)]
21mod tests {
22 use super::*;
23 use alloy_primitives::Address;
24
25 #[test]
26 fn test_keccak_key_hasher_always_hashes_regardless_of_length() {
27 let addr = Address::repeat_byte(0x42);
28 assert_eq!(KeccakKeyHasher::hash_key(addr), keccak256(addr));
29
30 let slot = B256::repeat_byte(0x42);
31 assert_eq!(KeccakKeyHasher::hash_key(slot), keccak256(slot));
32 assert_ne!(KeccakKeyHasher::hash_key(slot), slot);
33 }
34}