1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::DeserializeError;
use std::str::FromStr;
use std::string::ToString;
#[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())
}
pub(crate) fn to_array(self) -> [u8; 32] {
self.0
}
}
impl ToString for Hash {
fn to_string(&self) -> String {
hex::encode(&self.to_array())
}
}
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(),
));
}
use std::convert::TryInto;
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_to_array_works() {
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_array(), original);
}
}