molframe_core/contract/policy/
fingerprint.rs1use std::fmt;
8use std::hash::Hasher;
9
10const FNV1A64_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
12const FNV1A64_PRIME: u64 = 0x0000_0100_0000_01b3;
14
15#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
17pub struct Fingerprint(u64);
18
19impl Fingerprint {
20 pub(super) const fn from_hash(hash: u64) -> Self {
22 Self(hash)
23 }
24
25 #[must_use]
27 pub fn of(bytes: &[u8]) -> Self {
28 let mut hasher = Fnv1a::new();
29 hasher.write(bytes);
30 Self(hasher.finish())
31 }
32
33 #[must_use]
35 pub const fn get(self) -> u64 {
36 self.0
37 }
38}
39
40impl fmt::Display for Fingerprint {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 write!(f, "fnv1a64:{:016x}", self.0)
43 }
44}
45
46impl fmt::Debug for Fingerprint {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 write!(f, "{self}")
49 }
50}
51
52pub(super) struct Fnv1a(u64);
58
59impl Fnv1a {
60 pub(super) const fn new() -> Self {
61 Self(FNV1A64_OFFSET_BASIS)
62 }
63}
64
65impl Hasher for Fnv1a {
66 fn finish(&self) -> u64 {
67 self.0
68 }
69
70 fn write(&mut self, bytes: &[u8]) {
71 for byte in bytes {
72 self.0 ^= u64::from(*byte);
73 self.0 = self.0.wrapping_mul(FNV1A64_PRIME);
76 }
77 }
78}