31 lines
775 B
Rust
31 lines
775 B
Rust
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 {
|
|
// At the time of this writing, the sha2 library is stuck on an old version
|
|
// of generic_array (0.9.0). Decouple ourselves with a clone to our version.
|
|
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())
|
|
}
|
|
}
|