summaryrefslogtreecommitdiffstats
path: root/openpgp/src/keyhandle.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2021-03-02 11:25:58 +0100
committerJustus Winter <justus@sequoia-pgp.org>2021-03-02 11:46:20 +0100
commitcd94fae7726d565b89e4d7a14246046f9c23f29a (patch)
tree2bf5379af02363ce3ed7f111fa61dc1c50c2118d /openpgp/src/keyhandle.rs
parentf82d3e9a4d2e324405a1c115b3666d977567d377 (diff)
openpgp: Add methods for hexadecimal representation with spaces.
- These are explicitly intended for manual comparison of key ids and fingerprints. - See #422.
Diffstat (limited to 'openpgp/src/keyhandle.rs')
-rw-r--r--openpgp/src/keyhandle.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/openpgp/src/keyhandle.rs b/openpgp/src/keyhandle.rs
index 5ae9741a..082c0299 100644
--- a/openpgp/src/keyhandle.rs
+++ b/openpgp/src/keyhandle.rs
@@ -365,6 +365,37 @@ impl KeyHandle {
format!("{:X}", self)
}
+ /// Converts this `KeyHandle` to its hexadecimal representation
+ /// with spaces.
+ ///
+ /// This representation is always uppercase and with spaces
+ /// grouping the hexadecimal digits into groups of four. It is
+ /// only suitable for manual comparison of key handles.
+ ///
+ /// Note: The spaces will hinder other kind of use cases. For
+ /// example, it is harder to select the whole key handle for
+ /// copying, and it has to be quoted when used as a command line
+ /// argument. Only use this form for displaying a key handle with
+ /// the intent of manual comparisons.
+ ///
+ /// ```rust
+ /// # fn main() -> sequoia_openpgp::Result<()> {
+ /// # use sequoia_openpgp as openpgp;
+ /// use openpgp::KeyHandle;
+ ///
+ /// let h: KeyHandle =
+ /// "0123 4567 89AB CDEF 0123 4567 89AB CDEF 0123 4567".parse()?;
+ ///
+ /// assert_eq!("0123 4567 89AB CDEF 0123 4567 89AB CDEF 0123 4567",
+ /// h.to_spaced_hex());
+ /// # Ok(()) }
+ /// ```
+ pub fn to_spaced_hex(&self) -> String {
+ match self {
+ KeyHandle::Fingerprint(v) => v.to_spaced_hex(),
+ KeyHandle::KeyID(v) => v.to_spaced_hex(),
+ }
+ }
}
#[cfg(test)]