aboutsummaryrefslogtreecommitdiff
path: root/src/mnemonic.rs
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2024-06-28 13:38:09 +0200
committerNicholas Rodrigues Lordello <[email protected]>2024-06-28 13:38:13 +0200
commitbdb47ee691f0302c05daaf0278b606fbaf319f61 (patch)
tree3c414c0adbcfa8a76255502e9d5d38bdbb31d5d0 /src/mnemonic.rs
parent0f523a185ff67848e890156c04b95f765969051d (diff)
Update dependency version
Additionally, we start using `ethdigest` instead of the `sha3` crate for doing Keccak-256 hashing and representing digest types in code.
Diffstat (limited to 'src/mnemonic.rs')
-rw-r--r--src/mnemonic.rs15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/mnemonic.rs b/src/mnemonic.rs
index bd9e108..a9420c4 100644
--- a/src/mnemonic.rs
+++ b/src/mnemonic.rs
@@ -4,12 +4,11 @@ mod language;
mod wordlist;
pub use self::{language::Language, wordlist::WORD_COUNT};
-use crate::{hash, rand};
+use crate::rand;
use anyhow::{ensure, Context as _, Result};
use hmac::Hmac;
-use sha2::Sha512;
+use sha2::{Digest as _, Sha256, Sha512};
use std::{
- convert::TryInto,
fmt::{self, Display, Formatter},
mem,
ops::Deref,
@@ -49,7 +48,7 @@ impl Mnemonic {
let (seed, hash) = buf.split_at_mut(len);
rand::get_entropy(&mut *seed)?;
- hash[..32].copy_from_slice(&hash::sha256(seed));
+ hash_seed(seed, hash);
buf
};
@@ -94,7 +93,7 @@ impl Mnemonic {
debug_assert_eq!(len * 8 + bit_offset, words.len() * WORD_BITS);
debug_assert_eq!(byte_offset, len);
- hash[..32].copy_from_slice(&hash::sha256(seed));
+ hash_seed(seed, hash);
let checksum_mask = (1 << bit_offset) - 1;
ensure!(
@@ -209,6 +208,12 @@ fn mnemonic_to_byte_length(len: usize) -> Result<usize> {
Ok((len * WORD_BITS * 32 / 33) / 8)
}
+fn hash_seed(seed: &[u8], hash: &mut [u8]) {
+ let mut hasher = Sha256::new();
+ hasher.update(seed);
+ hasher.finalize_into((&mut hash[..32]).into());
+}
+
#[cfg(test)]
mod tests {
use super::*;