prs_lib/crypto/proto/
gpg.rs

1//! Crypto GPG protocol.
2
3/// GnuPG Long key ID length
4const LONG_KEY_ID: usize = 16;
5
6/// Represents a GPG key.
7#[derive(Clone)]
8pub struct Key {
9    /// Full fingerprint.
10    pub fingerprint: String,
11
12    /// Displayable user ID strings.
13    pub user_ids: Vec<String>,
14}
15
16impl Key {
17    /// Key fingerprint.
18    pub fn fingerprint(&self, short: bool) -> String {
19        let fp = if short {
20            &self.fingerprint[self.fingerprint.len() - LONG_KEY_ID..]
21        } else {
22            &self.fingerprint
23        };
24
25        crate::crypto::util::normalize_fingerprint(fp)
26    }
27
28    /// Key displayable user data.
29    pub fn display_user(&self) -> String {
30        self.user_ids.join("; ")
31    }
32
33    /// Transform into generic key.
34    pub fn into_key(self) -> crate::crypto::Key {
35        crate::crypto::Key::Gpg(self)
36    }
37}
38
39impl PartialEq for Key {
40    fn eq(&self, other: &Self) -> bool {
41        self.fingerprint.trim().to_uppercase() == other.fingerprint.trim().to_uppercase()
42    }
43}