Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- rust modules and PasswordInfo - Update generation methods and include entropy in UI - Add entropy meter and update css
- Loading branch information
Showing
13 changed files
with
351 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::collections::HashSet; | ||
|
||
// https://en.wikipedia.org/wiki/Password_strength#Entropy_as_a_measure_of_password_strength | ||
// R = pool of unique characters | ||
// log2(R^Length) | ||
pub fn bits_entropy(unique_symbols: usize, pool_length: usize) -> u32 { | ||
let pool_length = pool_length as f64; | ||
(pool_length.powf(unique_symbols as f64)).log2() as u32 | ||
} | ||
|
||
pub fn get_password_strength(text: &String, pool_length: usize) -> u32 { | ||
let unique_symbols = get_unique_chars_count(text); | ||
bits_entropy(unique_symbols, pool_length) | ||
} | ||
|
||
fn get_unique_chars_count(text: &String) -> usize { | ||
let mut set: HashSet<char> = HashSet::new(); | ||
let chars: Vec<char> = text.chars().into_iter().collect(); | ||
for item in chars { | ||
set.insert(item); | ||
} | ||
|
||
set.len() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate::{entropy::get_password_strength, password_info::PasswordInfo, POOL, SPECIAL}; | ||
use hex::encode; | ||
use rand::prelude::*; | ||
|
||
pub fn gen_schemed( | ||
hash_length: usize, | ||
is_inverted: bool, | ||
words: &mut Vec<String>, | ||
use_special: bool, | ||
) -> PasswordInfo { | ||
let rng = &mut thread_rng(); | ||
for _ in 0..10 { | ||
words.shuffle(rng); | ||
} | ||
|
||
let word = words.choose(rng).unwrap().to_owned(); | ||
let nonsense = gen_nonsense(hash_length, use_special); | ||
let hash = nonsense.text; | ||
|
||
let pool_length = POOL.len() + (use_special as usize) * SPECIAL.len(); | ||
let text = if is_inverted { | ||
format!("{}{}", hash, word) | ||
} else { | ||
format!("{}{}", word, hash) | ||
}; | ||
|
||
let entropy = get_password_strength(&text, pool_length); | ||
|
||
PasswordInfo::new(text, entropy) | ||
} | ||
|
||
pub fn gen_nonsense(length: usize, use_special: bool) -> PasswordInfo { | ||
let rng = &mut thread_rng(); | ||
let mut bytes: Vec<u8> = POOL.as_bytes().iter().cloned().collect(); | ||
|
||
if use_special { | ||
bytes.extend(SPECIAL.as_bytes()); | ||
} | ||
|
||
for _ in 0..10 { | ||
bytes.shuffle(rng); | ||
} | ||
|
||
let bucket: Vec<u8> = (0..length) | ||
.map(|_| bytes.choose(rng).unwrap().to_owned()) | ||
.collect(); | ||
|
||
let pool_length = POOL.len() + (use_special as usize) * SPECIAL.len(); | ||
let text = String::from_utf8(bucket).unwrap_or(String::default()); | ||
let entropy = get_password_strength(&text, pool_length); | ||
|
||
PasswordInfo::new(text, entropy) | ||
} | ||
|
||
pub fn gen_random_bytes_hex(size: usize) -> PasswordInfo { | ||
let text = encode(get_random_bytes(size)); | ||
let entropy = get_password_strength(&text, 16); | ||
PasswordInfo::new(text, entropy) | ||
} | ||
|
||
fn get_random_bytes(size: usize) -> Vec<u8> { | ||
let bytes: Vec<u8> = (0..size).map(|_| random::<u8>()).collect(); | ||
bytes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pub mod entropy; | ||
pub mod gen_methods; | ||
pub mod password_info; | ||
|
||
pub const POOL: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // 62 | ||
pub const SPECIAL: &str = "!#$%&'()*+,-./:;<=>?{|}~[]^_@|\""; // 31 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use serde::Serialize; | ||
#[derive(Serialize)] | ||
pub struct PasswordInfo { | ||
pub text: String, | ||
pub entropy: u32, | ||
} | ||
|
||
impl PasswordInfo { | ||
pub fn new(text: String, entropy: u32) -> Self { | ||
PasswordInfo { text, entropy } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.