From 22926ce29e3f8e040ec349401aeb6a77f32eae72 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 4 Mar 2026 21:26:04 +0800 Subject: Initialize Butchunker project structure and policy system --- src/core/hash.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/core/hash.rs (limited to 'src/core/hash.rs') diff --git a/src/core/hash.rs b/src/core/hash.rs new file mode 100644 index 0000000..36a62b3 --- /dev/null +++ b/src/core/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)] +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() +} -- cgit