aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs16
-rw-r--r--www/index.html1
-rw-r--r--www/index.js10
3 files changed, 19 insertions, 8 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 662dda6..cfa078a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -15,7 +15,17 @@ extern {
}
#[wasm_bindgen]
-pub fn generate() -> String {
- let num = rand::thread_rng().gen_range(0..100);
- format!("{}", num)
+pub fn generate(len: usize) -> String {
+ const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
+ abcdefghijklmnopqrstuvwxyz\
+ 1234567890\
+ !@#$%^&*()+{}[]";
+ let mut rng = rand::thread_rng();
+
+ (0..len)
+ .map(|_| {
+ let idx = rng.gen_range(0..CHARSET.len());
+ CHARSET[idx] as char
+ })
+ .collect()
}
diff --git a/www/index.html b/www/index.html
index 8007121..76d8c1c 100644
--- a/www/index.html
+++ b/www/index.html
@@ -12,7 +12,6 @@
<h1 class="title">
Password Generator
</h1>
- <label>This is a work in progress</label>
<h2 class="subtitle" id="password">
password
</h2>
diff --git a/www/index.js b/www/index.js
index c9baaf8..e25ec3a 100644
--- a/www/index.js
+++ b/www/index.js
@@ -1,7 +1,9 @@
import * as wasm from "genpass";
-function generatePassword() {
- document.getElementById("password").innerHTML = wasm.generate();
-}
+const LEN = 32;
-document.getElementById("generate").onclick = generatePassword
+document.getElementById("password").innerHTML = wasm.generate(LEN);
+
+document.getElementById("generate").onclick = (function () {
+ document.getElementById("password").innerHTML = wasm.generate(LEN);
+});