From 98f4d475fb5c2a8e41dd47c6d9c898eca8ed2732 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 11 Jul 2021 09:55:47 +0800 Subject: [PATCH] feat: add json output for pgp card sign --- src/pgpcardlist.rs | 6 ++---- src/pgpcardsign.rs | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/pgpcardlist.rs b/src/pgpcardlist.rs index e481045..27f9081 100644 --- a/src/pgpcardlist.rs +++ b/src/pgpcardlist.rs @@ -1,4 +1,4 @@ -use clap::{ArgMatches, SubCommand, App, Arg}; +use clap::{ArgMatches, SubCommand, App}; use crate::cmd::{Command, CommandError}; use openpgp_card::{OpenPGPCard, DecryptMe, Hash}; @@ -9,9 +9,7 @@ impl Command for CommandImpl { fn subcommand<'a>(&self) -> App<'a, 'a> { SubCommand::with_name(self.name()).about("OpenPGP Card List subcommand") - // .arg(Arg::with_name("app-id").long("app-id").default_value("https://example.com").help("App id")) - // .arg(Arg::with_name("timeout").long("timeout").default_value("10").help("Timeout in seconds")) - .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 { diff --git a/src/pgpcardsign.rs b/src/pgpcardsign.rs index 70f9e2f..1a53e72 100644 --- a/src/pgpcardsign.rs +++ b/src/pgpcardsign.rs @@ -2,6 +2,7 @@ use clap::{ArgMatches, SubCommand, App, Arg}; use crate::cmd::{Command, CommandError}; use openpgp_card::{OpenPGPCard, Hash, OpenPGPCardUser}; use rust_util::XResult; +use std::collections::BTreeMap; pub struct CommandImpl; @@ -18,6 +19,10 @@ impl Command for CommandImpl { } 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); + } let pass = sub_arg_matches.value_of("pass"); let pass = match pass { Some(p) => p, @@ -31,13 +36,20 @@ impl Command for CommandImpl { return simple_error!("SHA256, SHA384 or SHA512 must assign one"); } + let mut json = BTreeMap::new(); if let Some(sha256) = sha256 { let user = get_card_user_sw1_81(pass)?; let sha256_hex = opt_result!(hex::decode(sha256), "Decode sha256 failed: {}"); let sha256_hex = copy_sha256(&sha256_hex)?; let sig = user.signature_for_hash(Hash::SHA256(sha256_hex))?; success!("SHA256 signature: {}", hex::encode(&sig)); - success!("SHA256 signature: {}", base64::encode(&sig)); + // success!("SHA256 signature: {}", base64::encode(&sig)); + if json_output { + let mut entry = BTreeMap::new(); + entry.insert("digest", hex::encode(&sha256_hex)); + entry.insert("signature", hex::encode(&sig)); + json.insert("sha256", entry); + } } if let Some(sha384) = sha384 { let user = get_card_user_sw1_81(pass)?; @@ -45,7 +57,13 @@ impl Command for CommandImpl { let sha384_hex = copy_sha384(&sha384_hex)?; let sig = user.signature_for_hash(Hash::SHA384(sha384_hex))?; success!("SHA384 signature: {}", hex::encode(&sig)); - success!("SHA384 signature: {}", base64::encode(&sig)); + // success!("SHA384 signature: {}", base64::encode(&sig)); + if json_output { + let mut entry = BTreeMap::new(); + entry.insert("digest", hex::encode(&sha384_hex)); + entry.insert("signature", hex::encode(&sig)); + json.insert("sha384", entry); + } } if let Some(sha512) = sha512 { let user = get_card_user_sw1_81(pass)?; @@ -53,7 +71,17 @@ impl Command for CommandImpl { let sha512_hex = copy_sha512(&sha512_hex)?; let sig = user.signature_for_hash(Hash::SHA512(sha512_hex))?; success!("SHA512 signature: {}", hex::encode(&sig)); - success!("SHA512 signature: {}", base64::encode(&sig)); + // success!("SHA512 signature: {}", base64::encode(&sig)); + if json_output { + let mut entry = BTreeMap::new(); + entry.insert("digest", hex::encode(&sha512_hex)); + entry.insert("signature", hex::encode(&sig)); + json.insert("sha512", entry); + } + } + + if json_output { + println!("{}", serde_json::to_string_pretty(&json).unwrap()); } Ok(())