osslsigncode 0.1.0

In-process Rust bindings for the vendored osslsigncode Authenticode implementation
//! Cryptographic digest and related typed values.
//!
//! ```
//! use osslsigncode::{Digest, LeafHash};
//!
//! assert_eq!(Digest::default(), Digest::Sha256);
//! assert_eq!(Digest::Sha256.to_string(), "sha256");
//!
//! let leaf = LeafHash { digest: Digest::Sha1, hex: "deadbeef" };
//! assert_eq!(leaf.digest, Digest::Sha1);
//! assert_eq!(leaf.hex, "deadbeef");
//! assert_eq!(leaf.to_string(), "sha1:deadbeef");
//! ```

use std::fmt;

/// Digest algorithm used for Authenticode hashing.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum Digest {
    /// MD5.
    Md5,
    /// SHA-1.
    Sha1,
    /// SHA-256.
    #[default]
    Sha256,
    /// SHA-384.
    Sha384,
    /// SHA-512.
    Sha512,
}

impl AsRef<str> for Digest {
    fn as_ref(&self) -> &str {
        match self {
            Self::Md5 => "md5",
            Self::Sha1 => "sha1",
            Self::Sha256 => "sha256",
            Self::Sha384 => "sha384",
            Self::Sha512 => "sha512",
        }
    }
}

impl fmt::Display for Digest {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_ref())
    }
}

/// Microsoft Internet Explorer 4.x CAB permission level.
///
/// Upstream only supports [`JpLevel::Low`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum JpLevel {
    Low,
    Medium,
    High,
}

impl AsRef<str> for JpLevel {
    fn as_ref(&self) -> &str {
        match self {
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
        }
    }
}

impl fmt::Display for JpLevel {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_ref())
    }
}

/// Required leaf-certificate digest for [`crate::Verify`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct LeafHash<'a> {
    /// Digest algorithm.
    pub digest: Digest,
    /// Hex-encoded hash of the leaf certificate.
    pub hex: &'a str,
}

impl fmt::Display for LeafHash<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}:{}", self.digest, self.hex)
    }
}