#![no_std]
extern crate no_std_compat as std;
use std::prelude::v1::*;
#[cfg(feature = "words")]
mod words;
#[cfg(feature = "nouns")]
mod nouns;
#[cfg(feature = "adjectives")]
mod adjectives;
#[cfg(feature = "names")]
mod names;
#[cfg(feature = "first_names")]
mod first_names;
#[cfg(feature = "last_names")]
mod last_names;
#[cfg(feature = "default")]
pub use rand::{
rngs::{ OsRng, ThreadRng, StdRng },
Rng, SeedableRng, RngCore, CryptoRng,
thread_rng,
};
#[cfg(feature = "custom")]
pub use getrandom::register_custom_getrandom;
#[cfg(all(any(feature = "constrained", feature = "custom"), not(feature = "default")))]
pub use rand::{
Rng, SeedableRng, RngCore, CryptoRng
};
#[cfg(any(feature = "first_names", feature = "last_names"))]
use inflections::Inflect;
#[cfg(any(feature = "constrained", feature = "custom", feature = "default"))]
use rand::Error;
#[cfg(feature = "custom")]
#[derive(Clone)]
pub struct CustomRng {
rng: std::sync::Arc<spin::mutex::Mutex<dyn RngCore>>
}
#[cfg(feature = "custom")]
impl CustomRng {
pub fn new(rng: impl RngCore + 'static) -> Self {
Self {
rng: std::sync::Arc::new(spin::mutex::Mutex::new(rng))
}
}
}
#[cfg(feature = "custom")]
impl RngCore for CustomRng {
fn next_u32(&mut self) -> u32 {
let mut rl = self.rng.lock();
rl.next_u32()
}
fn next_u64(&mut self) -> u64 {
let mut rl = self.rng.lock();
rl.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut rl = self.rng.lock();
rl.fill_bytes(dest)
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
let mut rl = self.rng.lock();
rl.try_fill_bytes(dest)
}
}
#[cfg(feature = "custom")]
pub trait RngCrypto: RngCore + CryptoRng {}
#[derive(Clone)]
#[cfg(feature = "custom")]
pub struct CustomCryptoRng {
rng: std::sync::Arc<spin::mutex::Mutex<dyn RngCrypto>>
}
#[cfg(feature = "custom")]
impl CustomCryptoRng {
pub fn new(rng: impl RngCrypto + 'static) -> Self {
Self {
rng: std::sync::Arc::new(spin::mutex::Mutex::new(rng))
}
}
}
#[cfg(feature = "custom")]
impl RngCrypto for CustomCryptoRng {}
#[cfg(feature = "custom")]
impl CryptoRng for CustomCryptoRng {}
#[cfg(feature = "custom")]
impl RngCore for CustomCryptoRng {
fn next_u32(&mut self) -> u32 {
let mut rl = self.rng.lock();
rl.next_u32()
}
fn next_u64(&mut self) -> u64 {
let mut rl = self.rng.lock();
rl.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut rl = self.rng.lock();
rl.fill_bytes(dest)
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
let mut rl = self.rng.lock();
rl.try_fill_bytes(dest)
}
}
#[derive(Clone)]
pub enum RNG {
#[cfg(feature = "default")]
Std(rand::rngs::StdRng),
#[cfg(feature = "default")]
Thread(rand::rngs::ThreadRng),
#[cfg(feature = "default")]
OS(rand::rngs::OsRng),
#[cfg(feature = "custom")]
Custom(CustomRng),
#[cfg(feature = "custom")]
CustomRaw(std::sync::Arc<spin::mutex::Mutex<dyn RngCore>>),
#[cfg(feature = "custom")]
CustomCrypto(CustomCryptoRng),
#[cfg(feature = "custom")]
CustomCryptoRaw(std::sync::Arc<spin::mutex::Mutex<dyn RngCrypto>>)
}
impl RngCore for RNG {
fn next_u32(&mut self) -> u32 {
match self {
#[cfg(feature = "default")]
RNG::Std(r) => r.next_u32(),
#[cfg(feature = "default")]
RNG::Thread(r) => r.next_u32(),
#[cfg(feature = "default")]
RNG::OS(r) => r.next_u32(),
#[cfg(feature = "custom")]
RNG::Custom(r) => r.next_u32(),
#[cfg(feature = "custom")]
RNG::CustomRaw(r) => {
let mut rl = r.lock();
rl.next_u32()
},
#[cfg(feature = "custom")]
RNG::CustomCrypto(r) => r.next_u32(),
#[cfg(feature = "custom")]
RNG::CustomCryptoRaw(r) => {
let mut rl = r.lock();
rl.next_u32()
},
_ => panic!("No known random generator")
}
}
fn next_u64(&mut self) -> u64 {
match self {
#[cfg(feature = "default")]
RNG::Std(r) => r.next_u64(),
#[cfg(feature = "default")]
RNG::Thread(r) => r.next_u64(),
#[cfg(feature = "default")]
RNG::OS(r) => r.next_u64(),
#[cfg(feature = "custom")]
RNG::Custom(r) => r.next_u64(),
#[cfg(feature = "custom")]
RNG::CustomRaw(r) => {
let mut rl = r.lock();
rl.next_u64()
},
#[cfg(feature = "custom")]
RNG::CustomCrypto(r) => r.next_u64(),
#[cfg(feature = "custom")]
RNG::CustomCryptoRaw(r) => {
let mut rl = r.lock();
rl.next_u64()
},
_ => panic!("No known random generator")
}
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
match self {
#[cfg(feature = "default")]
RNG::Std(r) => r.fill_bytes(dest),
#[cfg(feature = "default")]
RNG::Thread(r) => r.fill_bytes(dest),
#[cfg(feature = "default")]
RNG::OS(r) => r.fill_bytes(dest),
#[cfg(feature = "custom")]
RNG::Custom(r) => r.fill_bytes(dest),
#[cfg(feature = "custom")]
RNG::CustomRaw(r) => {
let mut rl = r.lock();
rl.fill_bytes(dest)
},
#[cfg(feature = "custom")]
RNG::CustomCrypto(r) => r.fill_bytes(dest),
#[cfg(feature = "custom")]
RNG::CustomCryptoRaw(r) => {
let mut rl = r.lock();
rl.fill_bytes(dest)
},
_ => panic!("No known random generator")
}
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
match self {
#[cfg(feature = "default")]
RNG::Std(r) => r.try_fill_bytes(dest),
#[cfg(feature = "default")]
RNG::Thread(r) => r.try_fill_bytes(dest),
#[cfg(feature = "default")]
RNG::OS(r) => r.try_fill_bytes(dest),
#[cfg(feature = "custom")]
RNG::Custom(r) => r.try_fill_bytes(dest),
#[cfg(feature = "custom")]
RNG::CustomRaw(r) => {
let mut rl = r.lock();
rl.try_fill_bytes(dest)
},
#[cfg(feature = "custom")]
RNG::CustomCrypto(r) => r.try_fill_bytes(dest),
#[cfg(feature = "custom")]
RNG::CustomCryptoRaw(r) => {
let mut rl = r.lock();
rl.try_fill_bytes(dest)
},
_ => panic!("No known random generator")
}
}
}
#[derive(Clone)]
pub enum CryptoRNG {
#[cfg(feature = "default")]
Std(rand::rngs::StdRng),
#[cfg(feature = "default")]
Thread(rand::rngs::ThreadRng),
#[cfg(feature = "default")]
OS(rand::rngs::OsRng),
#[cfg(feature = "custom")]
CustomCrypto(CustomCryptoRng),
#[cfg(feature = "custom")]
CustomCryptoRaw(std::sync::Arc<spin::mutex::Mutex<dyn RngCrypto>>)
}
impl CryptoRng for CryptoRNG {}
impl RngCore for CryptoRNG {
fn next_u32(&mut self) -> u32 {
match self {
#[cfg(feature = "default")]
CryptoRNG::Std(r) => r.next_u32(),
#[cfg(feature = "default")]
CryptoRNG::Thread(r) => r.next_u32(),
#[cfg(feature = "default")]
CryptoRNG::OS(r) => r.next_u32(),
#[cfg(feature = "custom")]
CryptoRNG::CustomCrypto(r) => r.next_u32(),
#[cfg(feature = "custom")]
CryptoRNG::CustomCryptoRaw(r) => {
let mut rl = r.lock();
rl.next_u32()
},
_ => panic!("No known random generator")
}
}
fn next_u64(&mut self) -> u64 {
match self {
#[cfg(feature = "default")]
CryptoRNG::Std(r) => r.next_u64(),
#[cfg(feature = "default")]
CryptoRNG::Thread(r) => r.next_u64(),
#[cfg(feature = "default")]
CryptoRNG::OS(r) => r.next_u64(),
#[cfg(feature = "custom")]
CryptoRNG::CustomCrypto(r) => r.next_u64(),
#[cfg(feature = "custom")]
CryptoRNG::CustomCryptoRaw(r) => {
let mut rl = r.lock();
rl.next_u64()
},
_ => panic!("No known random generator")
}
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
match self {
#[cfg(feature = "default")]
CryptoRNG::Std(r) => r.fill_bytes(dest),
#[cfg(feature = "default")]
CryptoRNG::Thread(r) => r.fill_bytes(dest),
#[cfg(feature = "default")]
CryptoRNG::OS(r) => r.fill_bytes(dest),
#[cfg(feature = "custom")]
CryptoRNG::CustomCrypto(r) => r.fill_bytes(dest),
#[cfg(feature = "custom")]
CryptoRNG::CustomCryptoRaw(r) => {
let mut rl = r.lock();
rl.fill_bytes(dest)
},
_ => panic!("No known random generator")
}
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
match self {
#[cfg(feature = "default")]
CryptoRNG::Std(r) => r.try_fill_bytes(dest),
#[cfg(feature = "default")]
CryptoRNG::Thread(r) => r.try_fill_bytes(dest),
#[cfg(feature = "default")]
CryptoRNG::OS(r) => r.try_fill_bytes(dest),
#[cfg(feature = "custom")]
CryptoRNG::CustomCrypto(r) => r.try_fill_bytes(dest),
#[cfg(feature = "custom")]
CryptoRNG::CustomCryptoRaw(r) => {
let mut rl = r.lock();
rl.try_fill_bytes(dest)
},
_ => panic!("No known random generator")
}
}
}
#[derive(Clone)]
pub struct RandomGenerator {
generator: RNG
}
impl RandomGenerator {
pub fn new(gen: Option<RNG>) -> Self {
let generator = {
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(g) => g,
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(g) => g,
None => RNG::Std(rand::rngs::StdRng::from_entropy())
}
}
};
Self {
generator
}
}
pub fn generate_simple_key_one_time(len: usize) -> Vec<u8> {
RandomGenerator::generate_key_one_time(len, &mut None::<RNG>)
}
pub fn generate_key_one_time(len: usize, gen: &mut Option<impl RngCore>) -> Vec<u8> {
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
let mut key = Vec::with_capacity(len);
for _ in 0..len { key.push(rng.gen()); }
key
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
let mut key = Vec::with_capacity(len);
for _ in 0..len { key.push(rng.gen()); }
key
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
let mut key = Vec::with_capacity(len);
for _ in 0..len { key.push(rng.gen()); }
key
}
}
}
}
pub fn get_simple_random_bytes_one_time(len: usize, gen: &mut Option<impl RngCore>) -> Vec<u8> {
RandomGenerator::get_random_bytes_one_time(len, &mut None::<RNG>)
}
pub fn get_random_bytes_one_time(len: usize, gen: &mut Option<impl RngCore>) -> Vec<u8> {
let mut bytes = vec![];
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
for _ in 0..len {
let value: u8 = rng.gen_range(0..=255);
bytes.push(value)
}
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
for _ in 0..len {
let value: u8 = rng.gen_range(0..=255);
bytes.push(value)
}
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
for _ in 0..len {
let value: u8 = rng.gen_range(0..=255);
bytes.push(value)
}
}
}
}
bytes
}
pub fn get_simple_random_usize_one_time(min: usize, max: usize) -> usize {
RandomGenerator::get_random_usize_one_time(min, max, &mut None::<RNG>)
}
pub fn get_random_usize_one_time(min: usize, max: usize, gen: &mut Option<impl RngCore>) -> usize {
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
rng.gen_range(min..=max)
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
rng.gen_range(min..=max)
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
rng.gen_range(min..=max)
}
}
}
}
pub fn get_simple_random_string_one_time(len: usize, gen: &mut Option<impl RngCore>) -> String {
RandomGenerator::get_simple_random_string_one_time(len, &mut None::<RNG>)
}
pub fn get_random_string_one_time(len: usize, gen: &mut Option<impl RngCore>) -> String {
let mut str = String::with_capacity(len);
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
for _ in 0..len {
let mut value: u8 = rng.gen_range(33..122);
if value >= 33 && value <= 37 { value = value + 20; }
else if value >= 38 && value <= 47 { value = value + 10; }
else if value >= 58 && value <= 64 { value = value + 10; }
else if value >= 91 && value <= 96 { value = value + 10; }
str.push(char::from(value));
}
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
for _ in 0..len {
let mut value: u8 = rng.gen_range(33..122);
if value >= 33 && value <= 37 { value = value + 20; }
else if value >= 38 && value <= 47 { value = value + 10; }
else if value >= 58 && value <= 64 { value = value + 10; }
else if value >= 91 && value <= 96 { value = value + 10; }
str.push(char::from(value));
}
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
for _ in 0..len {
let mut value: u8 = rng.gen_range(33..122);
if value >= 33 && value <= 37 { value = value + 20; }
else if value >= 38 && value <= 47 { value = value + 10; }
else if value >= 58 && value <= 64 { value = value + 10; }
else if value >= 91 && value <= 96 { value = value + 10; }
str.push(char::from(value));
}
}
}
}
str
}
pub fn get_simple_random_from_characters_one_time(len: usize, characters: &str) -> String {
RandomGenerator::get_random_from_characters_one_time(len, characters, &mut None::<RNG>)
}
pub fn get_random_from_characters_one_time(len: usize, characters: &str, gen: &mut Option<impl RngCore>) -> String {
let mut str = String::with_capacity(len);
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
for _ in 0..len {
str.push_str(characters.chars().nth(rng.gen_range(0..characters.len() - 1)).unwrap().to_string().as_str());
}
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
for _ in 0..len {
str.push_str(characters.chars().nth(rng.gen_range(0..characters.len() - 1)).unwrap().to_string().as_str());
}
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
for _ in 0..len {
str.push_str(characters.chars().nth(rng.gen_range(0..characters.len() - 1)).unwrap().to_string().as_str());
}
}
}
}
str
}
pub fn get_simple_random_alphanumeric_one_time(len: usize) -> String {
RandomGenerator::get_random_alphanumeric_one_time(len, &mut None::<RNG>)
}
pub fn get_random_alphanumeric_one_time(len: usize, gen: &mut Option<impl RngCore>) -> String {
let mut str = String::with_capacity(len);
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
for _ in 0..len {
let mut value: u8 = rng.gen_range(48..122);
if value >= 58 && value <= 64 { value = value + 10; }
else if value >= 91 && value <= 96 { value = value + 10; }
str.push(char::from(value));
}
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
for _ in 0..len {
let mut value: u8 = rng.gen_range(48..122);
if value >= 58 && value <= 64 { value = value + 10; }
else if value >= 91 && value <= 96 { value = value + 10; }
str.push(char::from(value));
}
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
for _ in 0..len {
let mut value: u8 = rng.gen_range(48..122);
if value >= 58 && value <= 64 { value = value + 10; }
else if value >= 91 && value <= 96 { value = value + 10; }
str.push(char::from(value));
}
}
}
}
str
}
pub fn get_simple_random_password_one_time(len: usize) -> String {
RandomGenerator::get_random_password_one_time(len, &mut None::<RNG>)
}
pub fn get_random_password_one_time(len: usize, gen: &mut Option<impl RngCore>) -> String {
let mut str = String::with_capacity(len);
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
for _ in 0..len {
let value: u8 = rng.gen_range(33..122);
str.push(char::from(value));
}
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
for _ in 0..len {
let value: u8 = rng.gen_range(33..122);
str.push(char::from(value));
}
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
for _ in 0..len {
let value: u8 = rng.gen_range(33..122);
str.push(char::from(value));
}
}
}
}
str
}
#[cfg(feature = "names")]
pub fn get_simple_random_email_one_time(num_names: Option<usize>, num_numbers: Option<usize>, separator: Option<String>, domains: Vec<String>) -> String {
RandomGenerator::get_random_email_one_time(num_names, num_numbers, separator, domains, &mut None::<RNG>)
}
#[cfg(feature = "names")]
pub fn get_random_email_one_time(num_names: Option<usize>, num_numbers: Option<usize>, separator: Option<String>, domains: Vec<String>, gen: &mut Option<impl RngCore>) -> String {
let num_names = match num_names {
None => 2,
Some(n) => n
};
let num_numbers = match num_numbers {
None => 3,
Some(n) => n
};
let separator = match separator {
None => String::from(""),
Some(s) => s
};
let mut str = String::new();
let username = RandomGenerator::get_random_username_one_time(Some(num_names), Some(num_numbers), Some(separator.to_owned()), gen);
str.push_str(username.as_str());
str.push_str("@");
let num_domains = domains.len();
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
let pick_domain = rng.gen_range(0..num_domains);
str.push_str(domains.get(pick_domain).unwrap().as_str());
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
let pick_domain = rng.gen_range(0..num_domains);
str.push_str(domains.get(pick_domain).unwrap().as_str());
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
let pick_domain = rng.gen_range(0..num_domains);
str.push_str(domains.get(pick_domain).unwrap().as_str());
}
}
}
str
}
#[cfg(feature = "names")]
pub fn get_simple_random_username_one_time(num_names: Option<usize>, num_numbers: Option<usize>, separator: Option<String>) -> String {
RandomGenerator::get_random_username_one_time(num_names, num_numbers, separator, &mut None::<RNG>)
}
#[cfg(feature = "names")]
pub fn get_random_username_one_time(num_names: Option<usize>, num_numbers: Option<usize>, separator: Option<String>, gen: &mut Option<impl RngCore>) -> String {
let names_count = names::NAMES.len();
let num_names = match num_names {
None => 2,
Some(n) => n
};
let num_numbers = match num_numbers {
None => 5,
Some(n) => n
};
let separator = match separator {
None => String::from(""),
Some(s) => s
};
let mut str = String::new();
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
for counter in 0..num_names {
let value: usize = rng.gen_range(0..names_count);
if counter > 0 { str.push_str(separator.as_str()); }
str.push_str(names::NAMES[value].to_lowercase().as_str());
}
if num_numbers > 0 { str.push_str(separator.as_str()); }
for _ in 0..num_numbers {
let value: u8 = rng.gen_range(48..57);
str.push(char::from(value));
}
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
for counter in 0..num_names {
let value: usize = rng.gen_range(0..names_count);
if counter > 0 { str.push_str(separator.as_str()); }
str.push_str(names::NAMES[value].to_lowercase().as_str());
}
if num_numbers > 0 { str.push_str(separator.as_str()); }
for _ in 0..num_numbers {
let value: u8 = rng.gen_range(48..57);
str.push(char::from(value));
}
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
for counter in 0..num_names {
let value: usize = rng.gen_range(0..names_count);
if counter > 0 { str.push_str(separator.as_str()); }
str.push_str(names::NAMES[value].to_lowercase().as_str());
}
if num_numbers > 0 { str.push_str(separator.as_str()); }
for _ in 0..num_numbers {
let value: u8 = rng.gen_range(48..57);
str.push(char::from(value));
}
}
}
}
str
}
#[cfg(feature = "names")]
pub fn get_simple_random_name_one_time() -> String {
RandomGenerator::get_random_name_one_time(&mut None::<RNG>)
}
#[cfg(feature = "names")]
pub fn get_random_name_one_time(gen: &mut Option<impl RngCore>) -> String {
let names_count = names::NAMES.len();
let mut str = String::new();
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
let value: usize = rng.gen_range(0..names_count);
str.push_str(names::NAMES[value]);
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
let value: usize = rng.gen_range(0..names_count);
str.push_str(names::NAMES[value]);
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
let value: usize = rng.gen_range(0..names_count);
str.push_str(names::NAMES[value]);
}
}
};
str
}
#[cfg(feature = "names")]
pub fn get_simple_random_first_name_one_time() -> String {
RandomGenerator::get_random_first_name_one_time(&mut None::<RNG>)
}
#[cfg(feature = "first_names")]
pub fn get_random_first_name_one_time(gen: &mut Option<impl RngCore>) -> String {
let names_count = first_names::FIRSTNAMES.len();
let mut str = String::new();
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
let value: usize = rng.gen_range(0..names_count);
str.push_str(first_names::FIRSTNAMES[value].to_title_case().as_str());
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
let value: usize = rng.gen_range(0..names_count);
str.push_str(first_names::FIRSTNAMES[value].to_title_case().as_str());
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
let value: usize = rng.gen_range(0..names_count);
str.push_str(first_names::FIRSTNAMES[value].to_title_case().as_str());
}
}
}
str
}
#[cfg(feature = "names")]
pub fn get_simple_random_last_name_one_time() -> String {
RandomGenerator::get_random_last_name_one_time(&mut None::<RNG>)
}
#[cfg(feature = "last_names")]
pub fn get_random_last_name_one_time(gen: &mut Option<impl RngCore>) -> String {
let names_count = last_names::LASTNAMES.len();
let mut str = String::new();
#[cfg(all(feature = "constrained", not(feature = "default")))] {
match gen {
Some(rng) => {
let value: usize = rng.gen_range(0..names_count);
str.push_str(last_names::LASTNAMES[value].to_title_case().as_str());
},
None => panic!("No generator provided")
}
}
#[cfg(feature = "default")] {
match gen {
Some(rng) => {
let value: usize = rng.gen_range(0..names_count);
str.push_str(last_names::LASTNAMES[value].to_title_case().as_str());
},
None => {
let mut rng = rand::rngs::StdRng::from_entropy();
let value: usize = rng.gen_range(0..names_count);
str.push_str(last_names::LASTNAMES[value].to_title_case().as_str());
}
}
}
str
}
}
impl RandomGeneratorTemplate for RandomGenerator {
fn get_random_bytes(&mut self, len: usize) -> Vec<u8> {
let mut bytes = vec![];
for _ in 0..len {
let value: u8 = self.generator.gen_range(0..=255);
bytes.push(value)
}
bytes
}
fn get_random_usize(&mut self, min: usize, max: usize) -> usize {
self.generator.gen_range(min..=max)
}
fn get_random_u32(&mut self) -> u32 {
self.generator.next_u32()
}
fn get_random_u64(&mut self) -> u64 {
self.generator.next_u64()
}
fn get_random_string(&mut self, len: usize) -> String {
let mut str = String::with_capacity(len);
for _ in 0..len {
let mut value: u8 = self.generator.gen_range(33..122);
if value >= 33 && value <= 37 { value = value + 20; }
else if value >= 38 && value <= 47 { value = value + 10; }
else if value >= 58 && value <= 64 { value = value + 10; }
else if value >= 91 && value <= 96 { value = value + 10; }
str.push(char::from(value));
}
str
}
fn get_random_from_characters(&mut self, len: usize, characters: &str) -> String {
let mut str = String::with_capacity(len);
for _ in 0..len {
str.push_str(characters.chars().nth(self.generator.gen_range(0..characters.len() - 1)).unwrap().to_string().as_str());
}
str
}
fn get_random_alphanumeric(&mut self, len: usize) -> String {
let mut str = String::with_capacity(len);
for _ in 0..len {
let mut value: u8 = self.generator.gen_range(48..122);
if value >= 58 && value <= 64 { value = value + 10; }
else if value >= 91 && value <= 96 { value = value + 10; }
str.push(char::from(value));
}
str
}
fn generate_key(&mut self, len: usize) -> Vec<u8> {
let mut key = Vec::with_capacity(len);
for _ in 0..len { key.push(self.generator.gen()); }
key
}
}
pub trait RandomGeneratorTemplate {
fn get_random_bytes(&mut self, len: usize) -> Vec<u8>;
fn get_random_usize(&mut self, min: usize, max: usize) -> usize;
fn get_random_u32(&mut self) -> u32;
fn get_random_u64(&mut self) -> u64;
fn get_random_string(&mut self, len: usize) -> String;
fn get_random_from_characters(&mut self, len: usize, characters: &str) -> String;
fn get_random_alphanumeric(&mut self, len: usize) -> String;
fn generate_key(&mut self, len: usize) -> Vec<u8>;
}