feat: v1.1.1, supports file in for pgp-card-sign
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -336,7 +336,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "card-cli"
|
||||
version = "1.1.0"
|
||||
version = "1.1.1"
|
||||
dependencies = [
|
||||
"authenticator",
|
||||
"base64 0.13.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "card-cli"
|
||||
version = "1.1.0"
|
||||
version = "1.1.1"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
|
||||
@@ -44,6 +44,10 @@ $ cargo r -- pgp-card-decrypt -c $(cat enc.txt | xxd -ps -c 11111)
|
||||
sign
|
||||
```
|
||||
$ cargo r -- pgp-card-sign -2 $(shasum -a 256 test.txt | awk '{print $1}')
|
||||
|
||||
OR
|
||||
|
||||
$ cargo r -- pgp-card-sign --in test.txt --use-sha256
|
||||
```
|
||||
|
||||
verify
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs::File;
|
||||
use std::io::{ErrorKind, Read};
|
||||
|
||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||
use digest::Digest;
|
||||
use openpgp_card::crypto_data::Hash;
|
||||
use openpgp_card::OpenPgp;
|
||||
use rust_util::util_clap::{Command, CommandError};
|
||||
use rust_util::XResult;
|
||||
use sha2::{Sha256, Sha384, Sha512};
|
||||
|
||||
const BUFF_SIZE: usize = 512 * 1024;
|
||||
|
||||
pub struct CommandImpl;
|
||||
|
||||
@@ -18,6 +24,10 @@ impl Command for CommandImpl {
|
||||
.arg(Arg::with_name("sha256").short("2").long("sha256").takes_value(true).help("Digest SHA256 HEX"))
|
||||
.arg(Arg::with_name("sha384").short("3").long("sha384").takes_value(true).help("Digest SHA384 HEX"))
|
||||
.arg(Arg::with_name("sha512").short("5").long("sha512").takes_value(true).help("Digest SHA512 HEX"))
|
||||
.arg(Arg::with_name("in").short("i").long("in").takes_value(true).help("File in"))
|
||||
.arg(Arg::with_name("use-sha256").long("use-sha256").help("Use SHA256 for file in"))
|
||||
.arg(Arg::with_name("use-sha384").long("use-sha384").help("Use SHA384 for file in"))
|
||||
.arg(Arg::with_name("use-sha512").long("use-sha512").help("Use SHA512 for file in"))
|
||||
.arg(Arg::with_name("json").long("json").help("JSON output"))
|
||||
}
|
||||
|
||||
@@ -29,9 +39,40 @@ 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 sha256 = sub_arg_matches.value_of("sha256");
|
||||
let sha384 = sub_arg_matches.value_of("sha384");
|
||||
let sha512 = sub_arg_matches.value_of("sha512");
|
||||
let mut sha256 = sub_arg_matches.value_of("sha256").map(|s| s.to_string());
|
||||
let mut sha384 = sub_arg_matches.value_of("sha384").map(|s| s.to_string());
|
||||
let mut sha512 = sub_arg_matches.value_of("sha512").map(|s| s.to_string());
|
||||
let file_in_opt = sub_arg_matches.value_of("in");
|
||||
|
||||
let mut json = BTreeMap::new();
|
||||
if let Some(file_in) = file_in_opt {
|
||||
if sha256.is_some() || sha384.is_some() || sha512.is_some() { return simple_error!("Conflict --in vs --sha256, --sha384, --sha512 args"); }
|
||||
|
||||
let use_sha256 = sub_arg_matches.is_present("use-sha256");
|
||||
let use_sha384 = sub_arg_matches.is_present("use-sha384");
|
||||
let use_sha512 = sub_arg_matches.is_present("use-sha512");
|
||||
|
||||
if !use_sha256 && !use_sha384 && !use_sha512 {
|
||||
return simple_error!("Must has one option --use-sha256, --use-sha384, --use-sha512");
|
||||
}
|
||||
|
||||
if use_sha256 {
|
||||
let hash = opt_result!(calc_file_digest::<Sha256>(file_in), "Calc file: {} SHA256 failed: {}", file_in);
|
||||
sha256 = Some(hex::encode(hash));
|
||||
}
|
||||
if use_sha384 {
|
||||
let hash = opt_result!(calc_file_digest::<Sha384>(file_in), "Calc file: {} SHA384 failed: {}", file_in);
|
||||
sha384 = Some(hex::encode(hash));
|
||||
}
|
||||
if use_sha512 {
|
||||
let hash = opt_result!(calc_file_digest::<Sha512>(file_in), "Calc file: {} SHA512 failed: {}", file_in);
|
||||
sha512 = Some(hex::encode(hash));
|
||||
}
|
||||
|
||||
let mut entry = BTreeMap::new();
|
||||
entry.insert("file", file_in.to_string());
|
||||
json.insert("meta", entry);
|
||||
}
|
||||
|
||||
if sha256.is_none() && sha384.is_none() && sha512.is_none() {
|
||||
return simple_error!("SHA256, SHA384 or SHA512 must assign at least one");
|
||||
@@ -41,13 +82,11 @@ impl Command for CommandImpl {
|
||||
let mut pgp = OpenPgp::new(&mut card);
|
||||
let mut trans = opt_result!(pgp.transaction(), "Open card failed: {}");
|
||||
|
||||
opt_result!(trans.verify_pw1_sign(pin.as_ref()), "User sign pin verify failed: {}");
|
||||
success!("User sign pin verify success!");
|
||||
|
||||
let mut json = BTreeMap::new();
|
||||
if let Some(sha256) = sha256 {
|
||||
let sha256_hex = opt_result!(hex::decode(sha256.trim()), "Decode sha256 failed: {}");
|
||||
let sha256_hex = copy_sha256(&sha256_hex)?;
|
||||
opt_result!(trans.verify_pw1_sign(pin.as_ref()), "User sign pin verify failed: {}");
|
||||
success!("User sign pin verify success!");
|
||||
let sig = trans.signature_for_hash(Hash::SHA256(sha256_hex))?;
|
||||
success!("SHA256 signature HEX: {}", hex::encode(&sig));
|
||||
success!("SHA256 signature base64: {}", base64::encode(&sig));
|
||||
@@ -61,6 +100,8 @@ impl Command for CommandImpl {
|
||||
if let Some(sha384) = sha384 {
|
||||
let sha384_hex = opt_result!(hex::decode(sha384.trim()), "Decode sha384 failed: {}");
|
||||
let sha384_hex = copy_sha384(&sha384_hex)?;
|
||||
opt_result!(trans.verify_pw1_sign(pin.as_ref()), "User sign pin verify failed: {}");
|
||||
success!("User sign pin verify success!");
|
||||
let sig = trans.signature_for_hash(Hash::SHA384(sha384_hex))?;
|
||||
success!("SHA384 signature HEX: {}", hex::encode(&sig));
|
||||
success!("SHA384 signature base64: {}", base64::encode(&sig));
|
||||
@@ -74,6 +115,8 @@ impl Command for CommandImpl {
|
||||
if let Some(sha512) = sha512 {
|
||||
let sha512_hex = opt_result!(hex::decode(sha512.trim()), "Decode sha512 failed: {}");
|
||||
let sha512_hex = copy_sha512(&sha512_hex)?;
|
||||
opt_result!(trans.verify_pw1_sign(pin.as_ref()), "User sign pin verify failed: {}");
|
||||
success!("User sign pin verify success!");
|
||||
let sig = trans.signature_for_hash(Hash::SHA512(sha512_hex))?;
|
||||
success!("SHA512 signature HEX: {}", hex::encode(&sig));
|
||||
success!("SHA512 signature base64: {}", base64::encode(&sig));
|
||||
@@ -93,6 +136,23 @@ impl Command for CommandImpl {
|
||||
}
|
||||
}
|
||||
|
||||
fn calc_file_digest<D>(file_name: &str) -> XResult<Vec<u8>> where D: Digest {
|
||||
let mut hasher = D::new();
|
||||
let mut buf: [u8; BUFF_SIZE] = [0u8; BUFF_SIZE];
|
||||
let mut f = File::open(file_name)?;
|
||||
let file_len = f.metadata()?.len();
|
||||
debugging!("File: {}, length: {}", file_name, file_len);
|
||||
loop {
|
||||
let len = match f.read(&mut buf) {
|
||||
Ok(0) => return Ok(hasher.finalize().as_slice().to_vec()),
|
||||
Ok(len) => len,
|
||||
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
||||
Err(e) => return simple_error!("Calc file digest failed: {}", e),
|
||||
};
|
||||
hasher.update(&buf[..len]);
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! define_copy_array {
|
||||
($fn_name: ident, $len: tt) => (
|
||||
fn $fn_name(in_arr: &[u8]) -> XResult<[u8; $len]> {
|
||||
|
||||
Reference in New Issue
Block a user