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-encrypt" } fn subcommand<'a>(&self) -> App<'a, 'a> { SubCommand::with_name(self.name()) .about("Yubikey HMAC encrypt") .arg( Arg::with_name("plaintext") .long("plaintext") .takes_value(true) .help("Plaintext"), ) .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 plaintext = sub_arg_matches.value_of("plaintext").unwrap(); let hmac_encrypt_ciphertext = hmacutil::hmac_encrypt_from_string(plaintext)?; if json_output { let mut json = BTreeMap::<&'_ str, String>::new(); json.insert("ciphertext", hmac_encrypt_ciphertext); println!( "{}", serde_json::to_string_pretty(&json).expect("Convert to JSON failed!") ); } else { success!("HMAC encrypt ciphertext: {}", hmac_encrypt_ciphertext); } Ok(None) } }