diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-07 19:37:52 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-07 19:37:52 +0800 |
| commit | 9e7c0fd45e169929156bdb317b10d7bb3db65f8b (patch) | |
| tree | 94c1e0e6cafe996b7b7da8dfd6e1ff1a04539cda /src/chunker/rw/storage/hash.rs | |
| parent | 22926ce29e3f8e040ec349401aeb6a77f32eae72 (diff) | |
Add callback support to chunk_stream_with and implement stream writing
Diffstat (limited to 'src/chunker/rw/storage/hash.rs')
| -rw-r--r-- | src/chunker/rw/storage/hash.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/chunker/rw/storage/hash.rs b/src/chunker/rw/storage/hash.rs new file mode 100644 index 0000000..8eaa641 --- /dev/null +++ b/src/chunker/rw/storage/hash.rs @@ -0,0 +1,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() +} |
