From 3dba0451cd67919d98ece951fef16a379fcaf2df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 26 Mar 2023 17:15:41 +0200 Subject: [PATCH] Add algorithm to the CLI --- src/algorithm.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/config.rs | 7 +++++++ src/main.rs | 1 + 3 files changed, 48 insertions(+) create mode 100644 src/algorithm.rs diff --git a/src/algorithm.rs b/src/algorithm.rs new file mode 100644 index 0000000..d84fc1e --- /dev/null +++ b/src/algorithm.rs @@ -0,0 +1,40 @@ +use std::str::FromStr; + +#[derive(Clone, Copy, Debug)] +pub enum Algorithm { + Ed25519Sha256, + Rsa2048Sha256, + Rsa3072Sha256, + Rsa4096Sha256, +} + +impl Default for Algorithm { + fn default() -> Self { + Self::Ed25519Sha256 + } +} + +impl ToString for Algorithm { + fn to_string(&self) -> String { + match self { + Self::Ed25519Sha256 => String::from("ed25519-sha256"), + Self::Rsa2048Sha256 => String::from("rsa2048-sha256"), + Self::Rsa3072Sha256 => String::from("rsa3072-sha256"), + Self::Rsa4096Sha256 => String::from("rsa4096-sha256"), + } + } +} + +impl FromStr for Algorithm { + type Err = String; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "ed25519-sha256" => Ok(Self::Ed25519Sha256), + "rsa2048-sha256" => Ok(Self::Rsa2048Sha256), + "rsa3072-sha256" => Ok(Self::Rsa3072Sha256), + "rsa4096-sha256" => Ok(Self::Rsa4096Sha256), + _ => Err(format!("{s}: invalid signing algorithm")), + } + } +} diff --git a/src/config.rs b/src/config.rs index 7098bbf..492dbcc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,4 @@ +use crate::algorithm::Algorithm; use crate::canonicalization::Canonicalization; use clap::Parser; use std::collections::HashSet; @@ -9,6 +10,8 @@ use std::path::PathBuf; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] pub struct Config { + #[arg(short, long, default_value_t = Algorithm::default())] + algorithm: Algorithm, #[arg(short, long, default_value_t = Canonicalization::default())] canonicalization: Canonicalization, #[arg(short, long)] @@ -37,6 +40,10 @@ impl Config { Ok(cnf) } + pub fn algorithm(&self) -> Algorithm { + self.algorithm + } + pub fn canonicalization(&self) -> Canonicalization { self.canonicalization } diff --git a/src/main.rs b/src/main.rs index cdb75a3..7418565 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod algorithm; mod canonicalization; mod config; mod entry;