use sha2::{Digest, Sha256};
pub const BLAKE3_PREFIX: &str = "blake3:";
pub const SHA256_PREFIX: &str = "sha256:";
pub fn compute_blake3(bytes: &[u8]) -> String {
let hash = blake3::hash(bytes);
hash.to_hex().to_string()
}
pub fn compute_sha256(bytes: &[u8]) -> String {
let mut hasher = Sha256::new();
hasher.update(bytes);
hex::encode(hasher.finalize())
}
pub fn prefixed_hash(bytes: &[u8]) -> String {
format!("{}{}", BLAKE3_PREFIX, compute_blake3(bytes))
}
pub fn verify_hash(bytes: &[u8], expected_hash: &str) -> bool {
let expected = expected_hash.trim();
if let Some(stripped) = expected.strip_prefix(BLAKE3_PREFIX) {
compute_blake3(bytes).eq_ignore_ascii_case(stripped)
} else if let Some(stripped) = expected.strip_prefix(SHA256_PREFIX) {
compute_sha256(bytes).eq_ignore_ascii_case(stripped)
} else if expected.len() == 64 {
compute_blake3(bytes).eq_ignore_ascii_case(expected)
|| compute_sha256(bytes).eq_ignore_ascii_case(expected)
} else {
false
}
}