From 6b0202a0c26a13f78a51dbca6b22ae676b40b063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Sun, 28 May 2023 08:51:38 +0200 Subject: openpgp: Implement Display and FromStr on CipherSuite --- openpgp/src/cert/builder.rs | 46 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/openpgp/src/cert/builder.rs b/openpgp/src/cert/builder.rs index 71ac1e90..36f25ce8 100644 --- a/openpgp/src/cert/builder.rs +++ b/openpgp/src/cert/builder.rs @@ -1,5 +1,9 @@ -use std::time; -use std::marker::PhantomData; +use std::{ + fmt::{Display, Formatter}, + marker::PhantomData, + str::FromStr, + time, +}; use crate::packet; use crate::packet::{ @@ -86,6 +90,44 @@ impl Default for CipherSuite { } } +impl Display for CipherSuite { + fn fmt(&self, formatter: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + match self { + CipherSuite::Cv25519 => formatter.write_str("cv25519")?, + CipherSuite::P256 => formatter.write_str("p256")?, + CipherSuite::P384 => formatter.write_str("p384")?, + CipherSuite::P521 => formatter.write_str("p521")?, + CipherSuite::RSA2k => formatter.write_str("rsa2k")?, + CipherSuite::RSA3k => formatter.write_str("rsa3k")?, + CipherSuite::RSA4k => formatter.write_str("rsa4k")?, + } + Ok(()) + } +} + +impl FromStr for CipherSuite { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + Ok(match s.to_lowercase().as_str() { + "cv25519" => CipherSuite::Cv25519, + "p256" => CipherSuite::P256, + "p384" => CipherSuite::P384, + "p521" => CipherSuite::P521, + "rsa2k" => CipherSuite::RSA2k, + "rsa3k" => CipherSuite::RSA3k, + "rsa4k" => CipherSuite::RSA4k, + cipher_suite => { + return Err(crate::Error::InvalidArgument(format!( + "cipher suite {} is not known", + cipher_suite + )) + .into()) + } + }) + } +} + impl CipherSuite { /// Returns whether the currently selected cryptographic backend /// supports the encryption and signing algorithms that the cipher -- cgit v1.2.3