pub trait Hash {
const NAME: &'static str;
const HASH_LEN: usize;
fn hash(data: &[u8]) -> Vec<u8>;
fn hmac(key: &[u8], data: &[u8]) -> Vec<u8>;
fn hash_two(a: &[u8], b: &[u8]) -> Vec<u8>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Blake2b;
impl Hash for Blake2b {
const NAME: &'static str = "BLAKE2b";
const HASH_LEN: usize = 64;
fn hash(data: &[u8]) -> Vec<u8> {
use cryptoxide::blake2b::Blake2b as B2b;
use cryptoxide::digest::Digest;
let mut hasher = B2b::new(Self::HASH_LEN);
Digest::input(&mut hasher, data);
let mut out = vec![0u8; Self::HASH_LEN];
Digest::result(&mut hasher, &mut out);
out
}
fn hmac(key: &[u8], data: &[u8]) -> Vec<u8> {
use cryptoxide::blake2b::Blake2b as B2b;
use cryptoxide::hmac::Hmac;
use cryptoxide::mac::Mac;
let mut mac = Hmac::new(B2b::new(Self::HASH_LEN), key);
mac.input(data);
let mut out = vec![0u8; Self::HASH_LEN];
mac.raw_result(&mut out);
out
}
fn hash_two(a: &[u8], b: &[u8]) -> Vec<u8> {
use cryptoxide::blake2b::Blake2b as B2b;
use cryptoxide::digest::Digest;
let mut hasher = B2b::new(Self::HASH_LEN);
Digest::input(&mut hasher, a);
Digest::input(&mut hasher, b);
let mut out = vec![0u8; Self::HASH_LEN];
Digest::result(&mut hasher, &mut out);
out
}
}