2024-10-01 23:20:35 +00:00
|
|
|
use rand::seq::SliceRandom as _;
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let passphrase = create_passphrase(" ", 8);
|
|
|
|
println!("{passphrase}");
|
|
|
|
println!("Press Enter");
|
|
|
|
let mut input = String::new();
|
|
|
|
std::io::stdin().read_line(&mut input).ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_passphrase(separator: &str, len: usize) -> String {
|
|
|
|
let dice = Dice::default();
|
|
|
|
let words: Vec<_> = (0..len).map(|_| dice.pick_word()).collect();
|
|
|
|
words.join(separator)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Dice {
|
|
|
|
words: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Dice {
|
|
|
|
fn default() -> Self {
|
|
|
|
let wordlist = include_str!("eff_short_wordlist_1.txt");
|
|
|
|
let words: Vec<_> = wordlist.split('\n').map(|s| s.to_string()).collect();
|
2024-10-01 23:25:29 +00:00
|
|
|
|
2024-10-01 23:20:35 +00:00
|
|
|
assert_eq!(words.len(), 1253);
|
|
|
|
assert_eq!(words[0], "acid");
|
|
|
|
assert_eq!(words[600], "large");
|
|
|
|
assert_eq!(words[1252], "zoom");
|
2024-10-01 23:25:29 +00:00
|
|
|
|
|
|
|
Self { words }
|
2024-10-01 23:20:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dice {
|
|
|
|
pub fn pick_word(&self) -> &str {
|
2024-10-01 23:25:29 +00:00
|
|
|
self.words
|
|
|
|
.choose(&mut rand::thread_rng())
|
|
|
|
.expect("`choose` should always return `Some`")
|
2024-10-01 23:20:35 +00:00
|
|
|
}
|
|
|
|
}
|