aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs10
-rw-r--r--tests/web.rs49
-rw-r--r--www/index.html8
-rw-r--r--www/index.js6
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<char> = String::from("abcdefghijklmnopqrstuvwxyz").chars().collect();
let mut number_charset: Vec<char> = String::from("1234567890").chars().collect();
let mut special_charset: Vec<char> = String::from("!@#$%^&*()+{}[]><:|+=;-.,'?_~\\/`\"").chars().collect();
+ let mut extended_charset: Vec<char> = String::from("®û´¢æ«æëàñîêáö½¬èñý¹áúóâªò×ä¸ÿ¦ù°º±¨åûïøí»§÷î¤ã¡òþ¿³óùçâüëíõôéðìö¶þ¾úý·êéãߣüèðµå¥õïà¼øäô©²¯çì").chars().collect();
+
let mut result: Vec<char> = 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
@@ -69,6 +69,14 @@
</div>
<div class="field">
<div class="control">
+ <label class="checkbox">
+ <input id="extended" type="checkbox">
+ Extended ASCII
+ </label>
+ </div>
+ </div>
+ <div class="field">
+ <div class="control">
<button class="button is-primary" id="generate">Generate</button>
</div>
</div>
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