diff options
author | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2022-08-10 13:23:45 +0200 |
---|---|---|
committer | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2022-08-16 11:04:43 +0200 |
commit | c4e6642f01ff45bfa4eea027b31c5418bfebb8b3 (patch) | |
tree | dda7a81febbfed9bed64f374c08b355656a6edca /openpgp/src/crypto/backend | |
parent | 3a3f59cc84acb2dd10c623c046201046ed6ea163 (diff) |
openpgp: Expose `HashAlgorithm::oid()` on all crypto backends.
- Expose `oid()` function for all cryptographic backends.
- Fix the description to accurately describe the bytes that are being
returned.
- Add the reference and note to the common use of this function.
- Add practical example of computing the entire `DigestInfo`
structure.
- Add mention of the change to the NEWS file.
- Add test case to check if the values match what Nettle is using.
- Fixes #919.
Diffstat (limited to 'openpgp/src/crypto/backend')
-rw-r--r-- | openpgp/src/crypto/backend/nettle/hash.rs | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/openpgp/src/crypto/backend/nettle/hash.rs b/openpgp/src/crypto/backend/nettle/hash.rs index 925ff855..4bf1535e 100644 --- a/openpgp/src/crypto/backend/nettle/hash.rs +++ b/openpgp/src/crypto/backend/nettle/hash.rs @@ -78,21 +78,21 @@ impl HashAlgorithm { Err(Error::UnsupportedHashAlgorithm(self).into()), } } +} - /// Returns the ASN.1 OID of this hash algorithm. - pub fn oid(self) -> Result<&'static [u8]> { - use nettle::rsa; +#[cfg(all(test, feature = "crypto-nettle"))] +mod tests { + use super::*; + use nettle::rsa; - match self { - HashAlgorithm::SHA1 => Ok(rsa::ASN1_OID_SHA1), - HashAlgorithm::SHA224 => Ok(rsa::ASN1_OID_SHA224), - HashAlgorithm::SHA256 => Ok(rsa::ASN1_OID_SHA256), - HashAlgorithm::SHA384 => Ok(rsa::ASN1_OID_SHA384), - HashAlgorithm::SHA512 => Ok(rsa::ASN1_OID_SHA512), - HashAlgorithm::MD5 => Ok(rsa::ASN1_OID_MD5), - HashAlgorithm::RipeMD => Ok(rsa::ASN1_OID_RIPEMD160), - HashAlgorithm::Private(_) | HashAlgorithm::Unknown(_) => - Err(Error::UnsupportedHashAlgorithm(self).into()), - } + #[test] + fn oids_match_nettle() { + assert_eq!(HashAlgorithm::SHA1.oid().unwrap(), rsa::ASN1_OID_SHA1); + assert_eq!(HashAlgorithm::SHA224.oid().unwrap(), rsa::ASN1_OID_SHA224); + assert_eq!(HashAlgorithm::SHA256.oid().unwrap(), rsa::ASN1_OID_SHA256); + assert_eq!(HashAlgorithm::SHA384.oid().unwrap(), rsa::ASN1_OID_SHA384); + assert_eq!(HashAlgorithm::SHA512.oid().unwrap(), rsa::ASN1_OID_SHA512); + assert_eq!(HashAlgorithm::MD5.oid().unwrap(), rsa::ASN1_OID_MD5); + assert_eq!(HashAlgorithm::RipeMD.oid().unwrap(), rsa::ASN1_OID_RIPEMD160); } } |