use crate::error::{OakError, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HashKind {
Blake3,
GitSha1,
GitSha256,
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Hash(pub String);
impl Hash {
pub fn from_hex(hex: &str) -> Result<Self> {
let len_ok = hex.len() == 40 || hex.len() == 64;
let chars_ok = hex.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'));
if !len_ok || !chars_ok {
return Err(OakError::InvalidHash(hex.to_string()));
}
Ok(Hash(hex.to_string()))
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn short(&self) -> &str {
let end = self
.0
.char_indices()
.map(|(idx, _)| idx)
.nth(12)
.unwrap_or(self.0.len());
&self.0[..end]
}
pub fn kind(&self) -> HashKind {
match self.0.len() {
40 => HashKind::GitSha1,
64 => HashKind::Blake3,
_ => HashKind::Unknown,
}
}
}
impl std::fmt::Display for Hash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl serde::Serialize for Hash {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.0)
}
}
impl<'de> serde::Deserialize<'de> for Hash {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Hash::from_hex(&s).map_err(serde::de::Error::custom)
}
}
pub fn hash_bytes(data: &[u8]) -> Hash {
let hash = blake3::hash(data);
Hash(hash.to_hex().to_string())
}
pub fn hash_string(s: &str) -> Hash {
hash_bytes(s.as_bytes())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hash_determinism() {
let data = b"hello world";
let hash1 = hash_bytes(data);
let hash2 = hash_bytes(data);
assert_eq!(hash1, hash2);
}
#[test]
fn test_hash_different_inputs() {
let hash1 = hash_bytes(b"hello");
let hash2 = hash_bytes(b"world");
assert_ne!(hash1, hash2);
}
#[test]
fn test_hash_length() {
let hash = hash_bytes(b"test");
assert_eq!(hash.as_str().len(), 64);
}
#[test]
fn test_hash_from_hex_blake3() {
let valid_hex = "a".repeat(64);
let h = Hash::from_hex(&valid_hex).unwrap();
assert_eq!(h.kind(), HashKind::Blake3);
}
#[test]
fn test_hash_from_hex_git_sha1() {
let valid_hex = "a".repeat(40);
let h = Hash::from_hex(&valid_hex).unwrap();
assert_eq!(h.kind(), HashKind::GitSha1);
}
#[test]
fn test_hash_from_hex_invalid_length() {
assert!(Hash::from_hex("abc").is_err());
assert!(Hash::from_hex(&"a".repeat(50)).is_err());
}
#[test]
fn test_hash_from_hex_invalid_chars() {
let invalid_hex = "g".repeat(64);
assert!(Hash::from_hex(&invalid_hex).is_err());
}
#[test]
fn test_hash_short() {
let hash = hash_bytes(b"test");
assert_eq!(hash.short().len(), 12);
}
#[test]
fn test_hash_short_handles_non_ascii_without_panicking() {
let hash = Hash("é".repeat(20));
assert_eq!(hash.short(), "éééééééééééé");
}
#[test]
fn test_hash_kind_reports_unknown_length() {
assert_eq!(Hash("abc".to_string()).kind(), HashKind::Unknown);
}
#[test]
fn test_hash_from_hex_rejects_uppercase() {
assert!(Hash::from_hex(&"A".repeat(64)).is_err());
assert!(Hash::from_hex(&format!("{}F", "a".repeat(63))).is_err());
}
#[test]
fn test_deserialize_valid_hex() {
let blake3 = format!("\"{}\"", "a".repeat(64));
let h: Hash = serde_json::from_str(&blake3).unwrap();
assert_eq!(h.kind(), HashKind::Blake3);
let sha1 = format!("\"{}\"", "0123456789abcdef0123".repeat(2));
let h: Hash = serde_json::from_str(&sha1).unwrap();
assert_eq!(h.kind(), HashKind::GitSha1);
let original = hash_bytes(b"round trip");
let json = serde_json::to_string(&original).unwrap();
let back: Hash = serde_json::from_str(&json).unwrap();
assert_eq!(original, back);
}
#[test]
fn test_deserialize_rejects_junk() {
let cases = [
"\"\"".to_string(),
"\"not a hash\"".to_string(),
format!("\"{}\"", "a".repeat(63)), format!("\"{}\"", "a".repeat(65)), format!("\"{}\"", "A".repeat(64)), format!("\"{}\"", "g".repeat(64)), format!("\"{}\\t{}\"", "a".repeat(31), "a".repeat(32)), format!("\"{}\\n{}\"", "a".repeat(31), "a".repeat(32)), ];
for json in cases {
assert!(
serde_json::from_str::<Hash>(&json).is_err(),
"should reject {json}"
);
}
}
}