Wordlist from crate (#5331)

This commit is contained in:
Tomasz Drwięga
2017-04-01 08:26:44 +02:00
committed by Marek Kotewicz
parent 1987dad527
commit 83fea78d38
5 changed files with 27 additions and 7827 deletions

View File

@@ -5,8 +5,8 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
log = "0.3"
libc = "0.2.11"
rand = "0.3.14"
libc = "0.2"
rand = "0.3"
ethkey = { path = "../ethkey" }
serde = "0.9"
serde_json = "0.9"
@@ -16,13 +16,13 @@ rust-crypto = "0.2.36"
tiny-keccak = "1.0"
docopt = { version = "0.7", optional = true }
time = "0.1.34"
lazy_static = "0.2"
itertools = "0.5"
parking_lot = "0.4"
ethcrypto = { path = "../ethcrypto" }
ethcore-util = { path = "../util" }
smallvec = "0.3.1"
ethcore-devtools = { path = "../devtools" }
parity-wordlist = "1.0"
[features]
cli = ["docopt"]

File diff suppressed because it is too large Load Diff

View File

@@ -16,32 +16,29 @@
//! Ethereum key-management.
#![warn(missing_docs)]
extern crate libc;
extern crate crypto as rcrypto;
extern crate itertools;
extern crate smallvec;
extern crate libc;
extern crate parking_lot;
extern crate rand;
extern crate time;
extern crate rustc_serialize;
extern crate serde;
extern crate serde_json;
extern crate rustc_serialize;
extern crate crypto as rcrypto;
extern crate smallvec;
extern crate time;
extern crate tiny_keccak;
extern crate parking_lot;
extern crate ethcore_devtools as devtools;
// reexport it nicely
extern crate ethkey as _ethkey;
extern crate ethcrypto as crypto;
extern crate ethcore_devtools as devtools;
extern crate ethcore_util as util;
extern crate ethcrypto as crypto;
extern crate ethkey as _ethkey;
extern crate parity_wordlist;
#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
pub mod dir;
@@ -67,4 +64,5 @@ pub use self::secret_store::{
SecretVaultRef, StoreAccountRef, SimpleSecretStore, SecretStore,
Derivation, IndexDerivation,
};
pub use self::random::{random_phrase, random_string};
pub use self::random::random_string;
pub use self::parity_wordlist::random_phrase;

View File

@@ -15,7 +15,6 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use rand::{Rng, OsRng};
use itertools::Itertools;
pub trait Random {
fn random() -> Self where Self: Sized;
@@ -39,41 +38,9 @@ impl Random for [u8; 32] {
}
}
/// Generate a string which is a random phrase of a number of lowercase words.
///
/// `words` is the number of words, chosen from a dictionary of 7,530. An value of
/// 12 gives 155 bits of entropy (almost saturating address space); 20 gives 258 bits
/// which is enough to saturate 32-byte key space
pub fn random_phrase(words: usize) -> String {
lazy_static! {
static ref WORDS: Vec<String> = String::from_utf8_lossy(include_bytes!("../res/wordlist.txt"))
.lines()
.map(|s| s.to_owned())
.collect();
}
let mut rng = OsRng::new().expect("Not able to operate without random source.");
(0..words).map(|_| rng.choose(&WORDS).unwrap()).join(" ")
}
/// Generate a random string of given length.
pub fn random_string(length: usize) -> String {
let mut rng = OsRng::new().expect("Not able to operate without random source.");
rng.gen_ascii_chars().take(length).collect()
}
#[cfg(test)]
mod tests {
use super::random_phrase;
#[test]
fn should_produce_right_number_of_words() {
let p = random_phrase(10);
assert_eq!(p.split(" ").count(), 10);
}
#[test]
fn should_not_include_carriage_return() {
let p = random_phrase(10);
assert!(!p.contains('\r'), "Carriage return should be trimmed.");
}
}