commit f48e7c4674ccf20da869d4788755b263da404a34
parent cc87cf01204f3674d9d20051a87bdd1253f8d1e8
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date: Thu, 8 Sep 2022 03:11:44 +0300
Add test for MiniLock wordlist.
Also:
- Move some code from main function to its own function in `src/lib.rs`.
- Move predefined wordlists to their own `diceware::wordlists` module.
- Minor inconsequential stuff that looks better, maybe(?).
Diffstat:
3 files changed, 41 insertions(+), 26 deletions(-)
diff --git a/src/bin/diceware.rs b/src/bin/diceware.rs
@@ -1,13 +1,17 @@
extern crate getopts;
extern crate rand;
-use std::fs::File;
-use std::io::Read;
use std::process::exit;
use getopts::Options;
use rand::thread_rng;
+use diceware::load_wordlist_file;
+use diceware::print_words;
+use diceware::wordlists::BEALE_WORDLIST;
+use diceware::wordlists::MINILOCK_WORDLIST;
+use diceware::wordlists::REINHOLD_WORDLIST;
+
fn make_options() -> Options {
let mut opts = Options::new();
opts.optflag("h", "help", "this help message");
@@ -66,20 +70,7 @@ fn main() {
if word_num != 0 {
if let Some(wordlist_filepath) = matches.opt_str("f") {
- let mut wordlist_file = match File::open(&wordlist_filepath) {
- Ok(ok) => ok,
- Err(err) => panic!(
- "Unable to open file: {}; due to error: {}",
- wordlist_filepath, err
- ),
- };
- let mut wordlist_string = String::new();
- if let Err(err) = wordlist_file.read_to_string(&mut wordlist_string) {
- panic!(
- "Unable to read file: {}; due to error: {}",
- wordlist_filepath, err
- )
- }
+ let wordlist_string = load_wordlist_file(&wordlist_filepath);
let wordlist = wordlist_string
.split('\n')
@@ -87,7 +78,7 @@ fn main() {
.filter(|x| x != &"")
.collect();
- diceware::print_words(
+ print_words(
wordlist,
&word_num,
&delimiter,
@@ -95,24 +86,24 @@ fn main() {
&mut rng,
);
} else if matches.opt_present("reinhold") {
- diceware::print_words(
- diceware::REINHOLD_WORDLIST.to_vec(),
+ print_words(
+ REINHOLD_WORDLIST.to_vec(),
&word_num,
&delimiter,
&is_entropy_printed,
&mut rng,
);
} else if matches.opt_present("beale") {
- diceware::print_words(
- diceware::BEALE_WORDLIST.to_vec(),
+ print_words(
+ BEALE_WORDLIST.to_vec(),
&word_num,
&delimiter,
&is_entropy_printed,
&mut rng,
);
} else {
- diceware::print_words(
- diceware::MINILOCK_WORDLIST.to_vec(),
+ print_words(
+ MINILOCK_WORDLIST.to_vec(),
&word_num,
&delimiter,
&is_entropy_printed,
diff --git a/src/lib.rs b/src/lib.rs
@@ -1,9 +1,14 @@
extern crate rand;
+use std::fs::File;
+use std::io::Read;
+
use rand::rngs::ThreadRng;
use rand::seq::SliceRandom;
-include!(concat!(env!("OUT_DIR"), "/diceware.rs"));
+pub mod wordlists {
+ include!(concat!(env!("OUT_DIR"), "/diceware.rs"));
+}
fn entropy(wordlist: &[&str]) -> f64 {
(wordlist.len() as f64).log2()
@@ -32,3 +37,15 @@ pub fn print_words(
println!("{}", entropyn(&wordlist, *word_num))
}
}
+
+pub fn load_wordlist_file(filepath: &str) -> String {
+ let mut wordlist_file = match File::open(&filepath) {
+ Ok(ok) => ok,
+ Err(err) => panic!("Unable to open file: {}; due to error: {}", filepath, err),
+ };
+ let mut wordlist_string = String::new();
+ if let Err(err) = wordlist_file.read_to_string(&mut wordlist_string) {
+ panic!("Unable to read file: {}; due to error: {}", filepath, err)
+ }
+ wordlist_string
+}
diff --git a/tests/tests.rs b/tests/tests.rs
@@ -4,8 +4,9 @@ use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
-use diceware::BEALE_WORDLIST;
-use diceware::REINHOLD_WORDLIST;
+use diceware::wordlists::BEALE_WORDLIST;
+use diceware::wordlists::MINILOCK_WORDLIST;
+use diceware::wordlists::REINHOLD_WORDLIST;
macro_rules! create_test {
( $wordlist_name: path, $test_name: ident, $expected: expr ) => {
@@ -42,3 +43,9 @@ create_test!(
reinhold_rng_test,
vec!["india", "gamma", "bcd", "theme"]
);
+
+create_test!(
+ MINILOCK_WORDLIST,
+ minilock_rng_test,
+ vec!["hoed", "femininity", "bedsit", "stabbings"]
+);