cleanup imports round 2

This commit is contained in:
Bilal Elmoussaoui 2025-02-13 12:24:17 +01:00
parent dd7eb63afb
commit 7767b6e171
3 changed files with 15 additions and 28 deletions

View file

@ -1,8 +1,7 @@
use std::fmt::Display; use std::fmt::Display;
use iso7816_tlv::simple::Tlv; use iso7816_tlv::simple::Tlv;
use sha1::{Digest, Sha1}; use sha1::Digest;
use sha2::{Sha256, Sha512};
use strum::IntoEnumIterator; // 0.17.1 use strum::IntoEnumIterator; // 0.17.1
use strum_macros::EnumIter; // 0.17.1 use strum_macros::EnumIter; // 0.17.1
pub const INS_SELECT: u8 = 0xa4; pub const INS_SELECT: u8 = 0xa4;
@ -114,18 +113,18 @@ impl HashAlgo {
// returns a function capable of hashing a byte array // returns a function capable of hashing a byte array
pub fn get_hash_fun(&self) -> impl Fn(&[u8]) -> Vec<u8> { pub fn get_hash_fun(&self) -> impl Fn(&[u8]) -> Vec<u8> {
match self { match self {
HashAlgo::Sha1 => |m: &[u8]| { Self::Sha1 => |m: &[u8]| {
let mut hasher = Sha1::new(); let mut hasher = sha1::Sha1::new();
hasher.update(m); hasher.update(m);
hasher.finalize().to_vec() hasher.finalize().to_vec()
}, },
HashAlgo::Sha256 => |m: &[u8]| { Self::Sha256 => |m: &[u8]| {
let mut hasher = Sha256::new(); let mut hasher = sha2::Sha256::new();
hasher.update(m); hasher.update(m);
hasher.finalize().to_vec() hasher.finalize().to_vec()
}, },
HashAlgo::Sha512 => |m: &[u8]| { Self::Sha512 => |m: &[u8]| {
let mut hasher = Sha512::new(); let mut hasher = sha2::Sha512::new();
hasher.update(m); hasher.update(m);
hasher.finalize().to_vec() hasher.finalize().to_vec()
}, },
@ -135,9 +134,9 @@ impl HashAlgo {
// returns digest output size in number of bytes // returns digest output size in number of bytes
pub fn digest_size(&self) -> usize { pub fn digest_size(&self) -> usize {
match self { match self {
HashAlgo::Sha1 => 20, Self::Sha1 => 20,
HashAlgo::Sha256 => 32, Self::Sha256 => 32,
HashAlgo::Sha512 => 64, Self::Sha512 => 64,
} }
} }
} }

View file

@ -1,10 +1,6 @@
// SPDX-License-Identifier: BSD-3-Clause // SPDX-License-Identifier: BSD-3-Clause
use core::time;
use lib_ykoath2::OathSession; use lib_ykoath2::OathSession;
use pcsc;
use std::process;
use std::thread;
// use crate::args::Cli; // use crate::args::Cli;
// use clap::Parser; // use clap::Parser;
@ -27,7 +23,7 @@ fn main() {
// Show message if no YubiKey(s) // Show message if no YubiKey(s)
if yubikeys.len() == 0 { if yubikeys.len() == 0 {
println!("No yubikeys detected"); println!("No yubikeys detected");
process::exit(0); std::process::exit(0);
} }
// Print device info for all the YubiKeys we detected // Print device info for all the YubiKeys we detected
@ -53,7 +49,7 @@ fn main() {
println!("No credentials on device {}", device_label); println!("No credentials on device {}", device_label);
} }
thread::sleep(time::Duration::from_secs(5)); // show refresh is working std::thread::sleep(std::time::Duration::from_secs(5)); // show refresh is working
// Enumerate the OATH codes // Enumerate the OATH codes
for oath in codes { for oath in codes {

View file

@ -7,15 +7,7 @@ mod oath_credentialid;
use oath_credential::*; use oath_credential::*;
use oath_credentialid::*; use oath_credentialid::*;
/// Utilities for interacting with YubiKey OATH/TOTP functionality /// Utilities for interacting with YubiKey OATH/TOTP functionality
extern crate pcsc; use std::{fmt::Display, time::Duration};
use pbkdf2::pbkdf2_hmac_array;
use sha1::Sha1;
use std::{
fmt::Display,
str::{self},
time::Duration,
};
use base64::{engine::general_purpose, Engine as _}; use base64::{engine::general_purpose, Engine as _};
use hmac::{Hmac, Mac}; use hmac::{Hmac, Mac};
@ -32,13 +24,13 @@ fn _get_device_id(salt: Vec<u8>) -> String {
return general_purpose::URL_SAFE_NO_PAD.encode(hash_16_bytes); return general_purpose::URL_SAFE_NO_PAD.encode(hash_16_bytes);
} }
fn _hmac_sha1(key: &[u8], message: &[u8]) -> Vec<u8> { fn _hmac_sha1(key: &[u8], message: &[u8]) -> Vec<u8> {
let mut mac = Hmac::<Sha1>::new_from_slice(key).expect("Invalid key length"); let mut mac = Hmac::<sha1::Sha1>::new_from_slice(key).expect("Invalid key length");
mac.update(message); mac.update(message);
mac.finalize().into_bytes().to_vec() mac.finalize().into_bytes().to_vec()
} }
fn _derive_key(salt: &[u8], passphrase: &str) -> Vec<u8> { fn _derive_key(salt: &[u8], passphrase: &str) -> Vec<u8> {
pbkdf2_hmac_array::<Sha1, 16>(passphrase.as_bytes(), salt, 1000).to_vec() pbkdf2::pbkdf2_hmac_array::<sha1::Sha1, 16>(passphrase.as_bytes(), salt, 1000).to_vec()
} }
fn _hmac_shorten_key(key: &[u8], algo: HashAlgo) -> Vec<u8> { fn _hmac_shorten_key(key: &[u8], algo: HashAlgo) -> Vec<u8> {