diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2021-02-10 14:08:00 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2021-02-10 15:48:47 +0100 |
commit | 233d5dc3567bc094d91ec18001bc4da43d55d021 (patch) | |
tree | 31063368ad70abd3d09b2f2ed7b252f63bfa7352 /openpgp/src/types | |
parent | fa2e4cf472137748975439ad7414dbc444f83b6e (diff) |
openpgp: Add HashAlgorithm::text_name.
Diffstat (limited to 'openpgp/src/types')
-rw-r--r-- | openpgp/src/types/mod.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/openpgp/src/types/mod.rs b/openpgp/src/types/mod.rs index 91749dd2..4908f681 100644 --- a/openpgp/src/types/mod.rs +++ b/openpgp/src/types/mod.rs @@ -1055,6 +1055,42 @@ impl fmt::Display for HashAlgorithm { } } +impl HashAlgorithm { + /// Returns the text name of this algorithm. + /// + /// [Section 9.4 of RFC 4880] defines a textual representation of + /// hash algorithms. This is used in cleartext signed messages + /// (see [Section 7 of RFC 4880]). + /// + /// [Section 9.4 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-9.4 + /// [Section 7 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-7 + /// + /// # Examples + /// + /// ```rust + /// # use sequoia_openpgp as openpgp; + /// # use openpgp::types::HashAlgorithm; + /// # fn main() -> openpgp::Result<()> { + /// assert_eq!(HashAlgorithm::RipeMD.text_name()?, "RIPEMD160"); + /// # Ok(()) } + /// ``` + pub fn text_name(&self) -> Result<&str> { + match self { + HashAlgorithm::MD5 => Ok("MD5"), + HashAlgorithm::SHA1 => Ok("SHA1"), + HashAlgorithm::RipeMD => Ok("RIPEMD160"), + HashAlgorithm::SHA256 => Ok("SHA256"), + HashAlgorithm::SHA384 => Ok("SHA384"), + HashAlgorithm::SHA512 => Ok("SHA512"), + HashAlgorithm::SHA224 => Ok("SHA224"), + HashAlgorithm::Private(_) => + Err(Error::UnsupportedHashAlgorithm(*self).into()), + HashAlgorithm::Unknown(_) => + Err(Error::UnsupportedHashAlgorithm(*self).into()), + } + } +} + #[cfg(test)] impl Arbitrary for HashAlgorithm { fn arbitrary<G: Gen>(g: &mut G) -> Self { @@ -1828,6 +1864,18 @@ mod tests { } quickcheck! { + fn hash_roundtrip_text_name(hash: HashAlgorithm) -> bool { + match hash { + HashAlgorithm::Private(_) | HashAlgorithm::Unknown(_) => true, + hash => { + let s = hash.text_name().unwrap(); + hash == HashAlgorithm::from_str(s).unwrap() + } + } + } + } + + quickcheck! { fn hash_display(hash: HashAlgorithm) -> bool { let s = format!("{}", hash); !s.is_empty() |