From 42fe21904054dca1d4c0d71a750c3fe995f4f5a6 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 20 Feb 2023 09:03:02 +0200 Subject: [PATCH] fix clippy warnings --- src/lib.rs | 29 +++++++++++++++-------------- src/pbkdf2.rs | 23 ++++++++++++++--------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9337e29..1294ee0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -175,7 +175,7 @@ impl Mnemonic { /// can be avoided for languages without special UTF8 characters. #[inline] #[cfg(feature = "std")] - fn normalize_utf8_cow<'a>(cow: &mut Cow<'a, str>) { + fn normalize_utf8_cow(cow: &mut Cow) { let is_nfkd = unicode_normalization::is_nfkd_quick(cow.as_ref().chars()); if is_nfkd != unicode_normalization::IsNormalized::Yes { *cow = Cow::Owned(cow.as_ref().nfkd().to_string()); @@ -184,7 +184,7 @@ impl Mnemonic { /// Create a new [Mnemonic] in the specified language from the given entropy. /// Entropy must be a multiple of 32 bits (4 bytes) and 128-256 bits in length. - pub fn from_entropy_in(language: Language, entropy: &[u8]) -> Result { + pub fn from_entropy_in(lang: Language, entropy: &[u8]) -> Result { const MAX_ENTROPY_BITS: usize = 256; const MIN_ENTROPY_BITS: usize = 128; const MAX_CHECKSUM_BITS: usize = 8; @@ -195,11 +195,11 @@ impl Mnemonic { if nb_bits % 32 != 0 { return Err(Error::BadEntropyBitCount(nb_bits)); } - if nb_bits < MIN_ENTROPY_BITS || nb_bits > MAX_ENTROPY_BITS { + if !(MIN_ENTROPY_BITS..=MAX_ENTROPY_BITS).contains(&nb_bits) { return Err(Error::BadEntropyBitCount(nb_bits)); } - let check = sha256::Hash::hash(&entropy); + let check = sha256::Hash::hash(entropy); let mut bits = [false; MAX_ENTROPY_BITS + MAX_CHECKSUM_BITS]; for i in 0..nb_bytes { for j in 0..8 { @@ -223,8 +223,8 @@ impl Mnemonic { } Ok(Mnemonic { - lang: language, - words: words, + lang, + words, }) } @@ -320,7 +320,7 @@ impl Mnemonic { { // Start scope to drop first_word so that words can be reborrowed later. let first_word = words.peek().ok_or(Error::BadWordCount(0))?; - if first_word.len() == 0 { + if first_word.is_empty() { return Err(Error::BadWordCount(0)); } @@ -364,7 +364,7 @@ impl Mnemonic { } } - return Err(Error::AmbiguousLanguages(AmbiguousLanguages(possible))); + Err(Error::AmbiguousLanguages(AmbiguousLanguages(possible))) } /// Determine the language of the mnemonic. @@ -382,7 +382,7 @@ impl Mnemonic { } /// Parse a mnemonic in normalized UTF8 in the given language. - pub fn parse_in_normalized(language: Language, s: &str) -> Result { + pub fn parse_in_normalized(lang: Language, s: &str) -> Result { let nb_words = s.split_whitespace().count(); if is_invalid_word_count(nb_words) { return Err(Error::BadWordCount(nb_words)); @@ -396,7 +396,7 @@ impl Mnemonic { let mut bits = [false; MAX_NB_WORDS * 11]; for (i, word) in s.split_whitespace().enumerate() { - let idx = language.find_word(word).ok_or(Error::UnknownWord(i))?; + let idx = lang.find_word(word).ok_or(Error::UnknownWord(i))?; words[i] = idx; @@ -424,8 +424,8 @@ impl Mnemonic { } Ok(Mnemonic { - lang: language, - words: words, + lang, + words, }) } @@ -443,7 +443,8 @@ impl Mnemonic { ) -> Result { let mut cow = s.into(); Mnemonic::normalize_utf8_cow(&mut cow); - Ok(Mnemonic::parse_in_normalized(language, cow.as_ref())?) + + Mnemonic::parse_in_normalized(language, cow.as_ref()) } /// Parse a mnemonic and detect the language from the enabled languages. @@ -458,7 +459,7 @@ impl Mnemonic { Mnemonic::language_of(cow.as_ref())? }; - Ok(Mnemonic::parse_in_normalized(language, cow.as_ref())?) + Mnemonic::parse_in_normalized(language, cow.as_ref()) } /// Get the number of words in the mnemonic. diff --git a/src/pbkdf2.rs b/src/pbkdf2.rs index e7d3375..9535230 100644 --- a/src/pbkdf2.rs +++ b/src/pbkdf2.rs @@ -1,10 +1,11 @@ use bitcoin_hashes::{hmac, sha512, Hash, HashEngine}; -const SALT_PREFIX: &'static str = "mnemonic"; +const SALT_PREFIX: &str = "mnemonic"; /// Calculate the binary size of the mnemonic. fn mnemonic_byte_len(mnemonic: M) -> usize - where M: Iterator + Clone, +where + M: Iterator + Clone, { let mut len = 0; for (i, word) in mnemonic.enumerate() { @@ -18,7 +19,8 @@ fn mnemonic_byte_len(mnemonic: M) -> usize /// Wrote the mnemonic in binary form into the hash engine. fn mnemonic_write_into(mnemonic: M, engine: &mut sha512::HashEngine) - where M: Iterator + Clone, +where + M: Iterator + Clone, { for (i, word) in mnemonic.enumerate() { if i > 0 { @@ -32,7 +34,8 @@ fn mnemonic_write_into(mnemonic: M, engine: &mut sha512::HashEngine) /// We need a special method because we can't allocate a new byte /// vector for the entire serialized mnemonic. fn create_hmac_engine(mnemonic: M) -> hmac::HmacEngine - where M: Iterator + Clone, +where + M: Iterator + Clone, { // Inner code is borrowed from the bitcoin_hashes::hmac::HmacEngine::new method. let mut ipad = [0x36u8; 128]; @@ -58,8 +61,8 @@ fn create_hmac_engine(mnemonic: M) -> hmac::HmacEngine let mut cursor = 0; for (i, word) in mnemonic.enumerate() { if i > 0 { - ipad[cursor] ^= ' ' as u8; - opad[cursor] ^= ' ' as u8; + ipad[cursor] ^= b' '; + opad[cursor] ^= b' '; cursor += 1; } for (b_i, b_h) in ipad.iter_mut().skip(cursor).zip(word.as_bytes()) { @@ -82,9 +85,10 @@ fn create_hmac_engine(mnemonic: M) -> hmac::HmacEngine #[inline] fn u32_to_array_be(val: u32) -> [u8; 4] { let mut res = [0; 4]; - for i in 0..4 { - res[i] = ((val >> (4 - i - 1) * 8) & 0xff) as u8; + for (i, item) in res.iter_mut().enumerate() { + *item = ((val >> ((4 - i - 1) * 8)) & 0xff) as u8; } + res } @@ -97,7 +101,8 @@ fn xor(res: &mut [u8], salt: &[u8]) { /// PBKDF2-HMAC-SHA512 implementation using bitcoin_hashes. pub(crate) fn pbkdf2(mnemonic: M, unprefixed_salt: &[u8], c: usize, res: &mut [u8]) - where M: Iterator + Clone, +where + M: Iterator + Clone, { let prf = create_hmac_engine(mnemonic);