2022-10-16 19:59:06 +00:00
|
|
|
use rand::Rng;
|
|
|
|
|
|
|
|
pub fn main () {
|
2022-10-16 22:24:15 +00:00
|
|
|
let passphrase = passphrase (" ", 8);
|
|
|
|
println! ("{}", passphrase);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn passphrase (separator: &str, len: usize) -> String {
|
2022-10-16 19:59:06 +00:00
|
|
|
let diceware = Diceware::default ();
|
|
|
|
|
2022-10-16 22:24:15 +00:00
|
|
|
let random_words: Vec <&str> = (0..len)
|
2022-10-16 19:59:06 +00:00
|
|
|
.map (|_| diceware.random_word ())
|
|
|
|
.collect ();
|
|
|
|
|
2022-10-16 22:24:15 +00:00
|
|
|
random_words.join (separator)
|
2022-10-16 19:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Diceware {
|
|
|
|
words: Vec <String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Diceware {
|
|
|
|
fn default () -> Self
|
|
|
|
{
|
|
|
|
let wordlist = include_str! ("eff_short_wordlist_1.txt");
|
|
|
|
let words: Vec <_> = wordlist.split ('\n').take (1253).map (str::to_string).collect ();
|
|
|
|
|
|
|
|
assert_eq! (words.len (), 1253);
|
|
|
|
assert_eq! (words [0], "acid");
|
|
|
|
assert_eq! (words [600], "large");
|
|
|
|
assert_eq! (words [1252], "zoom");
|
|
|
|
|
|
|
|
Self {
|
|
|
|
words,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Diceware {
|
|
|
|
pub fn random_word (&self) -> &str
|
|
|
|
{
|
|
|
|
&self.words [rand::thread_rng ().gen_range (0..self.words.len ())]
|
|
|
|
}
|
|
|
|
}
|