forked from filecoin-project/ref-fvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_algorithm.rs
More file actions
83 lines (70 loc) · 1.92 KB
/
Copy pathhash_algorithm.rs
File metadata and controls
83 lines (70 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2021-2023 Protocol Labs
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use std::hash::Hasher;
use sha2::{Digest, Sha256 as Sha256Hasher};
use crate::{Hash, HashedKey};
/// Algorithm used as the hasher for the Hamt.
pub trait HashAlgorithm {
fn hash<X>(key: &X) -> HashedKey
where
X: Hash + ?Sized;
}
/// Type is needed because the Sha256 hasher does not implement `std::hash::Hasher`
#[derive(Default)]
struct Sha2HasherWrapper(Sha256Hasher);
impl Hasher for Sha2HasherWrapper {
fn finish(&self) -> u64 {
// u64 hash not used in hamt
0
}
fn write(&mut self, bytes: &[u8]) {
self.0.update(bytes);
}
}
/// Sha256 hashing algorithm used for hashing keys in the Hamt.
#[derive(Debug)]
pub enum Sha256 {}
impl HashAlgorithm for Sha256 {
fn hash<X>(key: &X) -> HashedKey
where
X: Hash + ?Sized,
{
let mut hasher = Sha2HasherWrapper::default();
key.hash(&mut hasher);
hasher.0.finalize().into()
}
}
#[cfg(feature = "identity")]
#[derive(Default)]
struct IdentityHasher {
bz: HashedKey,
}
#[cfg(feature = "identity")]
impl Hasher for IdentityHasher {
fn finish(&self) -> u64 {
// u64 hash not used in hamt
0
}
fn write(&mut self, bytes: &[u8]) {
for (i, byte) in bytes.iter().take(self.bz.len()).enumerate() {
self.bz[i] = *byte;
}
}
}
/// Identity hashing algorithm used for hashing keys in the Hamt. This should only be used
/// for testing. The hash is just the first 32 bytes of the serialized key.
#[cfg(feature = "identity")]
#[derive(Debug)]
pub enum Identity {}
#[cfg(feature = "identity")]
impl HashAlgorithm for Identity {
fn hash<X>(key: &X) -> HashedKey
where
X: Hash + ?Sized,
{
let mut ident_hasher = IdentityHasher::default();
key.hash(&mut ident_hasher);
ident_hasher.bz
}
}