use std::fmt;
use std::str::FromStr;
pub enum HashFunctionError {
ImportError,
}
#[repr(C)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum HashFunction {
Sha1 = 1,
Sha224 = 2,
Sha256 = 3,
Sha384 = 4,
Sha512 = 5,
Sha512Trunc224 = 6,
Sha512Trunc256 = 7,
Sha3_224 = 8,
Sha3_256 = 9,
Sha3_384 = 10,
Sha3_512 = 11,
Keccak224 = 12,
Keccak256 = 13,
Keccak384 = 14,
Keccak512 = 15,
}
impl fmt::Display for HashFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
HashFunction::Sha1 => "SHA1",
HashFunction::Sha224 => "SHA224",
HashFunction::Sha256 => "SHA256",
HashFunction::Sha384 => "SHA384",
HashFunction::Sha512 => "SHA512",
HashFunction::Sha512Trunc224 => "SHA512-224",
HashFunction::Sha512Trunc256 => "SHA512-256",
HashFunction::Sha3_224 => "SHA3-224",
HashFunction::Sha3_256 => "SHA3-256",
HashFunction::Sha3_384 => "SHA3-384",
HashFunction::Sha3_512 => "SHA3-512",
HashFunction::Keccak224 => "Keccak224",
HashFunction::Keccak256 => "Keccak256",
HashFunction::Keccak384 => "Keccak384",
HashFunction::Keccak512 => "Keccak512",
};
write!(f, "{}", s)
}
}
impl FromStr for HashFunction {
type Err = HashFunctionError;
fn from_str(data: &str) -> Result<Self, Self::Err> {
Ok(match data.to_lowercase().as_str() {
"sha1" => HashFunction::Sha1,
"sha224" => HashFunction::Sha224,
"sha256" => HashFunction::Sha256,
"sha384" => HashFunction::Sha384,
"sha512" => HashFunction::Sha512,
"sha512-224" | "sha512t224" => HashFunction::Sha512Trunc224,
"sha512-256" | "sha512t256" => HashFunction::Sha512Trunc256,
"sha3-224" => HashFunction::Sha3_224,
"sha3-256" => HashFunction::Sha3_256,
"sha3-384" => HashFunction::Sha3_384,
"sha3-512" => HashFunction::Sha3_512,
"keccak224" => HashFunction::Keccak224,
"keccak256" => HashFunction::Keccak256,
"keccak384" => HashFunction::Keccak384,
"keccak512" => HashFunction::Keccak512,
_ => {
return Err(HashFunctionError::ImportError);
}
})
}
}