use clap::{App, Arg, ArgMatches, SubCommand}; use rust_util::util_clap::{Command, CommandError}; use rust_util::util_msg; use std::collections::BTreeMap; use crate::hmacutil; pub struct CommandImpl; impl Command for CommandImpl { fn name(&self) -> &str { "hmac-decrypt" } fn subcommand<'a>(&self) -> App<'a, 'a> { SubCommand::with_name(self.name()) .about("Yubikey HMAC decrypt") .arg( Arg::with_name("ciphertext") .long("ciphertext") .takes_value(true) .help("Ciphertext"), ) .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 { util_msg::set_logger_std_out(false); } let ciphertext = sub_arg_matches.value_of("ciphertext").unwrap(); let plaintext = hmacutil::hmac_decrypt_to_string(ciphertext)?; if json_output { let mut json = BTreeMap::<&'_ str, String>::new(); json.insert("plaintext", plaintext); println!( "{}", serde_json::to_string_pretty(&json).expect("Convert to JSON failed!") ); } else { success!("Plaintext: {}", plaintext); } Ok(None) } }