diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-11-26 13:04:47 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-11-26 14:29:29 +0100 |
commit | b24d1c5a099e2c741cf724816d49b8dd7977e049 (patch) | |
tree | 0288feb61d74b61acc39045a5e2b31551eb7b131 /openpgp | |
parent | 33fd6f48682b15e140d479574ec3377610acb22d (diff) |
openpgp: Implement From<Fingerprint> for KeyID.
- Remove Fingerprint::to_keyid, use From instead.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/fingerprint.rs | 12 | ||||
-rw-r--r-- | openpgp/src/keyid.rs | 26 | ||||
-rw-r--r-- | openpgp/src/packet/key/mod.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet/signature/mod.rs | 4 | ||||
-rw-r--r-- | openpgp/src/packet/signature/subpacket.rs | 4 | ||||
-rw-r--r-- | openpgp/src/tpk/parser/mod.rs | 2 |
6 files changed, 31 insertions, 19 deletions
diff --git a/openpgp/src/fingerprint.rs b/openpgp/src/fingerprint.rs index 309313be..ab662ed1 100644 --- a/openpgp/src/fingerprint.rs +++ b/openpgp/src/fingerprint.rs @@ -1,7 +1,6 @@ use std::fmt; use crate::Fingerprint; -use crate::KeyID; use crate::Result; impl fmt::Display for Fingerprint { @@ -129,17 +128,6 @@ impl Fingerprint { String::from_utf8(output).unwrap() } - /// Converts the fingerprint to a key ID. - pub fn to_keyid(&self) -> KeyID { - match self { - &Fingerprint::V4(ref fp) => - KeyID::from_bytes(&fp[fp.len() - 8..]), - &Fingerprint::Invalid(ref fp) => { - KeyID::Invalid(fp.clone()) - } - } - } - /// Converts the hex representation of the fingerprint to a phrase in the /// ICAO alphabet. pub fn to_icao(&self) -> String { diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs index 275599f9..5cda407b 100644 --- a/openpgp/src/keyid.rs +++ b/openpgp/src/keyid.rs @@ -45,6 +45,30 @@ impl From<u64> for KeyID { } } +impl From<&Fingerprint> for KeyID { + fn from(fp: &Fingerprint) -> Self { + match fp { + Fingerprint::V4(fp) => + KeyID::from_bytes(&fp[fp.len() - 8..]), + Fingerprint::Invalid(fp) => { + KeyID::Invalid(fp.clone()) + } + } + } +} + +impl From<Fingerprint> for KeyID { + fn from(fp: Fingerprint) -> Self { + match fp { + Fingerprint::V4(fp) => + KeyID::from_bytes(&fp[fp.len() - 8..]), + Fingerprint::Invalid(fp) => { + KeyID::Invalid(fp) + } + } + } +} + impl KeyID { /// Converts a u64 to a KeyID. pub fn new(data: u64) -> KeyID { @@ -100,7 +124,7 @@ impl KeyID { } else { // Maybe a fingerprint was given. Try to parse it and // convert it to a KeyID. - Ok(Fingerprint::from_hex(hex)?.to_keyid()) + Ok(Fingerprint::from_hex(hex)?.into()) } } diff --git a/openpgp/src/packet/key/mod.rs b/openpgp/src/packet/key/mod.rs index a1b3c561..ea1178f1 100644 --- a/openpgp/src/packet/key/mod.rs +++ b/openpgp/src/packet/key/mod.rs @@ -1070,7 +1070,7 @@ impl<P, R> Key4<P, R> /// Computes and returns the key's key ID as per Section 12.2 of /// RFC 4880. pub fn keyid(&self) -> KeyID { - self.fingerprint().to_keyid() + self.fingerprint().into() } } diff --git a/openpgp/src/packet/signature/mod.rs b/openpgp/src/packet/signature/mod.rs index 460dbe21..4fb15d18 100644 --- a/openpgp/src/packet/signature/mod.rs +++ b/openpgp/src/packet/signature/mod.rs @@ -507,7 +507,7 @@ impl Signature4 { if let Some(issuer) = self.issuer_fingerprint() { // Prefer the IssuerFingerprint. area.add(Subpacket::new( - SubpacketValue::Issuer(issuer.to_keyid()), false).unwrap()) + SubpacketValue::Issuer(issuer.into()), false).unwrap()) .unwrap(); } else if let Some(issuer) = self.issuer() { // Fall back to the Issuer, which we will also get @@ -1394,7 +1394,7 @@ mod test { hash.update(&msg[..]); let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); - let keyid = fp.to_keyid(); + let keyid = KeyID::from(&fp); // First, make sure any superfluous subpackets are removed, // yet the Issuer and EmbeddedSignature ones are kept. diff --git a/openpgp/src/packet/signature/subpacket.rs b/openpgp/src/packet/signature/subpacket.rs index f0950a1d..557c8eef 100644 --- a/openpgp/src/packet/signature/subpacket.rs +++ b/openpgp/src/packet/signature/subpacket.rs @@ -2847,10 +2847,10 @@ fn accessors() { assert_eq!(sig_.revocation_key(), Some((2, pk_algo.into(), fp.clone()))); - sig = sig.set_issuer(fp.to_keyid()).unwrap(); + sig = sig.set_issuer(fp.clone().into()).unwrap(); let sig_ = sig.clone().sign_hash(&mut keypair, hash_algo, hash.clone()).unwrap(); - assert_eq!(sig_.issuer(), Some(fp.to_keyid())); + assert_eq!(sig_.issuer(), Some(fp.clone().into())); let pref = vec![HashAlgorithm::SHA512, HashAlgorithm::SHA384, diff --git a/openpgp/src/tpk/parser/mod.rs b/openpgp/src/tpk/parser/mod.rs index d3d6260c..e014ef30 100644 --- a/openpgp/src/tpk/parser/mod.rs +++ b/openpgp/src/tpk/parser/mod.rs @@ -673,7 +673,7 @@ impl<'a, I: Iterator<Item=Packet>> TPKParser<'a, I> { } let primary_fp = tpk.primary().fingerprint(); - let primary_keyid = primary_fp.to_keyid(); + let primary_keyid = KeyID::from(&primary_fp); // The parser puts all of the signatures on the // certifications field. Split them now. |