made some fixes
This commit is contained in:
+37
-31
@@ -2,19 +2,16 @@ use rand::Rng;
|
|||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
// enum variant (i keep forgetting the terminology)
|
// alphanumeric only
|
||||||
// enum PasswordType {
|
const CHARSET: &[u8] =
|
||||||
// RandomChars(usize),
|
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
// Passphrase(usize, usize),
|
|
||||||
// }
|
|
||||||
|
|
||||||
// non-CTRL chars
|
// excludes backtick(`), quote('), dquote("), slash(/), bslash(\)
|
||||||
fn random_ascii() -> u8 {
|
const CHARSET_WITH_SYMBOLS: &[u8] =
|
||||||
// we definitely want to use the OS for randomness
|
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()[]{}|?<>,.-_=+~";
|
||||||
// doesn't require seeding because it is backed by CSPRNG, backed by OS
|
|
||||||
// the OS maintains it's own entropy from mouse movement, hardware noise,etc
|
|
||||||
let mut rng = OsRng;
|
|
||||||
|
|
||||||
|
|
||||||
|
fn random_ascii(rng: &mut OsRng, char_set: &[u8]) -> u8 {
|
||||||
// NOT IDEAL FOR CRYPTO/password generation
|
// NOT IDEAL FOR CRYPTO/password generation
|
||||||
// let range_max = 122; // 125 - 33
|
// let range_max = 122; // 125 - 33
|
||||||
// let n = rng.next_u32() % range_max; // slightly modulo biased
|
// let n = rng.next_u32() % range_max; // slightly modulo biased
|
||||||
@@ -22,47 +19,56 @@ fn random_ascii() -> u8 {
|
|||||||
|
|
||||||
// this function under the hood creates a zone that divides evenly
|
// this function under the hood creates a zone that divides evenly
|
||||||
// into the range so we don't encounter modulo bias
|
// into the range so we don't encounter modulo bias
|
||||||
rng.gen_range(33..125) as u8
|
let index = rng.gen_range(0..char_set.len());
|
||||||
|
char_set[index]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_random_chars(len: usize) -> String {
|
fn generate_random_chars(len: usize, symbols: bool, rng: &mut OsRng) -> String {
|
||||||
// String is a thin wrapper around Vec<u8> so we can expect re-allocation
|
// String is a thin wrapper around Vec<u8> so we can expect re-allocation
|
||||||
// and copy only when we exceed the capacity of the vector
|
// and copy only when we exceed the capacity of the vector
|
||||||
let mut res = String::new();
|
// However, with_capacity will perform only one allocation anyway
|
||||||
|
let mut res = String::with_capacity(len);
|
||||||
|
let char_set = if symbols { CHARSET_WITH_SYMBOLS } else { CHARSET };
|
||||||
for _ in 0..len {
|
for _ in 0..len {
|
||||||
res.push(random_ascii() as char);
|
res.push(random_ascii(rng, char_set) as char);
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_passphrase(phrases: usize, len: usize, words: &Vec<&str>) -> String {
|
// we can take a slice of "words" because we don't care about:
|
||||||
let mut res = String::new();
|
// capacity, mutation (push/pop), allocation strategy
|
||||||
let mut count = 0;
|
fn generate_passphrase(phrase_count: usize,
|
||||||
while count < phrases {
|
word_list: &[&str], rng: &mut OsRng) -> String {
|
||||||
let word = random_word(words);
|
let mut res = String::with_capacity(phrase_count*2); // save a couple allocations
|
||||||
if word.is_ascii() { // only add ascii words
|
for _ in 0..phrase_count {
|
||||||
res.push_str(random_word(words));
|
res.push_str(random_word(word_list, rng));
|
||||||
count += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
res.truncate(len); // only works on ascii
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn random_word<'a>(words: &'a Vec<&'a str>) -> &'a str {
|
fn random_word<'a>(word_list: &'a [&'a str], rng: &mut OsRng) -> &'a str {
|
||||||
let mut rng = OsRng;
|
word_list[rng.gen_range(0..word_list.len())]
|
||||||
words[rng.gen_range(0..words.len())]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let content = fs::read_to_string("/usr/share/dict/words")
|
let content = fs::read_to_string("/usr/share/dict/words")
|
||||||
.expect("could not read dictionary");
|
.expect("could not read dictionary");
|
||||||
let words: Vec<&str> = content.lines().collect();
|
|
||||||
|
|
||||||
let chars = generate_random_chars(20);
|
// filter words here for only ascii
|
||||||
|
let word_list: Vec<&str> = content.lines().filter(|w|
|
||||||
|
w.is_ascii()).collect();
|
||||||
|
|
||||||
|
// we definitely want to use the OS for randomness
|
||||||
|
// doesn't require seeding because it is backed by CSPRNG, backed by OS
|
||||||
|
// the OS maintains it's own entropy from mouse movement, hardware noise,etc
|
||||||
|
// we pass it through the functions to avoid a potential syscall
|
||||||
|
let mut rng = OsRng;
|
||||||
|
let chars = generate_random_chars(20, true, &mut rng);
|
||||||
println!("generated random chars: {}", chars.as_str());
|
println!("generated random chars: {}", chars.as_str());
|
||||||
|
|
||||||
let phrase = generate_passphrase(3, 20, &words);
|
let phrase = generate_passphrase(3, &word_list, &mut rng);
|
||||||
println!("generated passphrase: {}", phrase.as_str());
|
println!("generated passphrase: {}", phrase.as_str());
|
||||||
|
|
||||||
|
println!("words in dictionary: {}", word_list.len());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user