summaryrefslogtreecommitdiff
path: root/src/chunker/rw/storage/hash.rs
blob: 8eaa64160f570f124b13707db28e89933b02ab7d (plain)
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
use blake3::Hasher as Blake3Hasher;
use sha2::{Digest as Sha2Digest, Sha256};

const SALT: &[u8] = b"Dude@";

#[derive(Debug, Default, Clone, Copy)]
pub enum ChunkWriteHash {
    #[default]
    Blake3,
    Sha256,
}

impl ChunkWriteHash {
    pub fn hash(&self, d: &[u8]) -> [u8; 32] {
        match self {
            ChunkWriteHash::Blake3 => hash_blake3(d),
            ChunkWriteHash::Sha256 => hash_sha256(d),
        }
    }
}

/// Compute the Blake3 hash of the data with a salt
/// Returns a 32-byte hash value
pub fn hash_blake3(d: &[u8]) -> [u8; 32] {
    let mut hasher = Blake3Hasher::new();
    hasher.update(SALT);
    hasher.update(d);
    *hasher.finalize().as_bytes()
}

/// Compute the SHA-256 hash of the data with a salt
/// Returns a 32-byte hash value
pub fn hash_sha256(d: &[u8]) -> [u8; 32] {
    let mut hasher = Sha256::new();
    hasher.update(SALT);
    hasher.update(d);
    hasher.finalize().into()
}