Skip to content

Commit

Permalink
use one PRNG even if user requests multiple passphrases
Browse files Browse the repository at this point in the history
  • Loading branch information
sts10 committed Nov 11, 2023
1 parent 0b68faf commit 71addce
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions benches/generate_passphrase.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use phraze::*;
use rand::thread_rng;

fn criterion_benchmark(c: &mut Criterion) {
// Define a Criterion group, just so we can set a sample_size
Expand All @@ -15,11 +16,13 @@ fn criterion_benchmark(c: &mut Criterion) {
// include the fetching of the (built-in) list
// in the benchmark
let wordlist = fetch_list(ListChoice::Medium);
let mut rng = thread_rng();
generate_a_passphrase(
number_of_words_to_put_in_passphrase,
separator,
title_case,
wordlist,
&mut rng,
)
})
});
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod unicode_normalization_check;

use crate::separators::make_separator;
use include_lines::include_lines;
use rand::{seq::SliceRandom, thread_rng, Rng};
use rand::{seq::SliceRandom, Rng};

/// The possible word lists that Phraze can use.
#[derive(Clone, Debug, Copy)]
Expand Down Expand Up @@ -87,22 +87,22 @@ pub fn generate_a_passphrase<T: AsRef<str> + std::fmt::Display>(
separator: &str,
title_case: bool,
list: &[T], // Either type!
rng: &mut impl Rng,
) -> String {
let mut rng = thread_rng();
// Create a blank String to put words into to create our passphrase
let mut passphrase = String::new();
for i in 0..number_of_words_to_put_in_passphrase {
// Check if we're doing title_case
let random_word = if title_case {
make_title_case(&get_random_element(&mut rng, list))
make_title_case(&get_random_element(rng, list))
} else {
get_random_element(&mut rng, list)
get_random_element(rng, list)
};
// Add this word to our passphrase
passphrase += &random_word;
// Add a separator
if i != number_of_words_to_put_in_passphrase - 1 {
passphrase += &make_separator(&mut rng, separator);
passphrase += &make_separator(rng, separator);
}
}
passphrase.to_string()
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::file_reader::read_in_custom_list;
use clap::Parser;
use phraze::*;
use rand::thread_rng;
use std::path::PathBuf;

/// Generate random passphrases
Expand Down Expand Up @@ -126,6 +127,10 @@ fn generate_passphrases<T: AsRef<str> + std::fmt::Display>(opt: &Args, word_list
opt.n_passphrases,
);
}
// Create one random number generator for Phraze to use for ALL
// generated passphrases. I believe this is more
// cryptographically secure/responsible.
let mut rng = thread_rng();

// Now we can (finally) generate and print some number of passphrases
for _ in 0..opt.n_passphrases {
Expand All @@ -134,6 +139,7 @@ fn generate_passphrases<T: AsRef<str> + std::fmt::Display>(opt: &Args, word_list
&opt.separator,
opt.title_case,
word_list,
&mut rng,
);
println!("{}", passphrase);
}
Expand Down

0 comments on commit 71addce

Please sign in to comment.