From 91a5046ace8548338bc395ecde31f78a3ef3d064 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 27 Mar 2022 13:21:38 +0800 Subject: [PATCH] feat: chall supports sha1 --- src/cmd_chall.rs | 4 ++++ src/digest.rs | 13 ++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/cmd_chall.rs b/src/cmd_chall.rs index aefc9c3..0277ec4 100644 --- a/src/cmd_chall.rs +++ b/src/cmd_chall.rs @@ -15,6 +15,7 @@ impl Command for CommandImpl { SubCommand::with_name(self.name()).about("YubiKey challenge-response HMAC") .arg(Arg::with_name("challenge").short("c").long("challenge").takes_value(true).help("Challenge")) .arg(Arg::with_name("challenge-hex").short("x").long("challenge-hex").takes_value(true).help("Challenge HEX")) + .arg(Arg::with_name("sha1").short("1").long("sha1").help("Output SHA1")) .arg(Arg::with_name("sha256").short("2").long("sha256").help("Output SHA256")) .arg(Arg::with_name("sha384").short("3").long("sha384").help("Output SHA256")) .arg(Arg::with_name("sha512").short("5").long("sha512").help("Output SHA256")) @@ -25,6 +26,7 @@ impl Command for CommandImpl { let json_output = sub_arg_matches.is_present("json"); if json_output { rust_util::util_msg::set_logger_std_out(false); } + let sha1_output = sub_arg_matches.is_present("sha1"); let sha256_output = sub_arg_matches.is_present("sha256"); let sha384_output = sub_arg_matches.is_present("sha384"); let sha512_output = sub_arg_matches.is_present("sha512"); @@ -59,6 +61,7 @@ impl Command for CommandImpl { // Just for debug, lets check the hex let v: &[u8] = hmac_result.deref(); let hex_string = hex::encode(v); + let hex_sha1 = iff!(sha1_output, Some(crate::digest::sha1_bytes(v)), None); let hex_sha256 = iff!(sha256_output, Some(crate::digest::sha256_bytes(v)), None); let hex_sha384 = iff!(sha384_output, Some(crate::digest::sha384_bytes(v)), None); let hex_sha512 = iff!(sha512_output, Some(crate::digest::sha512_bytes(v)), None); @@ -67,6 +70,7 @@ impl Command for CommandImpl { let mut json = BTreeMap::<&'_ str, String>::new(); json.insert("challenge_hex", hex::encode(challenge_bytes)); json.insert("response_hex", hex_string); + hex_sha1.map(|hex_sha1| json.insert("response_sha1_hex", hex::encode(hex_sha1))); hex_sha256.map(|hex_sha256| json.insert("response_sha256_hex", hex::encode(hex_sha256))); hex_sha384.map(|hex_sha384| json.insert("response_sha384_hex", hex::encode(hex_sha384))); hex_sha512.map(|hex_sha512| json.insert("response_sha512_hex", hex::encode(hex_sha512))); diff --git a/src/digest.rs b/src/digest.rs index aa0938c..ae68531 100644 --- a/src/digest.rs +++ b/src/digest.rs @@ -1,7 +1,6 @@ +use sha1::Sha1; use sha2::{Digest, Sha256, Sha384, Sha512}; -// use sha1::Sha1; - // pub fn sha1(input: &str) -> Vec { // let mut challenge = Sha1::default(); // challenge.update(input.as_bytes()); @@ -14,11 +13,11 @@ pub fn sha256(input: &str) -> Vec { challenge.finalize().to_vec() } -// pub fn sha1_bytes(input: &[u8]) -> Vec { -// let mut challenge = Sha1::default(); -// challenge.update(input); -// challenge.digest().bytes().to_vec() -// } +pub fn sha1_bytes(input: &[u8]) -> Vec { + let mut challenge = Sha1::default(); + challenge.update(input); + challenge.digest().bytes().to_vec() +} pub fn sha256_bytes(input: &[u8]) -> Vec { let mut challenge = Sha256::default();