From 8d163a5ec2a73e74c97495d736abc37b940c26b8 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 20 Nov 2023 22:40:18 +0800 Subject: [PATCH] feat: optimize code --- src/cmd_chall.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/cmd_chall.rs b/src/cmd_chall.rs index aaa1f32..d73f046 100644 --- a/src/cmd_chall.rs +++ b/src/cmd_chall.rs @@ -31,24 +31,26 @@ impl Command for CommandImpl { let challenge_bytes = hmacutil::get_challenge_bytes(sub_arg_matches)?; let mut yubi = Yubico::new(); - if let Ok(device) = yubi.find_yubikey() { - success!("Found key, Vendor ID: {:?}, Product ID: {:?}", device.vendor_id, device.product_id); + let device = match yubi.find_yubikey() { + Ok(device) => device, + Err(_) => { + warning!("YubiKey not found"); + return Ok(Some(1)); + } + }; - let config = Config::default() - .set_vendor_id(device.vendor_id) - .set_product_id(device.product_id) - .set_variable_size(true) - .set_mode(Mode::Sha1) - .set_slot(Slot::Slot2); + success!("Found key, Vendor ID: {:?}, Product ID: {:?}", device.vendor_id, device.product_id); + let config = Config::default() + .set_vendor_id(device.vendor_id) + .set_product_id(device.product_id) + .set_variable_size(true) + .set_mode(Mode::Sha1) + .set_slot(Slot::Slot2); - // In HMAC Mode, the result will always be the SAME for the SAME provided challenge - let hmac_result = opt_result!(yubi.challenge_response_hmac(&challenge_bytes, config), "Challenge HMAC failed: {}"); + // In HMAC Mode, the result will always be the SAME for the SAME provided challenge + let hmac_result = opt_result!(yubi.challenge_response_hmac(&challenge_bytes, config), "Challenge HMAC failed: {}"); - hmacutil::output_hmac_result(sub_arg_matches, json_output, challenge_bytes, hmac_result.deref()); - } else { - warning!("YubiKey not found"); - return Ok(Some(1)); - } + hmacutil::output_hmac_result(sub_arg_matches, json_output, challenge_bytes, hmac_result.deref()); Ok(None) }