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
use core::mem::size_of;
use zerocopy::{AsBytes, FromBytes};
#[derive(AsBytes, FromBytes, PartialEq, Debug)]
#[repr(C)]
pub struct Hash(pub [u8; 32]);
impl Hash {
pub const SIZE: usize = size_of::<Self>();
pub fn from_slice(s: &[u8]) -> Option<Self> {
if s.len() == Self::SIZE {
let mut out = Self([0; Self::SIZE]);
out.0.copy_from_slice(s);
Some(out)
} else {
None
}
}
#[cfg(feature = "b64")]
pub fn from_base64(mut s: &str) -> Option<Self> {
let mut buf = [0; Self::SIZE];
if s.starts_with('%') || s.starts_with('&') {
s = &s[1..];
}
if crate::b64::decode(s, &mut buf, Some(".sha256")) {
Some(Self(buf))
} else {
None
}
}
#[cfg(feature = "alloc")]
pub fn as_base64(&self) -> alloc::string::String {
base64::encode_config(&self.0[..], base64::STANDARD)
}
}
#[cfg(all(feature = "dalek", not(feature = "force_sodium")))]
pub use crate::dalek::hash::hash;
#[cfg(all(
feature = "sodium",
any(feature = "force_sodium", not(feature = "dalek"))
))]
pub use crate::sodium::hash::hash;