From 7767b6e1714e9166a23531a793a75a7dc543bba2 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Thu, 13 Feb 2025 12:24:17 +0100 Subject: [PATCH] cleanup imports round 2 --- src/constants.rs | 21 ++++++++++----------- src/example.rs | 8 ++------ src/lib.rs | 14 +++----------- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 27d8709..efdc6fb 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,8 +1,7 @@ use std::fmt::Display; use iso7816_tlv::simple::Tlv; -use sha1::{Digest, Sha1}; -use sha2::{Sha256, Sha512}; +use sha1::Digest; use strum::IntoEnumIterator; // 0.17.1 use strum_macros::EnumIter; // 0.17.1 pub const INS_SELECT: u8 = 0xa4; @@ -114,18 +113,18 @@ impl HashAlgo { // returns a function capable of hashing a byte array pub fn get_hash_fun(&self) -> impl Fn(&[u8]) -> Vec { match self { - HashAlgo::Sha1 => |m: &[u8]| { - let mut hasher = Sha1::new(); + Self::Sha1 => |m: &[u8]| { + let mut hasher = sha1::Sha1::new(); hasher.update(m); hasher.finalize().to_vec() }, - HashAlgo::Sha256 => |m: &[u8]| { - let mut hasher = Sha256::new(); + Self::Sha256 => |m: &[u8]| { + let mut hasher = sha2::Sha256::new(); hasher.update(m); hasher.finalize().to_vec() }, - HashAlgo::Sha512 => |m: &[u8]| { - let mut hasher = Sha512::new(); + Self::Sha512 => |m: &[u8]| { + let mut hasher = sha2::Sha512::new(); hasher.update(m); hasher.finalize().to_vec() }, @@ -135,9 +134,9 @@ impl HashAlgo { // returns digest output size in number of bytes pub fn digest_size(&self) -> usize { match self { - HashAlgo::Sha1 => 20, - HashAlgo::Sha256 => 32, - HashAlgo::Sha512 => 64, + Self::Sha1 => 20, + Self::Sha256 => 32, + Self::Sha512 => 64, } } } diff --git a/src/example.rs b/src/example.rs index 1931d1c..951c16d 100644 --- a/src/example.rs +++ b/src/example.rs @@ -1,10 +1,6 @@ // SPDX-License-Identifier: BSD-3-Clause -use core::time; use lib_ykoath2::OathSession; -use pcsc; -use std::process; -use std::thread; // use crate::args::Cli; // use clap::Parser; @@ -27,7 +23,7 @@ fn main() { // Show message if no YubiKey(s) if yubikeys.len() == 0 { println!("No yubikeys detected"); - process::exit(0); + std::process::exit(0); } // Print device info for all the YubiKeys we detected @@ -53,7 +49,7 @@ fn main() { 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 for oath in codes { diff --git a/src/lib.rs b/src/lib.rs index 4433066..40258ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,15 +7,7 @@ mod oath_credentialid; use oath_credential::*; use oath_credentialid::*; /// Utilities for interacting with YubiKey OATH/TOTP functionality -extern crate pcsc; -use pbkdf2::pbkdf2_hmac_array; -use sha1::Sha1; - -use std::{ - fmt::Display, - str::{self}, - time::Duration, -}; +use std::{fmt::Display, time::Duration}; use base64::{engine::general_purpose, Engine as _}; use hmac::{Hmac, Mac}; @@ -32,13 +24,13 @@ fn _get_device_id(salt: Vec) -> String { return general_purpose::URL_SAFE_NO_PAD.encode(hash_16_bytes); } fn _hmac_sha1(key: &[u8], message: &[u8]) -> Vec { - let mut mac = Hmac::::new_from_slice(key).expect("Invalid key length"); + let mut mac = Hmac::::new_from_slice(key).expect("Invalid key length"); mac.update(message); mac.finalize().into_bytes().to_vec() } fn _derive_key(salt: &[u8], passphrase: &str) -> Vec { - pbkdf2_hmac_array::(passphrase.as_bytes(), salt, 1000).to_vec() + pbkdf2::pbkdf2_hmac_array::(passphrase.as_bytes(), salt, 1000).to_vec() } fn _hmac_shorten_key(key: &[u8], algo: HashAlgo) -> Vec {