rusty-diceware

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

commit 76e36d7dd615358b3c8b18d430c35a515b49fdc2
parent 9fcdee12859b0200c6a6b613c0ad048e73466b81
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Sat,  3 Sep 2022 17:57:22 +0300

Add a delimiter flag to the command. Fix clippy issues. Bump version.

Diffstat:
MCargo.toml | 4++--
Mbuild.rs | 16++++++++--------
Msrc/bin/diceware.rs | 73+++++++++++++++++++++++++++++++++++++++++++++++--------------------------
3 files changed, 57 insertions(+), 36 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "diceware" -version = "0.3.0" -authors = ["Yuval Langer <yuval.langer@gmail.com>"] +version = "0.3.1" +authors = ["Yuval Langer <yuvallangerontheroad@gmail.com>"] license = "AGPL-3.0" repository = "https://gitlab.com/yuvallanger/rusty-diceware" description = "A command line diceware, sans dice." diff --git a/build.rs b/build.rs @@ -5,12 +5,12 @@ use std::io::Write; use std::path::Path; use std::string::String; -fn make_wordlist(contents: &std::string::String) -> Vec<&str> { +fn make_wordlist(contents: &str) -> Vec<&str> { contents .split('\n') .skip(2) .take(7776) - .map(|s| s.splitn(2, '\t').nth(1).unwrap()) + .map(|s| s.split_once('\t').unwrap().1) .collect() } @@ -22,7 +22,7 @@ fn make_beale_struct(wordlist: Vec<&str>) -> std::string::String { let mut output = std::string::String::new(); // 7776 words = 6*6*6*6*6; five 6 faced dice throws. - output.push_str("const BEALE_WORDLIST: [BealeWord; 7776] = [\n"); + output.push_str("static BEALE_WORDLIST: [BealeWord; 7776] = [\n"); for word in wordlist { output.push_str(" BealeWord(\""); for c in word.chars() { @@ -35,14 +35,14 @@ fn make_beale_struct(wordlist: Vec<&str>) -> std::string::String { output.push_str("\"),\n"); } output.push_str("];\n"); - return output; + output } fn make_reinhold_struct(wordlist: Vec<&str>) -> std::string::String { let mut output = std::string::String::new(); // 7776 words = 6*6*6*6*6; five 6 faced dice throws. - output.push_str("const REINHOLD_WORDLIST: [ReinholdWord; 7776] = [\n"); + output.push_str("static REINHOLD_WORDLIST: [ReinholdWord; 7776] = [\n"); for word in wordlist { output.push_str(" ReinholdWord(\""); for c in word.chars() { @@ -55,14 +55,14 @@ fn make_reinhold_struct(wordlist: Vec<&str>) -> std::string::String { output.push_str("\"),\n"); } output.push_str("];\n"); - return output; + output } fn make_minilock_struct(wordlist: Vec<&str>) -> std::string::String { let mut output = std::string::String::new(); // 58110 words in the MiniLock wordlist. - output.push_str("const MINILOCK_WORDLIST: [MiniLockWord; 58110] = [\n"); + output.push_str("static MINILOCK_WORDLIST: [MiniLockWord; 58110] = [\n"); for word in wordlist { output.push_str(" MiniLockWord(\""); for c in word.chars() { @@ -75,7 +75,7 @@ fn make_minilock_struct(wordlist: Vec<&str>) -> std::string::String { output.push_str("\"),\n"); } output.push_str("];\n"); - return output; + output } fn main() { diff --git a/src/bin/diceware.rs b/src/bin/diceware.rs @@ -18,6 +18,12 @@ fn make_options() -> Options { opts.optflag("", "beale", "use the beale wordlist"); opts.optflag("e", "entropy", "display number of entropy bits"); opts.optopt("n", "nword", "number of words in a passphrase", "NWORD"); + opts.optopt( + "d", + "delimiter", + "the delimiter character used to separate the words", + "DELIM", + ); opts } @@ -35,14 +41,14 @@ fn main() { let matches = match opts.parse(&args[1..]) { Ok(m) => m, Err(f) => { - println!("{}\n", f.to_string()); - print_usage(&program, opts); + println!("{}\n", f); + print_usage(program, opts); exit(-1); } }; if matches.opt_present("h") { - print_usage(&program, opts); + print_usage(program, opts); return; }; @@ -50,38 +56,53 @@ fn main() { .opt_str("n") .map_or(8, |n_str| n_str.parse::<u64>().ok().unwrap()); + let delimiter: char = matches + .opt_str("d") + .map_or(' ', |n_str| n_str.parse::<char>().ok().unwrap()); + let mut rng = OsRng::new().unwrap(); - if matches.opt_present("reinhold") { - for _ in 0..word_num { + if word_num != 0 { + if matches.opt_present("reinhold") { + for _ in 0..(word_num - 1) { + let word: diceware::ReinholdWord = rng.gen(); + print!("{}{}", &word, delimiter); + } let word: diceware::ReinholdWord = rng.gen(); - print!("{} ", word); - } - println!(); - if matches.opt_present("entropy") { - println!("{}", diceware::ReinholdWord::entropyn(word_num)) + print!("{}", word); + + println!(); + if matches.opt_present("entropy") { + println!("{}", diceware::ReinholdWord::entropyn(word_num)) + } + return; } - return; - } - if matches.opt_present("beale") { - for _ in 0..word_num { + if matches.opt_present("beale") { + for _ in 0..(word_num - 1) { + let word: diceware::BealeWord = rng.gen(); + print!("{}{}", &word, delimiter); + } let word: diceware::BealeWord = rng.gen(); - print!("{} ", word); + print!("{}", word); + + println!(); + if matches.opt_present("entropy") { + println!("{}", diceware::BealeWord::entropyn(word_num)) + } + return; } + + for _ in 0..(word_num - 1) { + let word: diceware::MiniLockWord = rng.gen(); + print!("{}{}", &word, delimiter); + } + let word: diceware::MiniLockWord = rng.gen(); + print!("{}", word); + println!(); if matches.opt_present("entropy") { - println!("{}", diceware::BealeWord::entropyn(word_num)) + println!("{}", diceware::MiniLockWord::entropyn(word_num)) } - return; - } - - for _ in 0..word_num { - let word: diceware::MiniLockWord = rng.gen(); - print!("{} ", word); - } - println!(); - if matches.opt_present("entropy") { - println!("{}", diceware::MiniLockWord::entropyn(word_num)) } }