feat: add json output for pgp card sign

This commit is contained in:
2021-07-11 09:55:47 +08:00
parent 8a5bb56de9
commit 98f4d475fb
2 changed files with 33 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
use clap::{ArgMatches, SubCommand, App, Arg}; use clap::{ArgMatches, SubCommand, App};
use crate::cmd::{Command, CommandError}; use crate::cmd::{Command, CommandError};
use openpgp_card::{OpenPGPCard, DecryptMe, Hash}; use openpgp_card::{OpenPGPCard, DecryptMe, Hash};
@@ -9,9 +9,7 @@ impl Command for CommandImpl {
fn subcommand<'a>(&self) -> App<'a, 'a> { fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("OpenPGP Card List subcommand") 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("json").long("json").help("JSON output"))
// .arg(Arg::with_name("timeout").long("timeout").default_value("10").help("Timeout in seconds"))
.arg(Arg::with_name("json").long("json").help("JSON output"))
} }
fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError { fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError {

View File

@@ -2,6 +2,7 @@ use clap::{ArgMatches, SubCommand, App, Arg};
use crate::cmd::{Command, CommandError}; use crate::cmd::{Command, CommandError};
use openpgp_card::{OpenPGPCard, Hash, OpenPGPCardUser}; use openpgp_card::{OpenPGPCard, Hash, OpenPGPCardUser};
use rust_util::XResult; use rust_util::XResult;
use std::collections::BTreeMap;
pub struct CommandImpl; pub struct CommandImpl;
@@ -18,6 +19,10 @@ impl Command for CommandImpl {
} }
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { 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 = sub_arg_matches.value_of("pass");
let pass = match pass { let pass = match pass {
Some(p) => p, Some(p) => p,
@@ -31,13 +36,20 @@ impl Command for CommandImpl {
return simple_error!("SHA256, SHA384 or SHA512 must assign one"); return simple_error!("SHA256, SHA384 or SHA512 must assign one");
} }
let mut json = BTreeMap::new();
if let Some(sha256) = sha256 { if let Some(sha256) = sha256 {
let user = get_card_user_sw1_81(pass)?; let user = get_card_user_sw1_81(pass)?;
let sha256_hex = opt_result!(hex::decode(sha256), "Decode sha256 failed: {}"); let sha256_hex = opt_result!(hex::decode(sha256), "Decode sha256 failed: {}");
let sha256_hex = copy_sha256(&sha256_hex)?; let sha256_hex = copy_sha256(&sha256_hex)?;
let sig = user.signature_for_hash(Hash::SHA256(sha256_hex))?; let sig = user.signature_for_hash(Hash::SHA256(sha256_hex))?;
success!("SHA256 signature: {}", hex::encode(&sig)); 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 { if let Some(sha384) = sha384 {
let user = get_card_user_sw1_81(pass)?; let user = get_card_user_sw1_81(pass)?;
@@ -45,7 +57,13 @@ impl Command for CommandImpl {
let sha384_hex = copy_sha384(&sha384_hex)?; let sha384_hex = copy_sha384(&sha384_hex)?;
let sig = user.signature_for_hash(Hash::SHA384(sha384_hex))?; let sig = user.signature_for_hash(Hash::SHA384(sha384_hex))?;
success!("SHA384 signature: {}", hex::encode(&sig)); 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 { if let Some(sha512) = sha512 {
let user = get_card_user_sw1_81(pass)?; let user = get_card_user_sw1_81(pass)?;
@@ -53,7 +71,17 @@ impl Command for CommandImpl {
let sha512_hex = copy_sha512(&sha512_hex)?; let sha512_hex = copy_sha512(&sha512_hex)?;
let sig = user.signature_for_hash(Hash::SHA512(sha512_hex))?; let sig = user.signature_for_hash(Hash::SHA512(sha512_hex))?;
success!("SHA512 signature: {}", hex::encode(&sig)); 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(()) Ok(())