diff options
author | gabrielgio <gabriel.giovanini@pm.me> | 2022-03-25 20:31:58 +0100 |
---|---|---|
committer | gabrielgio <gabriel.giovanini@pm.me> | 2022-03-25 20:31:58 +0100 |
commit | 34af714c51173075d3a0ac7148ce60334d248aba (patch) | |
tree | 1f68750a5fd9002b8c1d731a38b31c2d6a1cb398 | |
parent | 51b4460935bb04d26d368810aa5c330fb9bcccdd (diff) | |
download | genpass-34af714c51173075d3a0ac7148ce60334d248aba.tar.gz genpass-34af714c51173075d3a0ac7148ce60334d248aba.tar.bz2 genpass-34af714c51173075d3a0ac7148ce60334d248aba.zip |
feat: Add options for password generator
Add new options to generate a passowrd:
- upper letters
- lower letters
- numbers
- special
Special needs to be worked on still, as probably there are some char
missing.
-rw-r--r-- | src/lib.rs | 34 | ||||
-rw-r--r-- | www/index.html | 72 | ||||
-rw-r--r-- | www/index.js | 55 |
3 files changed, 141 insertions, 20 deletions
@@ -14,18 +14,38 @@ extern { fn alert(s: &str); } + #[wasm_bindgen] -pub fn generate(len: usize) -> String { - const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\ - abcdefghijklmnopqrstuvwxyz\ - 1234567890\ - !@#$%^&*()+{}[]"; +pub fn generate(len: usize, lower: bool, upper: bool, number: bool, special: bool) -> String { + let mut upper_charset: Vec<char> = String::from("ABCDEFGHIJKLMNOPQRSTUVWXYZ").chars().collect(); + 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 result: Vec<char> = Vec::new(); + let mut rng = rand::thread_rng(); + if lower { + result.append(&mut lower_charset); + } + + if upper { + result.append(&mut upper_charset); + } + + if number { + result.append(&mut number_charset); + } + + if special { + result.append(&mut special_charset); + } + + (0..len) .map(|_| { - let idx = rng.gen_range(0..CHARSET.len()); - CHARSET[idx] as char + let idx = rng.gen_range(0..result.len()); + result[idx] as char }) .collect() } diff --git a/www/index.html b/www/index.html index 76d8c1c..5a4ada3 100644 --- a/www/index.html +++ b/www/index.html @@ -9,14 +9,70 @@ <body> <section class="section"> <div class="container"> - <h1 class="title"> - Password Generator - </h1> - <h2 class="subtitle" id="password"> - password - </h2> - <div class="control"> - <button class="button is-primary" id="generate">Generate</button> + <div class="columns is-centered"> + <div class="column is-half"> + <div class="field"> + <div class="control"> + <h1 class="title"> + Password Generator + </h1> + </div> + </div> + <div class="field"> + <div class="control" style="word-wrap: anywhere"> + <label class="label is-size-3 has-text-success" id="password"> + password + </label> + </div> + </div> + <div class="field"> + <div class="control"> + <button class="button is-link" id="copy">copy</button> + </div> + </div> + <div class="field"> + <div class="control"> + <input id="size" class="input" type="number" placeholder="Text input" value="32"> + </div> + </div> + <div class="field"> + <div class="control"> + <label class="checkbox"> + <input id="upperCase" type="checkbox"> + Upper-case letters + </label> + </div> + </div> + <div class="field"> + <div class="control"> + <label class="checkbox"> + <input id="lowerCase" type="checkbox"> + Lower-case letters + </label> + </div> + </div> + <div class="field"> + <div class="control"> + <label class="checkbox"> + <input id="number" type="checkbox"> + Numbers + </label> + </div> + </div> + <div class="field"> + <div class="control"> + <label class="checkbox"> + <input id="special" type="checkbox"> + Special characters + </label> + </div> + </div> + <div class="field"> + <div class="control"> + <button class="button is-primary" id="generate">Generate</button> + </div> + </div> + </div> </div> </div> </section> diff --git a/www/index.js b/www/index.js index e25ec3a..a4257c1 100644 --- a/www/index.js +++ b/www/index.js @@ -1,9 +1,54 @@ import * as wasm from "genpass"; -const LEN = 32; +const DEFAULT_LEN = 32; -document.getElementById("password").innerHTML = wasm.generate(LEN); +let passwordInput = document.getElementById("password"); +let generateButton = document.getElementById("generate"); +let copyButton = document.getElementById("copy"); +let sizeInput = document.getElementById("size"); +let upperCaseInput = document.getElementById("upperCase"); +let lowerCaseInput = document.getElementById("lowerCase"); +let numberInput = document.getElementById("number"); +let specialInput = document.getElementById("special"); -document.getElementById("generate").onclick = (function () { - document.getElementById("password").innerHTML = wasm.generate(LEN); -}); +function copyToClipboard() { + let text = passwordInput.innerText + navigator.clipboard.writeText(text).then(function () { + }, function (err) { + console.error('Async: Could not copy text: ', err); + }); + + copyButton.textContent = "copied" + + setTimeout(() => { + copyButton.innerText = "copy" + }, 3000); +} + +function generate() { + let size = sizeInput.value; + let lower = lowerCaseInput.checked; + let upper = upperCaseInput.checked; + let number = numberInput.checked; + let special = specialInput.checked; + passwordInput.innerHTML = wasm.generate(size, lower, upper, number, special); +} + +// Setting up defaults +sizeInput.value = DEFAULT_LEN; +upperCaseInput.checked = true; +lowerCaseInput.checked = true; +numberInput.checked = true; +specialInput.checked = true; +//this will trigger passwordInput to be filled +generate(); + +//Adding events hooks + +sizeInput.onchange = generate; +upperCaseInput.onchange = generate; +lowerCaseInput.onchange = generate; +numberInput.onchange = generate; +specialInput.onchange = generate; +generateButton.onclick = generate; +copyButton.onclick = copyToClipboard;
\ No newline at end of file |