diff --git a/src/pgpcarddecrypt.rs b/src/pgpcarddecrypt.rs index 53a2f5f..2932ac1 100644 --- a/src/pgpcarddecrypt.rs +++ b/src/pgpcarddecrypt.rs @@ -29,8 +29,6 @@ impl Command for CommandImpl { let cipher = sub_arg_matches.value_of("cipher"); let cipher_base64 = sub_arg_matches.value_of("cipher-base64"); - let mut json = BTreeMap::<&'_ str, String>::new(); - let cipher_bytes = if let Some(cipher) = cipher { opt_result!(hex::decode(cipher), "Decode cipher failed: {}") } else if let Some(cipher_base64) = cipher_base64 { @@ -44,18 +42,16 @@ impl Command for CommandImpl { success!("Clear text HEX: {}", hex::encode(&text)); success!("Clear text base64: {}", base64::encode(&text)); success!("Clear text UTF-8: {}", String::from_utf8_lossy(&text)); + if json_output { + let mut json = BTreeMap::<&'_ str, String>::new(); json.insert("cipher_hex", hex::encode(&cipher_bytes)); json.insert("cipher_base64", base64::encode(&cipher_bytes)); json.insert("text_hex", hex::encode(&text)); json.insert("text_utf8", String::from_utf8_lossy(&text).to_string()); - } - - if json_output { println!("{}", serde_json::to_string_pretty(&json).unwrap()); } - Ok(None) } } diff --git a/src/pivsign.rs b/src/pivsign.rs index 50f65e9..89eb9d0 100644 --- a/src/pivsign.rs +++ b/src/pivsign.rs @@ -1,3 +1,4 @@ +use std::collections::BTreeMap; use clap::{App, Arg, ArgMatches, SubCommand}; use rust_util::util_clap::{Command, CommandError}; use yubikey::piv::{AlgorithmId, SlotId}; @@ -11,10 +12,14 @@ impl Command for CommandImpl { fn subcommand<'a>(&self) -> App<'a, 'a> { SubCommand::with_name(self.name()).about("PIV Sign subcommand") .arg(Arg::with_name("pass").short("p").long("pass").takes_value(true).default_value("123456").help("OpenPGP card password")) - // .arg(Arg::with_name("json").long("json").help("JSON output")) + .arg(Arg::with_name("json").long("json").help("JSON output")) } fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { + let json_output = sub_arg_matches.is_present("json"); + if json_output { + rust_util::util_msg::set_logger_std_out(false); + } warning!("This feature is not complete"); let pass_opt = sub_arg_matches.value_of("pass"); let pass = opt_value_result!(pass_opt, "Pass must be assigned"); @@ -25,8 +30,16 @@ impl Command for CommandImpl { let sign_result = yubikey::piv::sign_data(&mut yk, &raw_in, AlgorithmId::Rsa2048, SlotId::Signature); let sign = opt_result!(sign_result, "Sign data failed: {}"); let sign_bytes = sign.as_slice(); - success!("Signature HEX: {}", hex::encode(sign_bytes)); - success!("Signature base64: {}", base64::encode(sign_bytes)); + + if json_output { + let mut json = BTreeMap::<&'_ str, String>::new(); + json.insert("sign_hex", hex::encode(&sign_bytes)); + json.insert("sign_base64", base64::encode(&sign_bytes)); + println!("{}", serde_json::to_string_pretty(&json).unwrap()); + } else { + success!("Signature HEX: {}", hex::encode(sign_bytes)); + success!("Signature base64: {}", base64::encode(sign_bytes)); + } Ok(None) } }