45 lines
892 B
Rust
45 lines
892 B
Rust
use rand::Rng;
|
|
|
|
pub fn main () {
|
|
let passphrase = passphrase (" ", 8);
|
|
println! ("{}", passphrase);
|
|
}
|
|
|
|
pub fn passphrase (separator: &str, len: usize) -> String {
|
|
let diceware = Diceware::default ();
|
|
|
|
let random_words: Vec <&str> = (0..len)
|
|
.map (|_| diceware.random_word ())
|
|
.collect ();
|
|
|
|
random_words.join (separator)
|
|
}
|
|
|
|
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 ())]
|
|
}
|
|
}
|