image4_pki/
algo.rs

1use crate::error::UnknownDigest;
2use std::{fmt, str::FromStr};
3
4/// An enum for all digital signature algorithms supported by the crate.
5#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
6#[non_exhaustive]
7pub enum SigningAlgo {
8    /// PKCS#1 v1.5 RSA algorithm.
9    #[allow(unused)]
10    Rsa,
11    /// ECDSA with NIST P-256 curve.
12    EcP256,
13    /// ECDSA with NIST P-384 curve.
14    EcP384,
15}
16
17impl fmt::Display for SigningAlgo {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        f.write_str(match self {
20            SigningAlgo::Rsa => "RSA PKCS#1 v1.5",
21            SigningAlgo::EcP256 => "ECDSA with NIST P-256 curve",
22            SigningAlgo::EcP384 => "ECDSA with NIST P-384 curve",
23        })
24    }
25}
26
27/// An enum for all digest algorithms supported by the crate.
28#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
29#[non_exhaustive]
30pub enum DigestAlgo {
31    /// The SHA-1 digest algorithm.
32    Sha1,
33    /// The SHA-256 digest algorithm.
34    Sha256,
35    /// The SHA-384 digest algorithm.
36    Sha384,
37}
38
39impl fmt::Display for DigestAlgo {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        f.write_str(match self {
42            DigestAlgo::Sha1 => "SHA-1",
43            DigestAlgo::Sha256 => "SHA-256",
44            DigestAlgo::Sha384 => "SHA-384",
45        })
46    }
47}
48
49impl FromStr for DigestAlgo {
50    type Err = UnknownDigest;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        let s = s.to_ascii_lowercase();
54
55        match s.as_str() {
56            "sha1" | "sha-1" => Ok(Self::Sha1),
57            "sha256" | "sha-256" => Ok(Self::Sha256),
58            "sha384" | "sha-384" => Ok(Self::Sha384),
59            _ => Err(UnknownDigest(s.into_boxed_str())),
60        }
61    }
62}