37 lines
831 B
Rust
37 lines
831 B
Rust
|
pub enum CryptoError {
|
||
|
InvalidHash,
|
||
|
InvalidSignature,
|
||
|
}
|
||
|
|
||
|
#[cfg(feature = "blake2")]
|
||
|
mod b2 {
|
||
|
use super::CryptoError;
|
||
|
use blake2::{Blake2b512, Digest};
|
||
|
|
||
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
||
|
pub struct Blake2Sum([u8; 64]);
|
||
|
|
||
|
impl Blake2Sum {
|
||
|
pub fn from_bytes(bytes: [u8; 64]) -> Self {
|
||
|
Self(bytes)
|
||
|
}
|
||
|
|
||
|
pub fn compute(buf: &[u8]) -> Self {
|
||
|
let mut hasher = Blake2b512::new();
|
||
|
hasher.update(buf);
|
||
|
Self(hasher.finalize()[..].try_into().unwrap())
|
||
|
}
|
||
|
|
||
|
pub fn check(&self, buf: &[u8]) -> Result<(), CryptoError> {
|
||
|
if Self::compute(buf) == *self {
|
||
|
Ok(())
|
||
|
} else {
|
||
|
Err(CryptoError::InvalidHash)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(feature = "blake2")]
|
||
|
pub use b2::*;
|