commit 9b77514fef80776ac82428e45ba32cc8f9d82d23
parent de060e454ab90fd5ee0cfe3d1287abe2c34e81ef
Author: Yuval Langer <yuval.langer@gmail.com>
Date: Wed, 17 Oct 2018 12:34:07 +0300
Modernize trait for generating random words and update tests
Diffstat:
5 files changed, 86 insertions(+), 38 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "diceware"
-version = "0.1.0"
+version = "0.2.0"
authors = ["Yuval Langer <yuval.langer@gmail.com>"]
build = "build.rs"
license = "AGPL-3.0"
@@ -9,5 +9,5 @@ description = ""
[dependencies]
-rand = "0.3"
-getopts = "0.2"
+rand = "^0.5"
+getopts = "^0.2"
diff --git a/README.md b/README.md
@@ -32,7 +32,7 @@ Inspired by the great passphrase generating solution [Diceware][diceware] invent
[rustlang]: <http://rust-lang.org>
-[xkcd-936]: <https://xkcd.com/936/>
+[xkcd-936]: <https://www.explainxkcd.com/wiki/index.php/936>
[minilock]: <http://minilock.io>
[minilock-github]: <https://github.com/kaepora/miniLock/>
diff --git a/src/diceware.rs b/src/diceware.rs
@@ -4,13 +4,13 @@ use std::fmt;
include!(concat!(env!("OUT_DIR"), "/diceware.rs"));
-#[derive(Debug,Clone,Eq,PartialEq)]
+#[derive(Debug, Clone, Eq, PartialEq, Copy)]
pub struct BealeWord(&'static str);
-#[derive(Debug,Clone,Eq,PartialEq)]
+#[derive(Debug, Clone, Eq, PartialEq, Copy)]
pub struct ReinholdWord(&'static str);
-#[derive(Debug,Clone,Eq,PartialEq)]
+#[derive(Debug, Clone, Eq, PartialEq, Copy)]
pub struct MiniLockWord(&'static str);
impl BealeWord {
@@ -58,44 +58,68 @@ impl MiniLockWord {
}
}
+impl rand::distributions::Distribution<BealeWord> for rand::distributions::Standard {
+ fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> BealeWord {
+ *rng.choose(&BEALE_WORDLIST).unwrap()
+ }
+}
+
+/*
impl rand::Rand for BealeWord {
fn rand<R: rand::Rng>(rng: &mut R) -> BealeWord {
rng.choose(&BEALE_WORDLIST).unwrap().clone()
}
}
+*/
impl fmt::Display for BealeWord {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
- &BealeWord(w) => write!(f, "{}", w)
+ &BealeWord(w) => write!(f, "{}", w),
}
}
}
+impl rand::distributions::Distribution<ReinholdWord> for rand::distributions::Standard {
+ fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> ReinholdWord {
+ *rng.choose(&REINHOLD_WORDLIST).unwrap()
+ }
+}
+
+/*
impl rand::Rand for ReinholdWord {
fn rand<R: rand::Rng>(rng: &mut R) -> ReinholdWord {
rng.choose(&REINHOLD_WORDLIST).unwrap().clone()
}
}
+*/
impl fmt::Display for ReinholdWord {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
- &ReinholdWord(w) => write!(f, "{}", w)
+ &ReinholdWord(w) => write!(f, "{}", w),
}
}
}
+impl rand::distributions::Distribution<MiniLockWord> for rand::distributions::Standard {
+ fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> MiniLockWord {
+ *rng.choose(&MINILOCK_WORDLIST).unwrap()
+ }
+}
+
+/*
impl rand::Rand for MiniLockWord {
fn rand<R: rand::Rng>(rng: &mut R) -> MiniLockWord {
rng.choose(&MINILOCK_WORDLIST).unwrap().clone()
}
}
+*/
impl fmt::Display for MiniLockWord {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
- &MiniLockWord(w) => write!(f, "{}", w)
+ &MiniLockWord(w) => write!(f, "{}", w),
}
}
}
diff --git a/src/main.rs b/src/main.rs
@@ -3,16 +3,16 @@ mod diceware;
#[cfg(test)]
mod tests;
-extern crate rand;
extern crate getopts;
+extern crate rand;
-use std::env;
+//use std::env;
use getopts::Options;
use rand::Rng;
+use std::process::exit;
-use diceware::{BealeWord, ReinholdWord, MiniLockWord};
-
+//use diceware::{BealeWord, ReinholdWord, MiniLockWord};
fn make_options() -> Options {
let mut opts = Options::new();
@@ -25,23 +25,25 @@ fn make_options() -> Options {
opts
}
-
fn print_usage(program: &str, opts: Options) {
let brief = format!("Usage: {} [options]", program);
print!("{}", opts.usage(&brief));
}
-
#[cfg(not(test))]
fn main() {
- let args: Vec<String> = env::args().collect();
- let program = args[0].clone();
+ let args: Vec<String> = std::env::args().collect();
+ let program = &args[0];
let opts = make_options();
let matches = match opts.parse(&args[1..]) {
- Ok(m) => { m }
- Err(f) => { panic!(f.to_string()) }
+ Ok(m) => m,
+ Err(f) => {
+ println!("{}\n", f.to_string());
+ print_usage(&program, opts);
+ exit(-1);
+ }
};
if matches.opt_present("h") {
@@ -49,7 +51,8 @@ fn main() {
return;
};
- let word_num: u64 = matches.opt_str("n")
+ let word_num: u64 = matches
+ .opt_str("n")
.map_or(8, |n_str| n_str.parse::<u64>().ok().unwrap());
let mut rng = rand::OsRng::new().unwrap();
@@ -59,7 +62,7 @@ fn main() {
let word: diceware::ReinholdWord = rng.gen();
print!("{} ", word);
}
- println!("");
+ println!();
if matches.opt_present("entropy") {
println!("{}", diceware::ReinholdWord::entropyn(word_num))
}
@@ -71,7 +74,7 @@ fn main() {
let word: diceware::BealeWord = rng.gen();
print!("{} ", word);
}
- println!("");
+ println!();
if matches.opt_present("entropy") {
println!("{}", diceware::BealeWord::entropyn(word_num))
}
@@ -82,9 +85,8 @@ fn main() {
let word: diceware::MiniLockWord = rng.gen();
print!("{} ", word);
}
- println!("");
+ println!();
if matches.opt_present("entropy") {
println!("{}", diceware::MiniLockWord::entropyn(word_num))
}
-
}
diff --git a/src/tests.rs b/src/tests.rs
@@ -2,30 +2,52 @@ extern crate rand;
use rand::{Rng, SeedableRng, StdRng};
-use diceware::{ReinholdWord, BealeWord};
+use diceware::{BealeWord, ReinholdWord};
-fn make_beale_word() -> BealeWord {
- let seed: &[_] = &[1, 2, 3, 4];
+fn make_beale_vector() -> Vec<BealeWord> {
+ let seed: [u8; 32] = [0; 32];
let mut rng: StdRng = SeedableRng::from_seed(seed);
- let word = rng.gen();
- word
+
+ let mut beale_vector: Vec<BealeWord> = vec![];
+ for _ in 0..4 {
+ let word: BealeWord = rng.gen();
+ beale_vector.push(word);
+ }
+ beale_vector
}
-fn make_reinhold_word() -> ReinholdWord {
- let seed: &[_] = &[1, 2, 3, 4];
+fn make_reinhold_vector() -> Vec<ReinholdWord> {
+ let seed: [u8; 32] = [0; 32];
let mut rng: StdRng = SeedableRng::from_seed(seed);
- let word = rng.gen();
- word
+
+ let mut reinhold_vector: Vec<ReinholdWord> = vec![];
+ for _ in 0..4 {
+ let word: ReinholdWord = rng.gen();
+ reinhold_vector.push(word);
+ }
+ reinhold_vector
}
#[test]
fn beale_rng_test() {
- let rand_word = make_beale_word();
- assert_eq!(rand_word, BealeWord::new("ladder"))
+ let wanted: Vec<BealeWord> = vec!["dr", "raced", "pvc", "moon"]
+ .into_iter()
+ .map(BealeWord::new)
+ .collect();
+
+ let got = make_beale_vector();
+
+ assert_eq!(got, wanted);
}
#[test]
fn reinhold_rng_test() {
- let rand_word = make_reinhold_word();
- assert_eq!(rand_word, ReinholdWord::new("ks"))
+ let wanted: Vec<ReinholdWord> = vec!["douse", "qo", "prune", "moan"]
+ .into_iter()
+ .map(ReinholdWord::new)
+ .collect();
+
+ let got = make_reinhold_vector();
+
+ assert_eq!(got, wanted);
}