image4-pki 0.1.0

An experimantal crate containing implementation of assymetric crypto primitives usable with the image4 crate.
Documentation
use crate::error::UnknownDigest;
use std::{fmt, str::FromStr};

/// An enum for all digital signature algorithms supported by the crate.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum SigningAlgo {
    /// PKCS#1 v1.5 RSA algorithm.
    #[allow(unused)]
    Rsa,
    /// ECDSA with NIST P-256 curve.
    EcP256,
    /// ECDSA with NIST P-384 curve.
    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",
        })
    }
}

/// An enum for all digest algorithms supported by the crate.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum DigestAlgo {
    /// The SHA-1 digest algorithm.
    Sha1,
    /// The SHA-256 digest algorithm.
    Sha256,
    /// The SHA-384 digest algorithm.
    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())),
        }
    }
}