webtools

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

lib.rs (700B)


      1 use wasm_bindgen::prelude::*;
      2 use rand::Rng;
      3 
      4 #[wasm_bindgen]
      5 pub fn generate_password(length: usize, ascii_flags: &[u8]) -> String {
      6     if ascii_flags.len() != 128 {
      7         return String::from("");
      8     }
      9 
     10     let mut charset: Vec<char> = Vec::new();
     11     for (i, flag) in ascii_flags.iter().enumerate() {
     12         if *flag == 1 {
     13             if let Some(c) = char::from_u32(i as u32) {
     14                 charset.push(c);
     15             }
     16         }
     17     }
     18 
     19     if charset.is_empty() {
     20         return String::from("");
     21     }
     22 
     23     let mut rng = rand::rng();
     24     (0..length)
     25         .map(|_| {
     26             let idx = rng.random_range(0..charset.len());
     27             charset[idx]
     28         })
     29         .collect()
     30 }