summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWiktor Kwapisiewicz <wiktor@metacode.biz>2020-04-27 09:14:32 +0200
committerWiktor Kwapisiewicz <wiktor@metacode.biz>2020-04-29 10:29:35 +0200
commit4ec978c15e2e729b6b90fe589b1d61bfab5ebbb9 (patch)
treef54b9ae771d674605d534bfa805a278ea31c4c4a
parentc5e71b5d4c339e2c6514dad362c5469b141ef4a7 (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.
-rw-r--r--openpgp/src/fingerprint.rs43
-rw-r--r--openpgp/src/keyid.rs39
2 files changed, 82 insertions, 0 deletions
diff --git a/openpgp/src/fingerprint.rs b/openpgp/src/fingerprint.rs
index ebc13366..6094229f 100644
--- a/openpgp/src/fingerprint.rs
+++ b/openpgp/src/fingerprint.rs
@@ -74,6 +74,49 @@ impl Fingerprint {
}
}
+ /// Converts this fingerprint 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::Fingerprint;
+ ///
+ /// let fpr = "0123 4567 89AB CDEF 0123 4567 89AB CDEF 0123 4567".parse::<Fingerprint>().unwrap();
+ ///
+ /// assert_eq!("0123456789ABCDEF0123456789ABCDEF01234567", fpr.to_hex());
+ /// assert_eq!(format!("{:X}", fpr), fpr.to_hex());
+ /// ```
+ pub fn to_hex(&self) -> String {
+ format!("{:X}", self)
+ }
+
+ /// Parses the hexadecimal representation of an OpenPGP fingerprint.
+ ///
+ /// This function is the reverse of `to_hex`. It also accepts other variants
+ /// of the fingerprint notation including lower-case letters, spaces and
+ /// optional leading `0x`.
+ ///
+ /// ```rust
+ /// # extern crate sequoia_openpgp as openpgp;
+ /// use openpgp::Fingerprint;
+ ///
+ /// let fpr = Fingerprint::from_hex("0123456789ABCDEF0123456789ABCDEF01234567").unwrap();
+ ///
+ /// assert_eq!("0123456789ABCDEF0123456789ABCDEF01234567", fpr.to_hex());
+ ///
+ /// let fpr = Fingerprint::from_hex("0123 4567 89ab cdef 0123 4567 89ab cdef 0123 4567").unwrap();
+ ///
+ /// assert_eq!("0123456789ABCDEF0123456789ABCDEF01234567", fpr.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 {
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 {