pod_types/cryptography/
hash.rs1use crate::Timestamp;
2use alloy_primitives::Address;
3pub use alloy_primitives::{B256 as Hash, keccak256 as hash};
4use bytes::Bytes;
5use std::hash::{DefaultHasher, Hash as StdHash, Hasher};
6
7pub trait Hashable {
8 fn hash_custom(&self) -> Hash;
9}
10
11impl Hashable for Vec<u8> {
12 fn hash_custom(&self) -> Hash {
13 hash(self)
14 }
15}
16
17impl Hashable for Bytes {
18 fn hash_custom(&self) -> Hash {
19 hash(self)
20 }
21}
22
23impl Hashable for Address {
24 fn hash_custom(&self) -> Hash {
25 hash(self.0)
26 }
27}
28
29impl Hashable for u128 {
30 fn hash_custom(&self) -> Hash {
31 hash(self.to_be_bytes())
32 }
33}
34
35impl Hashable for Timestamp {
36 fn hash_custom(&self) -> Hash {
37 hash(self.as_micros().to_be_bytes())
38 }
39}
40
41impl Hashable for (u64, u128) {
42 fn hash_custom(&self) -> Hash {
43 let mut bytes = [0u8; 24];
44 bytes[..8].copy_from_slice(&self.0.to_be_bytes());
45 bytes[8..].copy_from_slice(&self.1.to_be_bytes());
46 hash(bytes)
47 }
48}
49
50impl Hashable for Hash {
51 fn hash_custom(&self) -> Hash {
52 *self
53 }
54}
55
56pub fn std_hash<H: StdHash>(h: H) -> u64 {
57 let mut hasher = DefaultHasher::new();
58 h.hash(&mut hasher);
59 hasher.finish()
60}