From 1039b80518ca40920513da7dc38eb9be81ace9f6 Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 11 Nov 2022 20:41:40 +0100 Subject: [PATCH] feat: add asymetric & fix default value for decrypt flags --- recovery/src/main.rs | 45 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/recovery/src/main.rs b/recovery/src/main.rs index 357258c..d9a963e 100644 --- a/recovery/src/main.rs +++ b/recovery/src/main.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use url::Url; -use clap::{arg, command, value_parser, Command, ArgAction}; +use clap::{arg, command, value_parser, ArgAction, Command}; use sea_orm::{Database, DatabaseConnection}; use ::entity::candidate::Entity as Candidate; @@ -56,7 +56,7 @@ async fn main() -> Result<(), Box> { arg!( -d --decrypt ... "Decrypt flag" ) - .action(ArgAction::SetFalse) + .action(ArgAction::SetTrue) .required(false) ) .arg( @@ -72,6 +72,29 @@ async fn main() -> Result<(), Box> { .required(true), ) ) + .subcommand( + Command::new("asymetric") + .about("Asymetric encryption operations") + .arg( + arg!( + -d --decrypt ... "Decrypt flag" + ) + .action(ArgAction::SetTrue) + .required(false) + ) + .arg( + arg!( + -i --input "Plaintext to encrypt/decrypt" + ) + .required(true) + ) + .arg( + arg!( + -k --key "Public key / Private key" + ) + .required(true), + ) + ) .get_matches(); match clap.subcommand() { @@ -111,11 +134,25 @@ async fn main() -> Result<(), Box> { let input = sub_matches.get_one::("input").unwrap(); let key = sub_matches.get_one::("key").unwrap(); + let result = if !*decrypt { + portfolio_core::crypto::encrypt_password(input.to_string(), key.to_string()).await? + } else { + portfolio_core::crypto::decrypt_password(input.to_string(), key.to_string()).await? + }; + + println!("{}", result); + } + Some(("asymetric", sub_matches)) => { + let decrypt = sub_matches.get_one::("decrypt").unwrap(); + let input = sub_matches.get_one::("input").unwrap(); + let key = sub_matches.get_one::("key").unwrap(); + + println!("Decrypt: {}", decrypt); let result = if !*decrypt { - portfolio_core::crypto::decrypt_password(input.to_string(), key.to_string()).await? + portfolio_core::crypto::encrypt_password_with_recipients(input, &vec![key]).await? } else { - portfolio_core::crypto::encrypt_password(input.to_string(), key.to_string()).await? + portfolio_core::crypto::decrypt_password_with_private_key(input, key).await.map_err(|e| e.to_string())? }; println!("{}", result);