diff options
author | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2020-04-27 09:14:32 +0200 |
---|---|---|
committer | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2020-04-29 10:29:35 +0200 |
commit | 4ec978c15e2e729b6b90fe589b1d61bfab5ebbb9 (patch) | |
tree | f54b9ae771d674605d534bfa805a278ea31c4c4a /openpgp/src/keyid.rs | |
parent | c5e71b5d4c339e2c6514dad362c5469b141ef4a7 (diff) |
openpgp: Re-add KeyID/Fingerprint::from/to_hex.
- Add KeyID::to_hex.
- Add KeyID::from_hex.
- Add Fingerprint::to_hex.
- Add Fingerprint::from_hex.
- Fixes #456.
Diffstat (limited to 'openpgp/src/keyid.rs')
-rw-r--r-- | openpgp/src/keyid.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs index 9ef9f90b..ddd8b247 100644 --- a/openpgp/src/keyid.rs +++ b/openpgp/src/keyid.rs @@ -177,6 +177,45 @@ impl KeyID { self.as_bytes().iter().all(|b| *b == 0) } + /// Converts this key ID to its canonical hexadecimal representation. + /// + /// This representation is always uppercase and without spaces and is + /// suitable for stable key identifiers. + /// + /// The output of this function is exactly the same as formatting this + /// object with the `:X` format specifier. + /// + /// ```rust + /// # extern crate sequoia_openpgp as openpgp; + /// use openpgp::KeyID; + /// + /// let keyid = "fb3751f1587daef1".parse::<KeyID>().unwrap(); + /// + /// assert_eq!("FB3751F1587DAEF1", keyid.to_hex()); + /// assert_eq!(format!("{:X}", keyid), keyid.to_hex()); + /// ``` + pub fn to_hex(&self) -> String { + format!("{:X}", self) + } + + /// Parses the hexadecimal representation of an OpenPGP key ID. + /// + /// This function is the reverse of `to_hex`. It also accepts other variants + /// of the key ID notation including lower-case letters, spaces and optional + /// leading `0x`. + /// + /// ```rust + /// # extern crate sequoia_openpgp as openpgp; + /// use openpgp::KeyID; + /// + /// let keyid = KeyID::from_hex("0xfb3751f1587daef1").unwrap(); + /// + /// assert_eq!("FB3751F1587DAEF1", keyid.to_hex()); + /// ``` + pub fn from_hex(s: &str) -> std::result::Result<Self, anyhow::Error> { + std::str::FromStr::from_str(s) + } + /// Common code for the above functions. fn convert_to_string(&self, pretty: bool) -> String { let raw = match self { |