rusty-diceware

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit b8f26117383388da1ac2605fa83416c6c811cd59
parent ea43bd29e2c9ca0541acce024fc3181e998bbd7f
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Wed,  7 Sep 2022 21:42:14 +0300

Remove unnecessary struct definitions words of different wordlists.

Use simple `&str` instead.

Diffstat:
Mbuild.rs | 32++++++++++++++++----------------
Msrc/bin/diceware.rs | 30+++++++++++++++---------------
Msrc/lib.rs | 60++++++++++--------------------------------------------------
Mtests/tests.rs | 21++++++++++-----------
4 files changed, 51 insertions(+), 92 deletions(-)

diff --git a/build.rs b/build.rs @@ -14,15 +14,15 @@ fn make_wordlist(contents: &str) -> Vec<&str> { .collect() } -fn make_beale_struct(contents: &str) -> std::string::String { +fn make_beale_wordlist(contents: &str) -> std::string::String { let wordlist = make_wordlist(contents); let mut output = std::string::String::new(); // 7776 words = 6*6*6*6*6; five 6 faced dice throws. - output.push_str("static BEALE_WORDLIST: [BealeWord; 7776] = [\n"); + output.push_str("pub static BEALE_WORDLIST: [&str; 7776] = [\n"); for word in wordlist { - output.push_str(" BealeWord(\""); + output.push_str(" \""); for c in word.chars() { if c == '"' { output.push_str("\\\""); @@ -30,21 +30,21 @@ fn make_beale_struct(contents: &str) -> std::string::String { output.push(c); } } - output.push_str("\"),\n"); + output.push_str("\",\n"); } - output.push_str("];\n"); + output.push_str("];\n\n"); output } -fn make_reinhold_struct(contents: &str) -> std::string::String { +fn make_reinhold_wordlist(contents: &str) -> std::string::String { let wordlist = make_wordlist(contents); let mut output = std::string::String::new(); // 7776 words = 6*6*6*6*6; five 6 faced dice throws. - output.push_str("static REINHOLD_WORDLIST: [ReinholdWord; 7776] = [\n"); + output.push_str("pub static REINHOLD_WORDLIST: [&str; 7776] = [\n"); for word in wordlist { - output.push_str(" ReinholdWord(\""); + output.push_str(" \""); for c in word.chars() { if c == '"' { output.push_str("\\\""); @@ -52,21 +52,21 @@ fn make_reinhold_struct(contents: &str) -> std::string::String { output.push(c); } } - output.push_str("\"),\n"); + output.push_str("\",\n"); } output.push_str("];\n"); output } -fn make_minilock_struct(contents: &str) -> std::string::String { +fn make_minilock_wordlist(contents: &str) -> std::string::String { let wordlist: Vec<&str> = (&contents[1023..543718]).split(',').collect(); let mut output = std::string::String::new(); // 58110 words in the MiniLock wordlist. - output.push_str("static MINILOCK_WORDLIST: [MiniLockWord; 58110] = [\n"); + output.push_str("pub static MINILOCK_WORDLIST: [&str; 58110] = [\n"); for word in wordlist { - output.push_str(" MiniLockWord(\""); + output.push_str(" \""); for c in word.chars() { if c == '"' { panic!("Not supposed to have any double quotes."); @@ -74,7 +74,7 @@ fn make_minilock_struct(contents: &str) -> std::string::String { output.push(c); } } - output.push_str("\"),\n"); + output.push_str("\",\n"); } output.push_str("];\n"); output @@ -108,12 +108,12 @@ fn main() { build_wordlist( "beale.wordlist.asc", &mut destination_file, - &make_beale_struct, + &make_beale_wordlist, ); build_wordlist( "diceware.wordlist.asc", &mut destination_file, - &make_reinhold_struct, + &make_reinhold_wordlist, ); - build_wordlist("phrase.js", &mut destination_file, &make_minilock_struct); + build_wordlist("phrase.js", &mut destination_file, &make_minilock_wordlist); } diff --git a/src/bin/diceware.rs b/src/bin/diceware.rs @@ -1,9 +1,10 @@ extern crate getopts; extern crate rand; +use std::process::exit; + use getopts::Options; use rand::thread_rng; -use std::process::exit; fn make_options() -> Options { let mut opts = Options::new(); @@ -62,30 +63,29 @@ fn main() { if word_num != 0 { if matches.opt_present("reinhold") { - diceware::print_words::<diceware::ReinholdWord>( + diceware::print_words( + diceware::REINHOLD_WORDLIST.to_vec(), &word_num, &delimiter, &is_entropy_printed, &mut rng, ); - return; - } - - if matches.opt_present("beale") { - diceware::print_words::<diceware::BealeWord>( + } else if matches.opt_present("beale") { + diceware::print_words( + diceware::BEALE_WORDLIST.to_vec(), + &word_num, + &delimiter, + &is_entropy_printed, + &mut rng, + ); + } else { + diceware::print_words( + diceware::MINILOCK_WORDLIST.to_vec(), &word_num, &delimiter, &is_entropy_printed, &mut rng, ); - return; } - - diceware::print_words::<diceware::MiniLockWord>( - &word_num, - &delimiter, - &is_entropy_printed, - &mut rng, - ); } } diff --git a/src/lib.rs b/src/lib.rs @@ -2,73 +2,33 @@ extern crate rand; use rand::rngs::ThreadRng; use rand::seq::SliceRandom; -use rand::Rng; include!(concat!(env!("OUT_DIR"), "/diceware.rs")); -pub trait Word { - fn new(word: &'static str) -> Self; - - fn entropy() -> f64; - - fn entropyn(n: u64) -> f64 { - Self::entropy() * (n as f64) - } +fn entropy(wordlist: &[&str]) -> f64 { + (wordlist.len() as f64).log2() } -macro_rules! create_generator { - ( $gen_name:ident, $wordlist: expr ) => { - #[derive(Debug, Clone, Eq, PartialEq, Copy)] - pub struct $gen_name(&'static str); - impl Word for $gen_name { - fn new(word: &'static str) -> Self { - $gen_name(word) - } - - fn entropy() -> f64 { - ($wordlist.len() as f64).log2() - } - - fn entropyn(n: u64) -> f64 { - Self::entropy() * (n as f64) - } - } - - impl std::fmt::Display for $gen_name { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", self.0) - } - } - - impl rand::distributions::Distribution<$gen_name> for rand::distributions::Standard { - fn sample<R: rand::Rng + ?Sized>(&self, mut rng: &mut R) -> $gen_name { - *$wordlist.choose(&mut rng).unwrap() - } - } - }; +fn entropyn(wordlist: &[&str], n: u64) -> f64 { + entropy(wordlist) * (n as f64) } -create_generator!(BealeWord, BEALE_WORDLIST); -create_generator!(ReinholdWord, REINHOLD_WORDLIST); -create_generator!(MiniLockWord, MINILOCK_WORDLIST); - -pub fn print_words<T: Word + std::fmt::Display>( +pub fn print_words( + wordlist: Vec<&str>, word_num: &u64, delimiter: &char, is_entropy_printed: &bool, rng: &mut ThreadRng, -) where - rand::distributions::Standard: rand::distributions::Distribution<T>, -{ +) { for _ in 0..(word_num - 1) { - let word: T = rng.gen(); + let word = wordlist.choose(rng).unwrap(); print!("{}{}", &word, delimiter); } - let word: T = rng.gen(); + let word = wordlist.choose(rng).unwrap(); print!("{}", word); println!(); if *is_entropy_printed { - println!("{}", T::entropyn(*word_num)) + println!("{}", entropyn(&wordlist, *word_num)) } } diff --git a/tests/tests.rs b/tests/tests.rs @@ -1,29 +1,28 @@ extern crate rand; use rand::rngs::StdRng; -use rand::Rng; +use rand::seq::SliceRandom; use rand::SeedableRng; -use diceware::BealeWord; -use diceware::ReinholdWord; -use diceware::Word; +use diceware::BEALE_WORDLIST; +use diceware::REINHOLD_WORDLIST; macro_rules! create_test { - ( $gen_name: ident, $test_name: ident, $expected: expr ) => { + ( $wordlist_name: path, $test_name: ident, $expected: expr ) => { #[test] fn $test_name() { - fn make_vektor() -> Vec<$gen_name> { + fn make_vektor<'a>() -> Vec<&'a str> { let seed: [u8; 32] = [0; 32]; let mut rng: StdRng = SeedableRng::from_seed(seed); - let mut vector: Vec<$gen_name> = vec![]; + let mut vector: Vec<&str> = vec![]; for _ in 0..4 { - let word: $gen_name = rng.gen(); + let word: &str = $wordlist_name.choose(&mut rng).unwrap(); vector.push(word); } vector } - let wanted: Vec<$gen_name> = $expected.into_iter().map($gen_name::new).collect(); + let wanted: Vec<&str> = $expected.into_iter().collect(); let got = make_vektor(); @@ -33,13 +32,13 @@ macro_rules! create_test { } create_test!( - BealeWord, + BEALE_WORDLIST, beale_rng_test, vec!["io", "gavel", "beam", "time"] ); create_test!( - ReinholdWord, + REINHOLD_WORDLIST, reinhold_rng_test, vec!["india", "gamma", "bcd", "theme"] );