rusty-diceware

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

commit b0211637e8a9585f8264fd8b1c7ee1a689c685dd
parent 0d8d0e9112c6e9771eadb0b0479dfe0085d7e40b
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Thu, 13 Oct 2022 17:44:45 +0300

Move wordlists out into their own package.

Diffstat:
MCargo.lock | 5+++++
MCargo.toml | 5+++++
Dbuild.rs | 128-------------------------------------------------------------------------------
Adiceware_wordlists/Cargo.toml | 13+++++++++++++
Adiceware_wordlists/build.rs | 128+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adiceware_wordlists/src/lib.rs | 1+
Rbin/wordlists/beale.wordlist.asc -> diceware_wordlists/wordlists/beale.wordlist.asc | 0
Rbin/wordlists/diceware.wordlist.asc -> diceware_wordlists/wordlists/diceware.wordlist.asc | 0
Rbin/wordlists/phrase.js -> diceware_wordlists/wordlists/phrase.js | 0
Rbin/wordlists/www.eff.org/files/2016/07/18/eff_large_wordlist.txt -> diceware_wordlists/wordlists/www.eff.org/files/2016/07/18/eff_large_wordlist.txt | 0
Rbin/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_1.txt -> diceware_wordlists/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_1.txt | 0
Rbin/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_2_0.txt -> diceware_wordlists/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_2_0.txt | 0
Msrc/main.rs | 18+++++++-----------
13 files changed, 159 insertions(+), 139 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -12,11 +12,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "diceware" version = "0.5.4" dependencies = [ + "diceware_wordlists", "getopts", "rand", ] [[package]] +name = "diceware_wordlists" +version = "0.5.4" + +[[package]] name = "getopts" version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml @@ -1,3 +1,7 @@ +[workspace] + +members = ["diceware_wordlists"] + [package] name = "diceware" version = "0.5.4" @@ -13,3 +17,4 @@ edition = "2021" [dependencies] getopts = "^0.2" rand = "^0.8" +diceware_wordlists = { path = "diceware_wordlists" } diff --git a/build.rs b/build.rs @@ -1,128 +0,0 @@ -use std::env; -use std::fs::File; -use std::io::Read; -use std::io::Write; -use std::path::Path; -use std::string::String; - -fn make_diceware_wordlist(array_name: String) -> impl Fn(&str) -> String { - move |contents: &str| { - // 7776 words = 6*6*6*6*6; five 6 faced dice throws. - format!("pub(crate) static {}: [&str; 7776] = [\n", array_name) - + &contents - .split('\n') - .skip(2) - .take(6 * 6 * 6 * 6 * 6) - .map(|s| s.split_once('\t').unwrap().1) - .map(|s| { - s.chars() - .map(|c| { - if c == '"' { - "\\\"".to_owned() - } else { - c.to_string() - } - }) - .collect::<String>() - }) - .map(|s| format!(" \"{}\",\n", s)) - .collect::<String>() - + "];\n\n" - } -} - -fn make_minilock_wordlist(contents: &str) -> std::string::String { - // 58110 words in the MiniLock wordlist. - "pub(crate) static MINILOCK_WORDLIST: [&str; 58110] = [\n".to_owned() - + &contents[1023..543718] - .split(',') - .map(|x| format!(" \"{}\",\n", x)) - .collect::<String>() - + "];\n" -} - -fn make_eff_long_wordlist(contents: &str) -> String { - // 7776 words = 6*6*6*6*6; five 6 faced dice throws. - "pub(crate) static EFF_LONG_WORDLIST: [&str; 7776] = [\n".to_owned() - + &contents - .split('\n') - .take(6 * 6 * 6 * 6 * 6) - .map(|x| x.split('\t').nth(1).unwrap()) - .map(|x| format!(" \"{}\",\n", x)) - .collect::<String>() - + "];\n" -} - -fn make_eff_short_wordlist(array_name: String) -> impl Fn(&str) -> String { - move |contents: &str| { - // 1296 words = 6*6*6*6; five 6 faced dice throws. - format!("pub(crate) static {}: [&str; 1296] = [\n", array_name) - + &contents - .split('\n') - .take(6 * 6 * 6 * 6) - .map(|x| x.split('\t').nth(1).unwrap()) - .map(|x| format!(" \"{}\",\n", x)) - .collect::<String>() - + "];\n" - } -} - -fn build_wordlist( - wordlist_file_path: &str, - destination_file: &mut File, - make_wordlist: &dyn Fn(&str) -> String, -) { - let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - println!("{}", manifest_dir); - let wordlist_file_path = Path::new(&manifest_dir).join(wordlist_file_path); - - let mut wordlist_file = File::open(&wordlist_file_path).unwrap(); - - let mut wordlist_string = String::new(); - - wordlist_file.read_to_string(&mut wordlist_string).unwrap(); - - destination_file - .write_all(make_wordlist(&wordlist_string).as_bytes()) - .unwrap(); -} - -fn main() { - let out_dir = env::var("OUT_DIR").unwrap(); - let dest_path = Path::new(&out_dir).join("diceware.rs"); - let mut destination_file = File::create(&dest_path).unwrap(); - - build_wordlist( - "bin/wordlists/beale.wordlist.asc", - &mut destination_file, - &make_diceware_wordlist("BEALE_WORDLIST".to_string()), - ); - build_wordlist( - "bin/wordlists/diceware.wordlist.asc", - &mut destination_file, - &make_diceware_wordlist("REINHOLD_WORDLIST".to_string()), - ); - build_wordlist( - "bin/wordlists/phrase.js", - &mut destination_file, - &make_minilock_wordlist, - ); - build_wordlist( - // They call it "EFF's Long Wordlist" on https://www.eff.org/dice, not "EFF's Large - // Wordlist", although the file name is different, so I will call it "long" rather than - // "large". - "bin/wordlists/www.eff.org/files/2016/07/18/eff_large_wordlist.txt", - &mut destination_file, - &make_eff_long_wordlist, - ); - build_wordlist( - "bin/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_1.txt", - &mut destination_file, - &make_eff_short_wordlist("EFF_SHORT_WORDLIST_1".to_string()), - ); - build_wordlist( - "bin/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_2_0.txt", - &mut destination_file, - &make_eff_short_wordlist("EFF_SHORT_WORDLIST_2_0".to_string()), - ); -} diff --git a/diceware_wordlists/Cargo.toml b/diceware_wordlists/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "diceware_wordlists" +version = "0.5.4" +authors = ["Yuval Langer <yuvallangerontheroad@gmail.com>"] +license = "AGPL-3.0" +repository = "https://gitlab.com/yuvallanger/rusty-diceware" +description = "A command line diceware, with or without dice." +keywords = ["diceware", "password", "passphrase", "generator"] +readme = "README.md" +edition = "2021" + + +[dependencies] diff --git a/diceware_wordlists/build.rs b/diceware_wordlists/build.rs @@ -0,0 +1,128 @@ +use std::env; +use std::fs::File; +use std::io::Read; +use std::io::Write; +use std::path::Path; +use std::string::String; + +fn make_diceware_wordlist(array_name: String) -> impl Fn(&str) -> String { + move |contents: &str| { + // 7776 words = 6*6*6*6*6; five 6 faced dice throws. + format!("pub static {}: [&str; 7776] = [\n", array_name) + + &contents + .split('\n') + .skip(2) + .take(6 * 6 * 6 * 6 * 6) + .map(|s| s.split_once('\t').unwrap().1) + .map(|s| { + s.chars() + .map(|c| { + if c == '"' { + "\\\"".to_owned() + } else { + c.to_string() + } + }) + .collect::<String>() + }) + .map(|s| format!(" \"{}\",\n", s)) + .collect::<String>() + + "];\n\n" + } +} + +fn make_minilock_wordlist(contents: &str) -> std::string::String { + // 58110 words in the MiniLock wordlist. + "pub static MINILOCK_WORDLIST: [&str; 58110] = [\n".to_owned() + + &contents[1023..543718] + .split(',') + .map(|x| format!(" \"{}\",\n", x)) + .collect::<String>() + + "];\n" +} + +fn make_eff_long_wordlist(contents: &str) -> String { + // 7776 words = 6*6*6*6*6; five 6 faced dice throws. + "pub static EFF_LONG_WORDLIST: [&str; 7776] = [\n".to_owned() + + &contents + .split('\n') + .take(6 * 6 * 6 * 6 * 6) + .map(|x| x.split('\t').nth(1).unwrap()) + .map(|x| format!(" \"{}\",\n", x)) + .collect::<String>() + + "];\n" +} + +fn make_eff_short_wordlist(array_name: String) -> impl Fn(&str) -> String { + move |contents: &str| { + // 1296 words = 6*6*6*6; five 6 faced dice throws. + format!("pub static {}: [&str; 1296] = [\n", array_name) + + &contents + .split('\n') + .take(6 * 6 * 6 * 6) + .map(|x| x.split('\t').nth(1).unwrap()) + .map(|x| format!(" \"{}\",\n", x)) + .collect::<String>() + + "];\n" + } +} + +fn build_wordlist( + wordlist_file_path: &str, + destination_file: &mut File, + make_wordlist: &dyn Fn(&str) -> String, +) { + let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + println!("{}", manifest_dir); + let wordlist_file_path = Path::new(&manifest_dir).join(wordlist_file_path); + + let mut wordlist_file = File::open(&wordlist_file_path).unwrap(); + + let mut wordlist_string = String::new(); + + wordlist_file.read_to_string(&mut wordlist_string).unwrap(); + + destination_file + .write_all(make_wordlist(&wordlist_string).as_bytes()) + .unwrap(); +} + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + let dest_path = Path::new(&out_dir).join("diceware.rs"); + let mut destination_file = File::create(&dest_path).unwrap(); + + build_wordlist( + "wordlists/beale.wordlist.asc", + &mut destination_file, + &make_diceware_wordlist("BEALE_WORDLIST".to_string()), + ); + build_wordlist( + "wordlists/diceware.wordlist.asc", + &mut destination_file, + &make_diceware_wordlist("REINHOLD_WORDLIST".to_string()), + ); + build_wordlist( + "wordlists/phrase.js", + &mut destination_file, + &make_minilock_wordlist, + ); + build_wordlist( + // They call it "EFF's Long Wordlist" on https://www.eff.org/dice, not "EFF's Large + // Wordlist", although the file name is different, so I will call it "long" rather than + // "large". + "wordlists/www.eff.org/files/2016/07/18/eff_large_wordlist.txt", + &mut destination_file, + &make_eff_long_wordlist, + ); + build_wordlist( + "wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_1.txt", + &mut destination_file, + &make_eff_short_wordlist("EFF_SHORT_WORDLIST_1".to_string()), + ); + build_wordlist( + "wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_2_0.txt", + &mut destination_file, + &make_eff_short_wordlist("EFF_SHORT_WORDLIST_2_0".to_string()), + ); +} diff --git a/diceware_wordlists/src/lib.rs b/diceware_wordlists/src/lib.rs @@ -0,0 +1 @@ +include!(concat!(env!("OUT_DIR"), "/diceware.rs")); diff --git a/bin/wordlists/beale.wordlist.asc b/diceware_wordlists/wordlists/beale.wordlist.asc diff --git a/bin/wordlists/diceware.wordlist.asc b/diceware_wordlists/wordlists/diceware.wordlist.asc diff --git a/bin/wordlists/phrase.js b/diceware_wordlists/wordlists/phrase.js diff --git a/bin/wordlists/www.eff.org/files/2016/07/18/eff_large_wordlist.txt b/diceware_wordlists/wordlists/www.eff.org/files/2016/07/18/eff_large_wordlist.txt diff --git a/bin/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_1.txt b/diceware_wordlists/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_1.txt diff --git a/bin/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_2_0.txt b/diceware_wordlists/wordlists/www.eff.org/files/2016/09/08/eff_short_wordlist_2_0.txt diff --git a/src/main.rs b/src/main.rs @@ -158,10 +158,6 @@ fn read_rolls() -> Vec<Vec<u8>> { rolls } -mod wordlists { - include!(concat!(env!("OUT_DIR"), "/diceware.rs")); -} - fn entropy(wordlist: &[&str]) -> f64 { (wordlist.len() as f64).log2() } @@ -274,12 +270,12 @@ fn main() { }; } else { let wordlist = match wordlist_name.as_ref() { - "efflong" => wordlists::EFF_LONG_WORDLIST.as_ref(), - "reinhold" => wordlists::REINHOLD_WORDLIST.as_ref(), - "beale" => wordlists::BEALE_WORDLIST.as_ref(), - "minilock" => wordlists::MINILOCK_WORDLIST.as_ref(), - "effshort1" => wordlists::EFF_SHORT_WORDLIST_1.as_ref(), - "effshort2" => wordlists::EFF_SHORT_WORDLIST_2_0.as_ref(), + "efflong" => diceware_wordlists::EFF_LONG_WORDLIST.as_ref(), + "reinhold" => diceware_wordlists::REINHOLD_WORDLIST.as_ref(), + "beale" => diceware_wordlists::BEALE_WORDLIST.as_ref(), + "minilock" => diceware_wordlists::MINILOCK_WORDLIST.as_ref(), + "effshort1" => diceware_wordlists::EFF_SHORT_WORDLIST_1.as_ref(), + "effshort2" => diceware_wordlists::EFF_SHORT_WORDLIST_2_0.as_ref(), _ => unknown_wordlist(&wordlist_name), }; @@ -308,7 +304,7 @@ macro_rules! create_test { ( $wordlist_name: ident, $test_name: ident, $expected: expr ) => { #[test] fn $test_name() { - use crate::wordlists::$wordlist_name; + use diceware_wordlists::$wordlist_name; use rand::prelude::SeedableRng; use rand::prelude::StdRng;