summaryrefslogtreecommitdiff
path: root/src/core/hash.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-03-04 21:26:04 +0800
committer魏曹先生 <1992414357@qq.com>2026-03-04 21:35:09 +0800
commit22926ce29e3f8e040ec349401aeb6a77f32eae72 (patch)
tree678753ec49a61fb9d3e2d8e869393dec90ea7ef4 /src/core/hash.rs
Initialize Butchunker project structure and policy system
Diffstat (limited to 'src/core/hash.rs')
-rw-r--r--src/core/hash.rs38
1 files changed, 38 insertions, 0 deletions
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()
+}