crev_data/
digest.rs

1use std::fmt;
2
3/// This is a cryptographic hash of everything in a crate, which reliably identifies its exact source code
4#[derive(Eq, PartialEq, Debug, Clone)]
5pub struct Digest([u8; 32]);
6
7impl From<[u8; 32]> for Digest {
8    fn from(arr: [u8; 32]) -> Self {
9        Self(arr)
10    }
11}
12
13impl Digest {
14    #[must_use]
15    pub fn as_slice(&self) -> &[u8] {
16        &self.0
17    }
18
19    #[must_use]
20    pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
21        if bytes.len() == 32 {
22            let mut out = [0; 32];
23            out.copy_from_slice(bytes);
24            Some(Self(out))
25        } else {
26            None
27        }
28    }
29
30    #[must_use]
31    pub fn into_vec(self) -> Vec<u8> {
32        self.as_slice().to_vec()
33    }
34}
35
36impl fmt::Display for Digest {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        f.write_str(&crev_common::base64_encode(&self.0))
39    }
40}