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

13
Cargo.lock generated
View File

@ -795,9 +795,9 @@ dependencies = [
"ethcrypto 0.1.0", "ethcrypto 0.1.0",
"ethkey 0.2.0", "ethkey 0.2.0",
"itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-wordlist 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1716,6 +1716,16 @@ dependencies = [
"target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "parity-wordlist"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.4.0" version = "0.4.0"
@ -2744,6 +2754,7 @@ dependencies = [
"checksum parity-dapps-glue 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e1d06f6ee0fda786df3784a96ee3f0629f529b91cbfb7d142f6410e6bcd1ce2c" "checksum parity-dapps-glue 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e1d06f6ee0fda786df3784a96ee3f0629f529b91cbfb7d142f6410e6bcd1ce2c"
"checksum parity-tokio-ipc 0.1.0 (git+https://github.com/nikvolf/parity-tokio-ipc)" = "<none>" "checksum parity-tokio-ipc 0.1.0 (git+https://github.com/nikvolf/parity-tokio-ipc)" = "<none>"
"checksum parity-ui-precompiled 1.4.0 (git+https://github.com/paritytech/js-precompiled.git)" = "<none>" "checksum parity-ui-precompiled 1.4.0 (git+https://github.com/paritytech/js-precompiled.git)" = "<none>"
"checksum parity-wordlist 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07779ab11d958acbee30fcf644c99d3fae132d8fcb41282a25e1ee284097bdd2"
"checksum parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aebb68eebde2c99f89592d925288600fde220177e46b5c9a91ca218d245aeedf" "checksum parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aebb68eebde2c99f89592d925288600fde220177e46b5c9a91ca218d245aeedf"
"checksum parking_lot_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fb1b97670a2ffadce7c397fb80a3d687c4f3060140b885621ef1653d0e5d5068" "checksum parking_lot_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fb1b97670a2ffadce7c397fb80a3d687c4f3060140b885621ef1653d0e5d5068"
"checksum phf 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)" = "447d9d45f2e0b4a9b532e808365abf18fc211be6ca217202fcd45236ef12f026" "checksum phf 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)" = "447d9d45f2e0b4a9b532e808365abf18fc211be6ca217202fcd45236ef12f026"

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -16,32 +16,29 @@
//! Ethereum key-management. //! Ethereum key-management.
#![warn(missing_docs)] #![warn(missing_docs)]
extern crate libc; extern crate crypto as rcrypto;
extern crate itertools; extern crate itertools;
extern crate smallvec; extern crate libc;
extern crate parking_lot;
extern crate rand; extern crate rand;
extern crate time; extern crate rustc_serialize;
extern crate serde; extern crate serde;
extern crate serde_json; extern crate serde_json;
extern crate rustc_serialize; extern crate smallvec;
extern crate crypto as rcrypto; extern crate time;
extern crate tiny_keccak; extern crate tiny_keccak;
extern crate parking_lot;
extern crate ethcore_devtools as devtools;
// reexport it nicely extern crate ethcore_devtools as devtools;
extern crate ethkey as _ethkey;
extern crate ethcrypto as crypto;
extern crate ethcore_util as util; extern crate ethcore_util as util;
extern crate ethcrypto as crypto;
extern crate ethkey as _ethkey;
extern crate parity_wordlist;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use] #[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive; extern crate serde_derive;
pub mod dir; pub mod dir;
@ -67,4 +64,5 @@ pub use self::secret_store::{
SecretVaultRef, StoreAccountRef, SimpleSecretStore, SecretStore, SecretVaultRef, StoreAccountRef, SimpleSecretStore, SecretStore,
Derivation, IndexDerivation, 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/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use rand::{Rng, OsRng}; use rand::{Rng, OsRng};
use itertools::Itertools;
pub trait Random { pub trait Random {
fn random() -> Self where Self: Sized; 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. /// Generate a random string of given length.
pub fn random_string(length: usize) -> String { pub fn random_string(length: usize) -> String {
let mut rng = OsRng::new().expect("Not able to operate without random source."); let mut rng = OsRng::new().expect("Not able to operate without random source.");
rng.gen_ascii_chars().take(length).collect() 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.");
}
}