Enum sequoia_openpgp::KeyHandle

source ·
pub enum KeyHandle {
    Fingerprint(Fingerprint),
    KeyID(KeyID),
}
Expand description

Enum representing an identifier for certificates and keys.

A KeyHandle contains either a Fingerprint or a KeyID. This is needed because signatures can reference their issuer either by Fingerprint or by KeyID.

Currently, Sequoia supports version 4 fingerprints and Key ID only. Version 3 fingerprints and Key ID were deprecated by RFC 4880 in 2007.

A v4 fingerprint is, essentially, a 20-byte SHA-1 hash over the key’s public key packet. A v4 Key ID is defined as the fingerprint’s lower 8 bytes.

For the exact definition, see Section 12.2 of RFC 4880.

Both fingerprint and Key ID are used to identify a key, e.g., the issuer of a signature.

§A Note on Equality

Like other data types, two KeyHandles are considered equal if their serialized forms are the same. That is, if you compare a key handle that contains a Fingerprint, and a key handle that contains a KeyID, they will not be considered equal even if the key ID aliases the fingerprint. If you want to check for aliasing, you should use KeyHandle::aliases.

§Examples

use openpgp::KeyHandle;
use openpgp::KeyID;
use openpgp::Packet;
use openpgp::parse::Parse;

let p = Packet::from_bytes(
    "-----BEGIN PGP SIGNATURE-----
     // ...
     -----END PGP SIGNATURE-----")?;
if let Packet::Signature(sig) = p {
    let issuers = sig.get_issuers();
    assert_eq!(issuers.len(), 2);
    let kh: KeyHandle
        = "C03F A641 1B03 AE12 5764  6118 7223 B566 78E0 2528".parse()?;
    assert!(&issuers[0].aliases(&kh));
    assert!(&issuers[1].aliases(&kh));
} else {
    unreachable!("It's a signature!");
}

Variants§

§

Fingerprint(Fingerprint)

A Fingerprint.

§

KeyID(KeyID)

A KeyID.

Implementations§

source§

impl KeyHandle

source

pub fn as_bytes(&self) -> &[u8]

Returns the raw identifier as a byte slice.

source

pub fn aliases<H>(&self, other: H) -> bool
where H: Borrow<KeyHandle>,

Returns whether self and other could be aliases of each other.

KeyHandle’s PartialEq implementation cannot assert that a Fingerprint and a KeyID are equal, because distinct fingerprints may have the same KeyID, and PartialEq must be transitive, i.e.,

a == b and b == c implies a == c.

That is, if fpr1 and fpr2 are distinct fingerprints with the same key ID then:

fpr1 == keyid and fpr2 == keyid, but fpr1 != fpr2.

This definition of equality makes searching for a given KeyHandle using PartialEq awkward. This function fills that gap. It answers the question: given two KeyHandles, could they be aliases? That is, it implements the desired, non-transitive equality relation:

// fpr1 and fpr2 are different fingerprints with the same KeyID.
assert_ne!(fpr1, fpr2);
assert_eq!(KeyID::from(&fpr1), KeyID::from(&fpr2));

assert!(fpr1.aliases(&keyid));
assert!(fpr2.aliases(&keyid));
assert!(! fpr1.aliases(&fpr2));
source

pub fn is_invalid(&self) -> bool

Returns whether the KeyHandle is invalid.

A KeyHandle is invalid if the Fingerprint or KeyID that it contains is invalid.

use sequoia_openpgp as openpgp;
use openpgp::Fingerprint;
use openpgp::KeyID;
use openpgp::KeyHandle;

// 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());
source

pub fn is_fingerprint(&self) -> bool

Returns whether the KeyHandle contains a fingerprint.

§Examples
let fpr: KeyHandle = "8F17 7771 18A3 3DDA 9BA4  8E62 AACB 3243 6300 52D9"
    .parse()?;
let keyid: KeyHandle = KeyHandle::from(KeyID::from(&fpr));

assert!(fpr.is_fingerprint());
assert!(! keyid.is_fingerprint());
source

pub fn is_keyid(&self) -> bool

Returns whether the KeyHandle contains a key ID.

§Examples
let fpr: KeyHandle = "8F17 7771 18A3 3DDA 9BA4  8E62 AACB 3243 6300 52D9"
    .parse()?;
let keyid: KeyHandle = KeyHandle::from(KeyID::from(&fpr));

assert!(! fpr.is_keyid());
assert!(keyid.is_keyid());
source

pub fn to_hex(&self) -> String

Converts this KeyHandle 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.

use openpgp::KeyHandle;

let h: KeyHandle =
    "0123 4567 89AB CDEF 0123 4567 89AB CDEF 0123 4567".parse()?;

assert_eq!("0123456789ABCDEF0123456789ABCDEF01234567", h.to_hex());
assert_eq!(format!("{:X}", h), h.to_hex());
source

pub fn to_spaced_hex(&self) -> String

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.

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());

Trait Implementations§

source§

impl Clone for KeyHandle

source§

fn clone(&self) -> KeyHandle

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for KeyHandle

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for KeyHandle

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<&Fingerprint> for KeyHandle

source§

fn from(i: &Fingerprint) -> Self

Converts to this type from the input type.
source§

impl From<&KeyHandle> for KeyID

source§

fn from(i: &KeyHandle) -> Self

Converts to this type from the input type.
source§

impl From<&KeyID> for KeyHandle

source§

fn from(i: &KeyID) -> Self

Converts to this type from the input type.
source§

impl From<Fingerprint> for KeyHandle

source§

fn from(i: Fingerprint) -> Self

Converts to this type from the input type.
source§

impl From<KeyHandle> for KeyID

source§

fn from(i: KeyHandle) -> Self

Converts to this type from the input type.
source§

impl From<KeyID> for KeyHandle

source§

fn from(i: KeyID) -> Self

Converts to this type from the input type.
source§

impl FromStr for KeyHandle

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl LowerHex for KeyHandle

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for KeyHandle

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for KeyHandle

source§

fn partial_cmp(&self, other: &KeyHandle) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl TryFrom<&KeyHandle> for Fingerprint

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(i: &KeyHandle) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<KeyHandle> for Fingerprint

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(i: KeyHandle) -> Result<Self>

Performs the conversion.
source§

impl UpperHex for KeyHandle

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T