#[allow(clippy::module_name_repetitions)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, strum::EnumString, strum::Display)]
pub enum SigningAlgorithm {
#[cfg_attr(docsrs, doc(cfg(feature = "historic")))]
#[cfg(feature = "historic")]
#[strum(serialize = "rsa-sha1")]
RsaSha1,
#[strum(serialize = "rsa-sha256")]
RsaSha256,
#[strum(serialize = "ed25519-sha256")]
Ed25519Sha256,
}
impl SigningAlgorithm {
pub(super) fn support_any(self, hash_algo: &[HashAlgorithm]) -> bool {
let supported = self.get_supported_hash_algo();
hash_algo.iter().any(|a| supported.contains(a))
}
pub(super) const fn get_supported_hash_algo(self) -> &'static [HashAlgorithm] {
match self {
#[cfg(feature = "historic")]
Self::RsaSha1 => &[HashAlgorithm::Sha1],
#[cfg(feature = "historic")]
Self::RsaSha256 => &[HashAlgorithm::Sha256, HashAlgorithm::Sha1],
_ => &[HashAlgorithm::Sha256],
}
}
pub(super) fn get_preferred_hash_algo(self) -> &'static HashAlgorithm {
self.get_supported_hash_algo()
.first()
.expect("has at least one algorithm")
}
}
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, strum::EnumString, strum::Display)]
#[strum(serialize_all = "lowercase")]
pub enum HashAlgorithm {
#[cfg_attr(docsrs, doc(cfg(feature = "historic")))]
#[cfg(feature = "historic")]
Sha1,
Sha256,
}
impl HashAlgorithm {
#[must_use]
pub fn hash<T: AsRef<[u8]>>(self, data: T) -> Vec<u8> {
match self {
#[cfg(feature = "historic")]
Self::Sha1 => {
let mut digest = <sha1::Sha1 as sha1::Digest>::new();
sha1::Digest::update(&mut digest, data);
sha1::Digest::finalize(digest).to_vec()
}
Self::Sha256 => {
let mut digest = <sha2::Sha256 as sha2::Digest>::new();
sha2::Digest::update(&mut digest, data);
sha2::Digest::finalize(digest).to_vec()
}
}
}
}