blob: a05a1ac790473769b4315836c40e3214937aec25 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
mod utils;
use wasm_bindgen::prelude::*;
use rand::Rng;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
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();
}
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 extended_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);
}
if extended {
result.append(&mut extended_charset);
}
(0..len)
.map(|_| {
let idx = rng.gen_range(0..result.len());
result[idx] as char
})
.collect()
}
|