Skip to content

Instantly share code, notes, and snippets.

@frostu8
Created November 29, 2022 23:11
Show Gist options
  • Select an option

  • Save frostu8/fa2fd3a23028ad2110b8274b92b8cd90 to your computer and use it in GitHub Desktop.

Select an option

Save frostu8/fa2fd3a23028ad2110b8274b92b8cd90 to your computer and use it in GitHub Desktop.
passphrase generation
#![feature(iter_intersperse)]
use rand::prelude::*;
use std::fs::File;
use std::io::{BufRead, BufReader};
fn main() {
// import words list
let file = File::open("atoms.txt").unwrap();
let file = BufReader::new(file);
// collect words into list
let words = file
.lines()
.map(|s| s.unwrap())
.filter(|s| s.len() > 0)
.collect::<Vec<String>>();
// get passphrase len as 1st argument
let passphrase_len = std::env::args()
.nth(1)
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(4);
// select words from list
let passphrase = words
.iter()
.choose_multiple(&mut rand::thread_rng(), passphrase_len)
.into_iter()
.map(|s| &**s)
.intersperse(" ")
.collect::<String>();
println!("new passphrase: {}", passphrase);
// calculate entropy using:
// E = L (words) * log2(R (word count))
let entropy_bits = (passphrase_len as f64) * (words.len() as f64).log2();
println!("entropy: {}", entropy_bits);
// time to guess (at 1000 guesses/sec)
// T = E / 1000
println!("{:.1} years at 1000 guesses/sec", ((2.0f64).powf(entropy_bits) / 1000.) / 60. / 60. / 24. / 365.);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment