use {
sha2::{Digest, Sha256},
std::{convert::TryFrom, fmt},
};
const HASH_BYTES: usize = 32;
#[derive(AbiExample)]
pub struct Hash(pub [u8; HASH_BYTES]);
#[derive(Default)]
pub struct Hasher {
hasher: Sha256,
}
impl Hasher {
pub fn hash(&mut self, val: &[u8]) {
self.hasher.update(val);
}
pub fn result(self) -> Hash {
Hash(<[u8; HASH_BYTES]>::try_from(self.hasher.finalize().as_slice()).unwrap())
}
}
impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", bs58::encode(self.0).into_string())
}
}