use crate::error::UnknownDigest;
use std::{fmt, str::FromStr};
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum SigningAlgo {
#[allow(unused)]
Rsa,
EcP256,
EcP384,
}
impl fmt::Display for SigningAlgo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
SigningAlgo::Rsa => "RSA PKCS#1 v1.5",
SigningAlgo::EcP256 => "ECDSA with NIST P-256 curve",
SigningAlgo::EcP384 => "ECDSA with NIST P-384 curve",
})
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum DigestAlgo {
Sha1,
Sha256,
Sha384,
}
impl fmt::Display for DigestAlgo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
DigestAlgo::Sha1 => "SHA-1",
DigestAlgo::Sha256 => "SHA-256",
DigestAlgo::Sha384 => "SHA-384",
})
}
}
impl FromStr for DigestAlgo {
type Err = UnknownDigest;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_ascii_lowercase();
match s.as_str() {
"sha1" | "sha-1" => Ok(Self::Sha1),
"sha256" | "sha-256" => Ok(Self::Sha256),
"sha384" | "sha-384" => Ok(Self::Sha384),
_ => Err(UnknownDigest(s.into_boxed_str())),
}
}
}