use crate::Error;
use crate::Result;
use core::fmt;
use core::fmt::Display;
use core::fmt::Formatter;
use sha1::Sha1;
use sha2::digest::DynDigest;
use sha2::Digest;
use sha2::Sha256;
use std::str::FromStr;
#[repr(C)]
#[derive(Clone, Copy, PartialOrd, Eq, Ord, Debug, Hash, PartialEq)]
pub enum HashAlgorithm {
Sha1,
Sha256,
}
impl HashAlgorithm {
pub(crate) fn create_digester(&self) -> Box<dyn DynDigest> {
match self {
HashAlgorithm::Sha1 => Box::new(Sha1::new()),
HashAlgorithm::Sha256 => Box::new(Sha256::new()),
}
}
}
pub const NUM_HASH_BYTES: usize = 32;
impl Display for HashAlgorithm {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
HashAlgorithm::Sha1 => write!(f, "sha1"),
HashAlgorithm::Sha256 => write!(f, "sha256"),
}
}
}
impl FromStr for HashAlgorithm {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"sha1" => Ok(HashAlgorithm::Sha1),
"sha256" => Ok(HashAlgorithm::Sha256),
_ => Err(Error::UnknownHashAlgorithm(s.to_owned())),
}
}
}