From d6a9e6e46f2c2574ec0665e73b64f0d6c700557a Mon Sep 17 00:00:00 2001 From: gabrielgio Date: Fri, 25 Mar 2022 23:14:04 +0100 Subject: feat: Add extended ASCII Add more chars... --- src/lib.rs | 10 ++++++++-- tests/web.rs | 49 ++++++++++++++++++++++++++++++------------------- www/index.html | 8 ++++++++ www/index.js | 6 +++++- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e8999d4..a05a1ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,8 +16,8 @@ extern { #[wasm_bindgen] -pub fn generate(len: usize, lower: bool, upper: bool, number: bool, special: bool) -> String { - if len == 0 ||!lower && !upper && !number && !special { +pub fn generate(len: usize, lower: bool, upper: bool, number: bool, special: bool, extended: bool) -> String { + if len == 0 || !lower && !upper && !number && !special { return "".to_string(); } @@ -25,6 +25,8 @@ pub fn generate(len: usize, lower: bool, upper: bool, number: bool, special: boo let mut lower_charset: Vec = String::from("abcdefghijklmnopqrstuvwxyz").chars().collect(); let mut number_charset: Vec = String::from("1234567890").chars().collect(); let mut special_charset: Vec = String::from("!@#$%^&*()+{}[]><:|+=;-.,'?_~\\/`\"").chars().collect(); + let mut extended_charset: Vec = String::from("®û´¢æ«æëàñîêáö½¬èñý¹áúóâªò×ä¸ÿ¦ù°º±¨åûïøí»§÷î¤ã¡òþ¿³óùçâüëíõôéðìö¶þ¾úý·êéãߣüèðµå¥õïà¼øäô©²¯çì").chars().collect(); + let mut result: Vec = Vec::new(); let mut rng = rand::thread_rng(); @@ -45,6 +47,10 @@ pub fn generate(len: usize, lower: bool, upper: bool, number: bool, special: boo result.append(&mut special_charset); } + if extended { + result.append(&mut extended_charset); + } + (0..len) .map(|_| { let idx = rng.gen_range(0..result.len()); diff --git a/tests/web.rs b/tests/web.rs index eedd1b4..b5af034 100644 --- a/tests/web.rs +++ b/tests/web.rs @@ -14,50 +14,61 @@ const ATTEMPTS: usize = 5; #[wasm_bindgen_test] fn generate_length() { for x in (0..=100).step_by(5) { - let password = generate(x, true, true, true, true); + let password = generate(x, true, true, true, true, false); assert_eq!(x, password.len()) } } #[wasm_bindgen_test] -fn generate_not_upper_letters() { - let lower_charset = "abcdefghijklmnopqrstuvwxyz"; +fn generate_no_upper_letters() { + let charset = "abcdefghijklmnopqrstuvwxyz"; for _ in 0..ATTEMPTS { - let password = generate(100, false, true, true, true); - for char in String::from(lower_charset).chars().into_iter() { + let password = generate(100, false, true, true, true, true); + for char in String::from(charset).chars().into_iter() { assert!(!password.contains(char)) } } } #[wasm_bindgen_test] -fn generate_not_lower_letters() { - let upper_charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +fn generate_no_lower_letters() { + let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for x in 0..ATTEMPTS { - let password = generate(x, true, false, true, true); - for char in String::from(upper_charset).chars().into_iter() { + let password = generate(x, true, false, true, true, true); + for char in String::from(charset).chars().into_iter() { assert!(!password.contains(char)) } } } #[wasm_bindgen_test] -fn generate_not_number_letters() { - let upper_charset = "1234567890"; +fn generate_no_number_letters() { + let charset = "1234567890"; for x in 0..ATTEMPTS { - let password = generate(x, true, true, false, true); - for char in String::from(upper_charset).chars().into_iter() { + let password = generate(x, true, true, false, true, true); + for char in String::from(charset).chars().into_iter() { assert!(!password.contains(char)) } } } #[wasm_bindgen_test] -fn generate_not_special_letters() { - let upper_charset = "!@#$%^&*()+{}[]><:|+=;-.,'?_~\\/`\""; +fn generate_no_special_letters() { + let charset = "!@#$%^&*()+{}[]><:|+=;-.,'?_~\\/`\""; for x in 0..ATTEMPTS { - let password = generate(x, true, true, true, false); - for char in String::from(upper_charset).chars().into_iter() { + let password = generate(x, true, true, true, false, true); + for char in String::from(charset).chars().into_iter() { + assert!(!password.contains(char)) + } + } +} + +#[wasm_bindgen_test] +fn generate_no_extended_letters() { + let charset = "®û´¢æ«æëàñîêáö½¬èñý¹áúóâªò×ä¸ÿ¦ù°º±¨åûïøí»§÷î¤ã¡òþ¿³óùçâüëíõôéðìö¶þ¾úý·êéãߣüèðµå¥õïà¼øäô©²¯çì"; + for x in 0..ATTEMPTS { + let password = generate(x, true, true, true, true, false); + for char in String::from(charset).chars().into_iter() { assert!(!password.contains(char)) } } @@ -65,12 +76,12 @@ fn generate_not_special_letters() { #[wasm_bindgen_test] fn generate_0_len() { - let password = generate(0, true, true, true, true); + let password = generate(0, true, true, true, true, true); assert_eq!(0, password.len()) } #[wasm_bindgen_test] fn generate_no_letters() { - let password = generate(100, false, false, false, false); + let password = generate(100, false, false, false, false, true); assert_eq!(0, password.len()) } diff --git a/www/index.html b/www/index.html index 5a4ada3..3197b6c 100644 --- a/www/index.html +++ b/www/index.html @@ -67,6 +67,14 @@ +
+
+ +
+
diff --git a/www/index.js b/www/index.js index a4257c1..d7b2869 100644 --- a/www/index.js +++ b/www/index.js @@ -10,6 +10,7 @@ let upperCaseInput = document.getElementById("upperCase"); let lowerCaseInput = document.getElementById("lowerCase"); let numberInput = document.getElementById("number"); let specialInput = document.getElementById("special"); +let extendedInput = document.getElementById("extended"); function copyToClipboard() { let text = passwordInput.innerText @@ -31,7 +32,8 @@ function generate() { let upper = upperCaseInput.checked; let number = numberInput.checked; let special = specialInput.checked; - passwordInput.innerHTML = wasm.generate(size, lower, upper, number, special); + let extended = extendedInput.checked; + passwordInput.innerHTML = wasm.generate(size, lower, upper, number, special, extended); } // Setting up defaults @@ -40,6 +42,7 @@ upperCaseInput.checked = true; lowerCaseInput.checked = true; numberInput.checked = true; specialInput.checked = true; +extendedInput.checked = true; //this will trigger passwordInput to be filled generate(); @@ -50,5 +53,6 @@ upperCaseInput.onchange = generate; lowerCaseInput.onchange = generate; numberInput.onchange = generate; specialInput.onchange = generate; +extendedInput.onchange = generate; generateButton.onclick = generate; copyButton.onclick = copyToClipboard; \ No newline at end of file -- cgit v1.2.3