diff --git a/Cargo.toml b/Cargo.toml index 438cc56..3910f6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,8 +27,9 @@ italian = [] japanese = [] korean = [] spanish = [] +default-langs = ["chinese-simplified", "chinese-traditional", "french", "italian", "japanese", "korean", "spanish"] -default = ["chinese-simplified", "chinese-traditional", "french", "italian", "japanese", "korean", "spanish"] +default = ["default-langs", "rand"] [dependencies] anyhow = "1.0.57" @@ -37,7 +38,7 @@ rustc-hash = "1.1.0" sha2 = "0.10.2" hmac = "0.12.1" pbkdf2 = { version = "0.11.0", default-features = false } -rand = "0.8.5" +rand = { version = "0.8.5", optional = true } once_cell = "1.12.0" unicode-normalization = "0.1.19" zeroize = { version = "1.5.5", features = ["zeroize_derive"] } diff --git a/src/crypto.rs b/src/crypto.rs index a32fe79..519369a 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -5,9 +5,9 @@ //! [Seed]: ../seed/struct.Seed.html //! -extern crate rand; -use self::rand::{thread_rng, RngCore}; use hmac::Hmac; +#[cfg(feature = "rand")] +use rand::{thread_rng, RngCore}; use sha2::Digest; const PBKDF2_ROUNDS: u32 = 2048; @@ -21,6 +21,7 @@ pub(crate) fn sha256_first_byte(input: &[u8]) -> u8 { /// Random byte generator, used to create new mnemonics /// +#[cfg(feature = "rand")] pub(crate) fn gen_random_bytes(byte_length: usize) -> Vec { let mut rng = thread_rng(); let mut bytes = vec![0u8; byte_length]; diff --git a/src/mnemonic.rs b/src/mnemonic.rs index a259b47..dd659db 100644 --- a/src/mnemonic.rs +++ b/src/mnemonic.rs @@ -1,13 +1,15 @@ -use std::fmt; -use anyhow::Error; -use std::mem; -use unicode_normalization::UnicodeNormalization; -use zeroize::Zeroizing; -use crate::crypto::{gen_random_bytes, sha256_first_byte}; +#[cfg(feature = "rand")] +use crate::crypto::gen_random_bytes; +use crate::crypto::sha256_first_byte; use crate::error::ErrorKind; use crate::language::Language; use crate::mnemonic_type::MnemonicType; use crate::util::{checksum, BitWriter, IterExt}; +use anyhow::Error; +use std::fmt; +use std::mem; +use unicode_normalization::UnicodeNormalization; +use zeroize::Zeroizing; /// The primary type in this crate, most tasks require creating or using one. /// @@ -61,6 +63,7 @@ impl Mnemonic { /// /// [Mnemonic]: ./mnemonic/struct.Mnemonic.html /// [Mnemonic::phrase()]: ./mnemonic/struct.Mnemonic.html#method.phrase + #[cfg(feature = "rand")] pub fn new(mtype: MnemonicType, lang: Language) -> Mnemonic { let entropy = gen_random_bytes(mtype.entropy_bits() / 8); @@ -107,12 +110,14 @@ impl Mnemonic { // // Given the entropy is of correct size, this ought to give us the correct word // count. - let phrase = Zeroizing::new(entropy - .iter() - .chain(Some(&checksum_byte)) - .bits() - .map(|bits| wordlist.get_word(bits)) - .join(" ")); + let phrase = Zeroizing::new( + entropy + .iter() + .chain(Some(&checksum_byte)) + .bits() + .map(|bits| wordlist.get_word(bits)) + .join(" "), + ); Mnemonic { phrase, @@ -139,10 +144,12 @@ impl Mnemonic { /// /// [Mnemonic]: ../mnemonic/struct.Mnemonic.html pub fn from_phrase(phrase: &str, lang: Language) -> Result { - let phrase = Zeroizing::new(phrase - .split_whitespace() - .map(|w| w.nfkd()) - .join::(" ")); + let phrase = Zeroizing::new( + phrase + .split_whitespace() + .map(|w| w.nfkd()) + .join::(" "), + ); // this also validates the checksum and phrase length before returning the entropy so we // can store it. We don't use the validate function here to avoid having a public API that