use crate::DeserializeError;
use std::string::ToString;
use std::{
fmt::{self, Display, Formatter},
str::FromStr,
};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Hash([u8; 32]);
impl Hash {
pub fn new(bytes: [u8; 32]) -> Self {
Self(bytes)
}
pub fn generate(bytes: &[u8]) -> Self {
let hash = blake3::hash(bytes);
Self::new(hash.into())
}
}
impl Display for Hash {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut buffer = [0_u8; 64];
hex::encode_to_slice(self.0, &mut buffer)
.expect("Can never fail with a hard-coded buffer length");
let s = std::str::from_utf8(&buffer).map_err(|_| fmt::Error)?;
f.write_str(s)
}
}
impl FromStr for Hash {
type Err = DeserializeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = hex::decode(s).map_err(|e| {
DeserializeError::Generic(format!(
"Could not decode prehashed key as hexadecimal: {}",
e
))
})?;
if bytes.len() != 32 {
return Err(DeserializeError::Generic(
"Prehashed keys must deserialze into exactly 32 bytes".to_string(),
));
}
Ok(Self(bytes[0..32].try_into().map_err(|e| {
DeserializeError::Generic(format!("Could not get first 32 bytes: {}", e))
})?))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hash_is_displayed_as_hex() {
let original = [
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD,
0xEE, 0xFF, 0x12, 0x65,
];
let hash = Hash::new(original);
assert_eq!(
hash.to_string(),
"aabbccddeeff1265aabbccddeeff1265aabbccddeeff1265aabbccddeeff1265"
);
}
}