From bf4c310cb400294514ca42994a8c72cf4111e7d9 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Mon, 24 Dec 2018 15:02:05 +0100 Subject: openpgp: Add a function to compare the public bits of two Keys - This is made available as Key::public_cmp and not as an implementation of Ord::cmp, because Ord::cmp, should it be implemented, should also compare the private bits. --- openpgp/src/packet/key.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'openpgp') diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index c5a9e663..f31d58aa 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -1,6 +1,8 @@ //! Public key, public subkey, private key and private subkey packets. use std::fmt; +use std::mem; +use std::cmp::Ordering; use time; use Error; @@ -56,6 +58,33 @@ impl fmt::Display for Key { } } +impl Key { + /// Compares the public bits of two keys. + /// + /// This returns Ordering::Equal if the public MPIs, version, + /// creation time and algorithm of the two `Key`s match. This + /// does not consider the packet's encoding, packet's tag or the + /// secret key material. + pub fn public_cmp(a: &Self, b: &Self) -> Ordering { + match a.mpis.cmp(&b.mpis) { + Ordering::Equal => (), + o => return o, + } + + match a.version.cmp(&b.version) { + Ordering::Equal => (), + o => return o, + } + + match a.creation_time.cmp(&b.creation_time) { + Ordering::Equal => (), + o => return o, + } + + a.pk_algo.cmp(&b.pk_algo) + } +} + impl Key { pub(crate) fn new_(creation_time: time::Tm,pk_algo: PublicKeyAlgorithm, mpis: mpis::PublicKey, secret: Option) -- cgit v1.2.3