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
use blake2::{Blake2b512, Digest};

use crate::crypto::CryptoError;
use crate::enc;

/// A Blake2b512 digest
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Blake2Sum([u8; 64]);

impl Blake2Sum {
    /// Create a Blake2Sum object by passing the digest as bytes directly
    pub fn from_bytes(bytes: [u8; 64]) -> Self {
        Self(bytes)
    }

    /// Compute the Blake2b512 digest of a byte slice
    pub fn compute(buf: &[u8]) -> Self {
        let mut hasher = Blake2b512::new();
        hasher.update(buf);
        Self(hasher.finalize()[..].try_into().unwrap())
    }

    /// Check that this digest corresponds to a given slice
    pub fn verify(&self, buf: &[u8]) -> Result<(), CryptoError> {
        if Self::compute(buf) == *self {
            Ok(())
        } else {
            Err(CryptoError::InvalidHash)
        }
    }

    /// Return a reference to the inner byte slice
    pub fn as_bytes(&self) -> &[u8] {
        &self.0[..]
    }
}

impl enc::Encode for Blake2Sum {
    fn term(&self) -> enc::Result<'_> {
        Ok(enc::bytes(self.as_bytes()))
    }
}