mod sha2;
pub use self::sha2::Sha256Hash;
use crate::algorithm::SHA256_ALG_ID;
use crate::{
encoding::Encodable,
error::{Error, ErrorKind},
};
use anomaly::fail;
use std::convert::TryInto;
pub enum Hash {
Sha256(Sha256Hash),
}
impl Hash {
pub fn new(alg: &str, bytes: &[u8]) -> Result<Self, Error> {
let result = match alg {
SHA256_ALG_ID => Hash::Sha256(bytes.try_into()?),
_ => fail!(ErrorKind::AlgorithmInvalid, "{}", alg),
};
Ok(result)
}
pub fn sha256_digest(&self) -> Option<&Sha256Hash> {
match self {
Hash::Sha256(ref digest) => Some(digest),
}
}
pub fn is_sha256_digest(&self) -> bool {
self.sha256_digest().is_some()
}
}
impl Encodable for Hash {
fn to_uri_string(&self) -> String {
match self {
Hash::Sha256(ref digest) => digest.to_uri_string(),
}
}
fn to_dasherized_string(&self) -> String {
match self {
Hash::Sha256(ref digest) => digest.to_dasherized_string(),
}
}
}