From c75a4d60e6a10b3559370498a99ab946f6846043 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 2 Apr 2020 10:35:11 +0200 Subject: openpgp: Drop KeyID::from_hex in favor of FromStr. - See #462. --- net/src/lib.rs | 2 +- net/tests/hkp.rs | 4 +-- openpgp-ffi/src/keyid.rs | 2 +- openpgp/src/cert/mod.rs | 32 ++++++++++---------- openpgp/src/cert/parser/mod.rs | 2 +- openpgp/src/keyhandle.rs | 2 +- openpgp/src/keyid.rs | 50 +++++++++++++++---------------- openpgp/src/message/mod.rs | 3 +- openpgp/src/packet/key.rs | 4 +-- openpgp/src/packet/signature/subpacket.rs | 6 ++-- tool/src/sq.rs | 2 +- 11 files changed, 54 insertions(+), 55 deletions(-) diff --git a/net/src/lib.rs b/net/src/lib.rs index 4e7c4655..17c790aa 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -27,7 +27,7 @@ //! let mut core = Core::new().unwrap(); //! let ctx = Context::new()?; //! let mut ks = KeyServer::keys_openpgp_org(&ctx)?; -//! let keyid = KeyID::from_hex("31855247603831FD").unwrap(); +//! let keyid = "31855247603831FD".parse().unwrap(); //! println!("{:?}", core.run(ks.get(&keyid))); //! Ok(()) //! # } diff --git a/net/tests/hkp.rs b/net/tests/hkp.rs index 0a2121f0..889aeaae 100644 --- a/net/tests/hkp.rs +++ b/net/tests/hkp.rs @@ -26,7 +26,7 @@ extern crate sequoia_net; use crate::openpgp::armor::Reader; use crate::openpgp::Cert; -use crate::openpgp::{Fingerprint, KeyID}; +use crate::openpgp::Fingerprint; use crate::openpgp::parse::Parse; use sequoia_core::{Context, NetworkPolicy}; use sequoia_net::KeyServer; @@ -155,7 +155,7 @@ fn get() { let mut keyserver = KeyServer::new(&ctx, &format!("hkp://{}", addr)).unwrap(); - let keyid = KeyID::from_hex(ID).unwrap(); + let keyid = ID.parse().unwrap(); let key = core.run(keyserver.get(&keyid)).unwrap(); assert_eq!(key.fingerprint(), diff --git a/openpgp-ffi/src/keyid.rs b/openpgp-ffi/src/keyid.rs index fb1fc735..f21c4a84 100644 --- a/openpgp-ffi/src/keyid.rs +++ b/openpgp-ffi/src/keyid.rs @@ -81,7 +81,7 @@ fn pgp_keyid_from_bytes(id: *const u8) -> *mut KeyID { #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C" fn pgp_keyid_from_hex(id: *const c_char) -> Maybe { let id = ffi_param_cstr!(id).to_string_lossy(); - openpgp::KeyID::from_hex(&id).ok().move_into_raw() + id.parse::().ok().move_into_raw() } /// Converts the KeyID to a hexadecimal number. diff --git a/openpgp/src/cert/mod.rs b/openpgp/src/cert/mod.rs index 642f0d6d..a2038806 100644 --- a/openpgp/src/cert/mod.rs +++ b/openpgp/src/cert/mod.rs @@ -1872,9 +1872,9 @@ mod test { .collect::>>(); subkeys.sort(); assert_eq!(subkeys, - &[ KeyID::from_hex("7223B56678E02528").ok(), - KeyID::from_hex("A3506AFB820ABD08").ok(), - KeyID::from_hex("C2B819056C652598").ok(), + &[ "7223B56678E02528".parse().ok(), + "A3506AFB820ABD08".parse().ok(), + "C2B819056C652598".parse().ok(), ]); // DKG's key has all of the self-signatures moved to the last @@ -1903,19 +1903,19 @@ mod test { .collect::>>(); subkeys.sort(); assert_eq!(subkeys, - &[ KeyID::from_hex(&"1075 8EBD BD7C FAB5"[..]).ok(), - KeyID::from_hex(&"1258 68EA 4BFA 08E4"[..]).ok(), - KeyID::from_hex(&"1498 ADC6 C192 3237"[..]).ok(), - KeyID::from_hex(&"24EC FF5A FF68 370A"[..]).ok(), - KeyID::from_hex(&"3714 7292 14D5 DA70"[..]).ok(), - KeyID::from_hex(&"3B7A A7F0 14E6 9B5A"[..]).ok(), - KeyID::from_hex(&"5B58 DCF9 C341 6611"[..]).ok(), - KeyID::from_hex(&"A524 01B1 1BFD FA5C"[..]).ok(), - KeyID::from_hex(&"A70A 96E1 439E A852"[..]).ok(), - KeyID::from_hex(&"C61B D3EC 2148 4CFF"[..]).ok(), - KeyID::from_hex(&"CAEF A883 2167 5333"[..]).ok(), - KeyID::from_hex(&"DC10 4C4E 0CA7 57FB"[..]).ok(), - KeyID::from_hex(&"E3A3 2229 449B 0350"[..]).ok(), + &[ "1075 8EBD BD7C FAB5".parse().ok(), + "1258 68EA 4BFA 08E4".parse().ok(), + "1498 ADC6 C192 3237".parse().ok(), + "24EC FF5A FF68 370A".parse().ok(), + "3714 7292 14D5 DA70".parse().ok(), + "3B7A A7F0 14E6 9B5A".parse().ok(), + "5B58 DCF9 C341 6611".parse().ok(), + "A524 01B1 1BFD FA5C".parse().ok(), + "A70A 96E1 439E A852".parse().ok(), + "C61B D3EC 2148 4CFF".parse().ok(), + "CAEF A883 2167 5333".parse().ok(), + "DC10 4C4E 0CA7 57FB".parse().ok(), + "E3A3 2229 449B 0350".parse().ok(), ]); } diff --git a/openpgp/src/cert/parser/mod.rs b/openpgp/src/cert/parser/mod.rs index a3a83c51..00d3fdd3 100644 --- a/openpgp/src/cert/parser/mod.rs +++ b/openpgp/src/cert/parser/mod.rs @@ -519,7 +519,7 @@ impl<'a, I: Iterator> CertParser<'a, I> { /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ppr = PacketParser::from_bytes(b"")?; - /// # let some_keyid = KeyID::from_hex("C2B819056C652598").unwrap(); + /// # let some_keyid = "C2B819056C652598".parse().unwrap(); /// for certr in CertParser::from_packet_parser(ppr) /// .unvalidated_cert_filter(|cert, _| { /// for component in cert.keys() { diff --git a/openpgp/src/keyhandle.rs b/openpgp/src/keyhandle.rs index f406101d..b8d17e8e 100644 --- a/openpgp/src/keyhandle.rs +++ b/openpgp/src/keyhandle.rs @@ -200,7 +200,7 @@ impl KeyHandle { /// # "0123 4567 8901 2345 6789 0123 AACB 3243 6300 52D9") /// # .unwrap().into(); /// # - /// # let keyid : KeyHandle = KeyID::from_hex("AACB 3243 6300 52D9") + /// # let keyid : KeyHandle = "AACB 3243 6300 52D9".parse::() /// # .unwrap().into(); /// # /// // fpr1 and fpr2 are different fingerprints with the same KeyID. diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs index 2ca86593..fc5b7222 100644 --- a/openpgp/src/keyid.rs +++ b/openpgp/src/keyid.rs @@ -54,7 +54,16 @@ impl std::str::FromStr for KeyID { type Err = anyhow::Error; fn from_str(s: &str) -> std::result::Result { - Self::from_hex(s) + let bytes = crate::fmt::hex::decode_pretty(s)?; + + // A KeyID is exactly 8 bytes long. + if bytes.len() == 8 { + Ok(KeyID::from_bytes(&bytes[..])) + } else { + // Maybe a fingerprint was given. Try to parse it and + // convert it to a KeyID. + Ok(Fingerprint::from_hex(s)?.into()) + } } } @@ -151,17 +160,9 @@ impl KeyID { } /// Reads a hex-encoded Key ID. + #[deprecated] pub fn from_hex(hex: &str) -> Result { - let bytes = crate::fmt::from_hex(hex, true)?; - - // A KeyID is exactly 8 bytes long. - if bytes.len() == 8 { - Ok(KeyID::from_bytes(&bytes[..])) - } else { - // Maybe a fingerprint was given. Try to parse it and - // convert it to a KeyID. - Ok(Fingerprint::from_hex(hex)?.into()) - } + hex.parse() } /// Returns a reference to the raw KeyID. @@ -247,26 +248,25 @@ mod test { #[test] fn from_hex() { - KeyID::from_hex("FB3751F1587DAEF1").unwrap(); - KeyID::from_hex("39D100AB67D5BD8C04010205FB3751F1587DAEF1") + "FB3751F1587DAEF1".parse::().unwrap(); + "39D100AB67D5BD8C04010205FB3751F1587DAEF1".parse::() .unwrap(); - KeyID::from_hex("0xFB3751F1587DAEF1").unwrap(); - KeyID::from_hex("0x39D100AB67D5BD8C04010205FB3751F1587DAEF1") + "0xFB3751F1587DAEF1".parse::().unwrap(); + "0x39D100AB67D5BD8C04010205FB3751F1587DAEF1".parse::() .unwrap(); - KeyID::from_hex("FB37 51F1 587D AEF1").unwrap(); - KeyID::from_hex("39D1 00AB 67D5 BD8C 0401 0205 FB37 51F1 587D AEF1") + "FB37 51F1 587D AEF1".parse::().unwrap(); + "39D1 00AB 67D5 BD8C 0401 0205 FB37 51F1 587D AEF1".parse::() .unwrap(); - KeyID::from_hex("GB3751F1587DAEF1").unwrap_err(); - KeyID::from_hex("EFB3751F1587DAEF1").unwrap_err(); - KeyID::from_hex("%FB3751F1587DAEF1").unwrap_err(); - assert_match!(KeyID::Invalid(_) = KeyID::from_hex("587DAEF1").unwrap()); - assert_match!(KeyID::Invalid(_) = - KeyID::from_hex("0x587DAEF1").unwrap()); + "GB3751F1587DAEF1".parse::().unwrap_err(); + "EFB3751F1587DAEF1".parse::().unwrap_err(); + "%FB3751F1587DAEF1".parse::().unwrap_err(); + assert_match!(KeyID::Invalid(_) = "587DAEF1".parse().unwrap()); + assert_match!(KeyID::Invalid(_) = "0x587DAEF1".parse().unwrap()); } #[test] fn hex_formatting() { - let keyid = KeyID::from_hex("FB3751F1587DAEF1").unwrap(); + let keyid = "FB3751F1587DAEF1".parse::().unwrap(); assert_eq!(format!("{:X}", keyid), "FB3751F1587DAEF1"); assert_eq!(format!("{:x}", keyid), "fb3751f1587daef1"); } @@ -274,6 +274,6 @@ mod test { #[test] fn keyid_is_send_and_sync() { fn f(_: T) {} - f(KeyID::from_hex("89AB CDEF 0123 4567").unwrap()); + f("89AB CDEF 0123 4567".parse::().unwrap()); } } diff --git a/openpgp/src/message/mod.rs b/openpgp/src/message/mod.rs index fa99414f..9488e35b 100644 --- a/openpgp/src/message/mod.rs +++ b/openpgp/src/message/mod.rs @@ -476,7 +476,6 @@ mod tests { use crate::crypto::S2K; use crate::crypto::mpis::{Ciphertext, MPI}; use crate::packet::prelude::*; - use crate::KeyID; #[test] fn tokens() { @@ -1124,7 +1123,7 @@ mod tests { packets.insert( 1, PKESK3::new( - KeyID::from_hex("0000111122223333").unwrap(), + "0000111122223333".parse().unwrap(), PublicKeyAlgorithm::RSAEncrypt, Ciphertext::RSA { c: MPI::new(&[]) }).unwrap().into()); diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index 56545859..7f40f072 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -2006,7 +2006,7 @@ mod tests { #[test] fn import_ed25519() { - use crate::{Fingerprint, KeyID}; + use crate::Fingerprint; use crate::types::SignatureType; use crate::packet::signature::Signature4; use crate::packet::signature::subpacket::{ @@ -2022,7 +2022,7 @@ mod tests { let mut hashed = SubpacketArea::default(); let mut unhashed = SubpacketArea::default(); let fpr = Fingerprint::from_hex("D81A 5DC0 DEBF EE5F 9AC8 20EB 6769 5DB9 920D 4FAC").unwrap(); - let kid = KeyID::from_hex("6769 5DB9 920D 4FAC").unwrap(); + let kid = "6769 5DB9 920D 4FAC".parse().unwrap(); let ctime = 1549460479.into(); let r = b"\x5A\xF9\xC7\x42\x70\x24\x73\xFF\x7F\x27\xF9\x20\x9D\x20\x0F\xE3\x8F\x71\x3C\x5F\x97\xFD\x60\x80\x39\x29\xC2\x14\xFD\xC2\x4D\x70"; let s = b"\x6E\x68\x74\x11\x72\xF4\x9C\xE1\x99\x99\x1F\x67\xFC\x3A\x68\x33\xF9\x3F\x3A\xB9\x1A\xA5\x72\x4E\x78\xD4\x81\xCB\x7B\xA5\xE5\x0A"; diff --git a/openpgp/src/packet/signature/subpacket.rs b/openpgp/src/packet/signature/subpacket.rs index ec5badcc..b028655f 100644 --- a/openpgp/src/packet/signature/subpacket.rs +++ b/openpgp/src/packet/signature/subpacket.rs @@ -2898,7 +2898,7 @@ fn subpacket_test_2() { Features::default().set_mdc(true)) })); - let keyid = KeyID::from_hex("F004 B9A4 5C58 6126").unwrap(); + let keyid = "F004 B9A4 5C58 6126".parse().unwrap(); assert_eq!(sig.issuer(), Some(&keyid)); assert_eq!(sig.subpacket(SubpacketTag::Issuer), Some(&Subpacket { @@ -3020,7 +3020,7 @@ fn subpacket_test_2() { })); - let keyid = KeyID::from_hex("CEAD 0621 0934 7957").unwrap(); + let keyid = "CEAD 0621 0934 7957".parse().unwrap(); assert_eq!(sig.issuer(), Some(&keyid)); assert_eq!(sig.subpacket(SubpacketTag::Issuer), Some(&Subpacket { @@ -3224,7 +3224,7 @@ fn subpacket_test_2() { 63072000.into()) })); - let keyid = KeyID::from_hex("CEAD 0621 0934 7957").unwrap(); + let keyid = "CEAD 0621 0934 7957".parse().unwrap(); assert_eq!(sig.issuer(), Some(&keyid)); assert_eq!(sig.subpacket(SubpacketTag::Issuer), Some(&Subpacket { diff --git a/tool/src/sq.rs b/tool/src/sq.rs index 27e7c49f..3e371927 100644 --- a/tool/src/sq.rs +++ b/tool/src/sq.rs @@ -484,7 +484,7 @@ fn main() -> Result<()> { match m.subcommand() { ("get", Some(m)) => { let keyid = m.value_of("keyid").unwrap(); - let id = openpgp::KeyID::from_hex(keyid); + let id = keyid.parse(); if id.is_err() { eprintln!("Malformed key ID: {:?}\n\ (Note: only long Key IDs are supported.)", -- cgit v1.2.3