summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2020-12-22 13:08:16 +0100
committerNeal H. Walfield <neal@pep.foundation>2020-12-22 13:08:16 +0100
commitfae15076622472d6adc888b4a8f03fff30c4b079 (patch)
treed5a68edc4178b77a853836bb0ed0b6b537d08b51
parent70dd8e7f1dd643db7415c406ea264572528b8ee7 (diff)
openpgp: Add KeyHandle::is_invalid.
- Add a convenience function to determine if a KeyHandle contains an invalid identifier.
-rw-r--r--openpgp/src/keyhandle.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/openpgp/src/keyhandle.rs b/openpgp/src/keyhandle.rs
index 2642caee..ea923dcd 100644
--- a/openpgp/src/keyhandle.rs
+++ b/openpgp/src/keyhandle.rs
@@ -299,6 +299,47 @@ impl KeyHandle {
== Ordering::Equal
}
+ /// Returns whether the KeyHandle is invalid.
+ ///
+ /// A KeyHandle is invalid if the `Fingerprint` or `KeyID` that it
+ /// contains is valid.
+ ///
+ /// ```
+ /// use sequoia_openpgp as openpgp;
+ /// use openpgp::Fingerprint;
+ /// use openpgp::KeyID;
+ /// use openpgp::KeyHandle;
+ ///
+ /// # fn main() -> sequoia_openpgp::Result<()> {
+ /// // A perfectly valid fingerprint:
+ /// let kh : KeyHandle = "8F17 7771 18A3 3DDA 9BA4 8E62 AACB 3243 6300 52D9"
+ /// .parse()?;
+ /// assert!(! kh.is_invalid());
+ ///
+ /// // But, V3 fingerprints are invalid.
+ /// let kh : KeyHandle = "9E 94 45 13 39 83 5F 70 7B E7 D8 ED C4 BE 5A A6"
+ /// .parse()?;
+ /// assert!(kh.is_invalid());
+ ///
+ /// // A perfectly valid Key ID:
+ /// let kh : KeyHandle = "AACB 3243 6300 52D9"
+ /// .parse()?;
+ /// assert!(! kh.is_invalid());
+ ///
+ /// // But, short Key IDs are invalid:
+ /// let kh : KeyHandle = "6300 52D9"
+ /// .parse()?;
+ /// assert!(kh.is_invalid());
+ /// # Ok(()) }
+ /// ```
+ pub fn is_invalid(&self) -> bool {
+ match self {
+ KeyHandle::Fingerprint(Fingerprint::Invalid(_)) => true,
+ KeyHandle::KeyID(KeyID::Invalid(_)) => true,
+ _ => false,
+ }
+ }
+
/// Converts this fingerprint to its canonical hexadecimal
/// representation.
///