Skip to main content

uselesskey_pgp/
spec.rs

1/// Specification for OpenPGP fixture generation.
2#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
3pub enum PgpSpec {
4    /// RSA 2048-bit OpenPGP key.
5    Rsa2048,
6    /// RSA 3072-bit OpenPGP key.
7    Rsa3072,
8    /// Ed25519 OpenPGP key.
9    Ed25519,
10}
11
12impl PgpSpec {
13    pub fn rsa_2048() -> Self {
14        Self::Rsa2048
15    }
16
17    pub fn rsa_3072() -> Self {
18        Self::Rsa3072
19    }
20
21    pub fn ed25519() -> Self {
22        Self::Ed25519
23    }
24
25    pub fn kind_name(&self) -> &'static str {
26        match self {
27            Self::Rsa2048 => "rsa2048",
28            Self::Rsa3072 => "rsa3072",
29            Self::Ed25519 => "ed25519",
30        }
31    }
32
33    /// Stable encoding for cache keys / deterministic derivation.
34    ///
35    /// If you change this, bump the derivation version in `uselesskey-core`.
36    pub fn stable_bytes(&self) -> [u8; 4] {
37        match self {
38            Self::Rsa2048 => [0, 0, 0, 1],
39            Self::Rsa3072 => [0, 0, 0, 2],
40            Self::Ed25519 => [0, 0, 0, 3],
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn stable_bytes_are_unique() {
51        let rsa_2048 = PgpSpec::rsa_2048().stable_bytes();
52        let rsa_3072 = PgpSpec::rsa_3072().stable_bytes();
53        let ed25519 = PgpSpec::ed25519().stable_bytes();
54
55        assert_ne!(rsa_2048, rsa_3072);
56        assert_ne!(rsa_2048, ed25519);
57        assert_ne!(rsa_3072, ed25519);
58    }
59
60    #[test]
61    fn kind_names_are_stable() {
62        assert_eq!(PgpSpec::rsa_2048().kind_name(), "rsa2048");
63        assert_eq!(PgpSpec::rsa_3072().kind_name(), "rsa3072");
64        assert_eq!(PgpSpec::ed25519().kind_name(), "ed25519");
65    }
66}