From 51b4460935bb04d26d368810aa5c330fb9bcccdd Mon Sep 17 00:00:00 2001 From: gabrielgio Date: Fri, 25 Mar 2022 18:55:37 +0100 Subject: ref: Add actual pass generator function Modify `generate` function to actually generate usable. --- src/lib.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src') 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() } -- cgit v1.2.3