feat: v1.9.4
This commit is contained in:
@@ -6,7 +6,7 @@ use rust_util::{util_msg, XResult};
|
||||
use rust_util::util_clap::{Command, CommandError};
|
||||
|
||||
use crate::pgpcardutil;
|
||||
use crate::util::{base64_decode, base64_encode};
|
||||
use crate::util::{base64_encode, try_decode};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum EncryptAlgo {
|
||||
@@ -30,11 +30,10 @@ impl Command for CommandImpl {
|
||||
fn name(&self) -> &str { "pgp-card-decrypt" }
|
||||
|
||||
fn subcommand<'a>(&self) -> App<'a, 'a> {
|
||||
SubCommand::with_name(self.name()).about("OpenPGP Card Decrypt subcommand")
|
||||
SubCommand::with_name(self.name()).about("OpenPGP Card decrypt subcommand")
|
||||
.arg(Arg::with_name("pin").short("p").long("pin").takes_value(true).default_value("123456").help("OpenPGP card user pin"))
|
||||
.arg(Arg::with_name("pass").long("pass").takes_value(true).help("[deprecated] now OpenPGP card user pin"))
|
||||
.arg(Arg::with_name("cipher").short("c").long("cipher").takes_value(true).help("Cipher text HEX"))
|
||||
.arg(Arg::with_name("cipher-base64").short("b").long("cipher-base64").takes_value(true).help("Cipher text base64"))
|
||||
.arg(Arg::with_name("ciphertext").short("c").long("ciphertext").takes_value(true).help("Cipher text (HEX or Base64)"))
|
||||
.arg(Arg::with_name("algo").long("algo").takes_value(true).help("Algo: RSA, X25519/ECDH"))
|
||||
.arg(Arg::with_name("json").long("json").help("JSON output"))
|
||||
}
|
||||
@@ -47,18 +46,15 @@ impl Command for CommandImpl {
|
||||
let pin = opt_value_result!(pin_opt, "User pin must be assigned");
|
||||
if pin.len() < 6 { return simple_error!("User pin length:{}, must >= 6!", pin.len()); }
|
||||
|
||||
let cipher = sub_arg_matches.value_of("cipher");
|
||||
let cipher_base64 = sub_arg_matches.value_of("cipher-base64");
|
||||
let ciphertext = sub_arg_matches.value_of("ciphertext");
|
||||
|
||||
let algo = sub_arg_matches.value_of("algo").unwrap_or("rsa").to_lowercase();
|
||||
let algo = EncryptAlgo::from_str(&algo)?;
|
||||
|
||||
let cipher_bytes = if let Some(cipher) = cipher {
|
||||
opt_result!(hex::decode(cipher), "Decode cipher failed: {}")
|
||||
} else if let Some(cipher_base64) = cipher_base64 {
|
||||
opt_result!(base64_decode(cipher_base64), "Decode cipher-base64 failed: {}")
|
||||
let ciphertext_bytes = if let Some(ciphertext) = ciphertext {
|
||||
opt_result!(try_decode(ciphertext), "Decode cipher failed: {}")
|
||||
} else {
|
||||
return simple_error!("cipher or cipher-base64 must assign one");
|
||||
return simple_error!("--ciphertext must be assigned");
|
||||
};
|
||||
|
||||
let mut pgp = pgpcardutil::get_openpgp_card()?;
|
||||
@@ -68,8 +64,8 @@ impl Command for CommandImpl {
|
||||
success!("User pin verify success!");
|
||||
|
||||
let text = match algo {
|
||||
EncryptAlgo::Rsa => trans.decipher(Cryptogram::RSA(&cipher_bytes))?,
|
||||
EncryptAlgo::Ecdh => trans.decipher(Cryptogram::ECDH(&cipher_bytes))?,
|
||||
EncryptAlgo::Rsa => trans.decipher(Cryptogram::RSA(&ciphertext_bytes))?,
|
||||
EncryptAlgo::Ecdh => trans.decipher(Cryptogram::ECDH(&ciphertext_bytes))?,
|
||||
};
|
||||
success!("Clear text HEX: {}", hex::encode(&text));
|
||||
success!("Clear text base64: {}", base64_encode(&text));
|
||||
@@ -80,8 +76,8 @@ impl Command for CommandImpl {
|
||||
|
||||
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("cipher_hex", hex::encode(&ciphertext_bytes));
|
||||
json.insert("cipher_base64", base64_encode(&ciphertext_bytes));
|
||||
json.insert("text_hex", hex::encode(&text));
|
||||
json.insert("text_base64", base64_encode(&text));
|
||||
if let Some(text) = text_opt {
|
||||
|
||||
Reference in New Issue
Block a user