1#![no_std]
2extern crate no_std_compat as std;
3
4use std::prelude::v1::*;
5
6#[cfg(feature = "words")]
7mod words;
8#[cfg(feature = "nouns")]
9mod nouns;
10#[cfg(feature = "adjectives")]
11mod adjectives;
12#[cfg(feature = "names")]
13mod names;
14#[cfg(feature = "first_names")]
15mod first_names;
16#[cfg(feature = "last_names")]
17mod last_names;
18
19#[cfg(any(feature = "core", feature = "send_only"))]
20pub use rand::rngs::StdRng;
21
22#[cfg(any(feature = "core"))]
23pub use rand::{
24 rngs::{ ThreadRng, OsRng }
25};
26
27
28#[cfg(feature = "custom")]
29pub use getrandom::register_custom_getrandom;
30
31
32#[cfg(any(feature = "first_names", feature = "last_names"))]
33use inflections::Inflect;
34
35pub use rand::{
36 Rng, SeedableRng, RngCore, CryptoRng, Error
37};
38
39#[cfg(feature = "custom")]
40#[derive(Clone)]
41pub struct CustomRng {
42 rng: std::sync::Arc<spin::mutex::Mutex<dyn RngCore>>
43}
44
45#[cfg(feature = "custom")]
46impl CustomRng {
47 pub fn new(rng: impl RngCore + 'static) -> Self {
48 Self {
49 rng: std::sync::Arc::new(spin::mutex::Mutex::new(rng))
50 }
51 }
52}
53
54#[cfg(feature = "custom")]
55impl RngCore for CustomRng {
56 fn next_u32(&mut self) -> u32 {
57 let mut rl = self.rng.lock();
58 rl.next_u32()
59 }
60
61 fn next_u64(&mut self) -> u64 {
62 let mut rl = self.rng.lock();
63 rl.next_u64()
64 }
65
66 fn fill_bytes(&mut self, dest: &mut [u8]) {
67 let mut rl = self.rng.lock();
68 rl.fill_bytes(dest)
69 }
70
71 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
72 let mut rl = self.rng.lock();
73 rl.try_fill_bytes(dest)
74 }
75}
76
77
78#[cfg(any(feature = "custom", feature = "custom_send_only"))]
79pub trait RngCrypto: RngCore + CryptoRng {}
80
81
82#[derive(Clone)]
83#[cfg(feature = "custom")]
84pub struct CustomCryptoRng {
85 rng: std::sync::Arc<spin::mutex::Mutex<dyn RngCrypto>>
86}
87
88#[cfg(feature = "custom")]
89impl CustomCryptoRng {
90 pub fn new(rng: impl RngCrypto + 'static) -> Self {
91 Self {
92 rng: std::sync::Arc::new(spin::mutex::Mutex::new(rng))
93 }
94 }
95}
96
97#[cfg(feature = "custom")]
98impl RngCrypto for CustomCryptoRng {}
99
100#[cfg(feature = "custom")]
101impl CryptoRng for CustomCryptoRng {}
102
103#[cfg(feature = "custom")]
104impl RngCore for CustomCryptoRng {
105 fn next_u32(&mut self) -> u32 {
106 let mut rl = self.rng.lock();
107 rl.next_u32()
108 }
109
110 fn next_u64(&mut self) -> u64 {
111 let mut rl = self.rng.lock();
112 rl.next_u64()
113 }
114
115 fn fill_bytes(&mut self, dest: &mut [u8]) {
116 let mut rl = self.rng.lock();
117 rl.fill_bytes(dest)
118 }
119
120 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
121 let mut rl = self.rng.lock();
122 rl.try_fill_bytes(dest)
123 }
124}
125
126
127
128#[derive(Clone)]
129pub enum RNG {
130 #[cfg(any(feature = "core", feature = "send_only"))]
131 Std(StdRng),
132 #[cfg(feature = "core")]
133 Thread(ThreadRng),
134 #[cfg(feature = "core")]
135 OS(OsRng),
136 #[cfg(feature = "custom")]
137 Custom(CustomRng),
138 #[cfg(feature = "custom")]
139 CustomRaw(std::sync::Arc<spin::mutex::Mutex<dyn RngCore>>),
140 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
141 CustomRawSend(std::sync::Arc<spin::mutex::Mutex<dyn RngCore + Send>>),
142 #[cfg(feature = "custom")]
143 CustomCrypto(CustomCryptoRng),
144 #[cfg(feature = "custom")]
145 CustomCryptoRaw(std::sync::Arc<spin::mutex::Mutex<dyn RngCrypto>>),
146 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
147 CustomCryptoRawSend(std::sync::Arc<spin::mutex::Mutex<dyn RngCrypto + Send>>),
148}
149
150impl RngCore for RNG {
151 fn next_u32(&mut self) -> u32 {
152 match self {
153 #[cfg(any(feature = "core", feature = "send_only"))]
154 RNG::Std(r) => r.next_u32(),
155 #[cfg(feature = "core")]
156 RNG::Thread(r) => r.next_u32(),
157 #[cfg(feature = "core")]
158 RNG::OS(r) => r.next_u32(),
159 #[cfg(feature = "custom")]
160 RNG::Custom(r) => r.next_u32(),
161 #[cfg(feature = "custom")]
162 RNG::CustomRaw(r) => {
163 let mut rl = r.lock();
164 rl.next_u32()
165 },
166 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
167 RNG::CustomRawSend(r) => {
168 let mut rl = r.lock();
169 rl.next_u32()
170 },
171 #[cfg(feature = "custom")]
172 RNG::CustomCrypto(r) => r.next_u32(),
173 #[cfg(feature = "custom")]
174 RNG::CustomCryptoRaw(r) => {
175 let mut rl = r.lock();
176 rl.next_u32()
177 },
178 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
179 RNG::CustomCryptoRawSend(r) => {
180 let mut rl = r.lock();
181 rl.next_u32()
182 },
183 _ => panic!("No known random generator")
184 }
185 }
186
187 fn next_u64(&mut self) -> u64 {
188 match self {
189 #[cfg(any(feature = "core", feature = "send_only"))]
190 RNG::Std(r) => r.next_u64(),
191 #[cfg(feature = "core")]
192 RNG::Thread(r) => r.next_u64(),
193 #[cfg(feature = "core")]
194 RNG::OS(r) => r.next_u64(),
195 #[cfg(feature = "custom")]
196 RNG::Custom(r) => r.next_u64(),
197 #[cfg(feature = "custom")]
198 RNG::CustomRaw(r) => {
199 let mut rl = r.lock();
200 rl.next_u64()
201 },
202 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
203 RNG::CustomRawSend(r) => {
204 let mut rl = r.lock();
205 rl.next_u64()
206 },
207 #[cfg(feature = "custom")]
208 RNG::CustomCrypto(r) => r.next_u64(),
209 #[cfg(feature = "custom")]
210 RNG::CustomCryptoRaw(r) => {
211 let mut rl = r.lock();
212 rl.next_u64()
213 },
214 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
215 RNG::CustomCryptoRawSend(r) => {
216 let mut rl = r.lock();
217 rl.next_u64()
218 },
219 _ => panic!("No known random generator")
220 }
221 }
222
223 fn fill_bytes(&mut self, dest: &mut [u8]) {
224 match self {
225 #[cfg(any(feature = "core", feature = "send_only"))]
226 RNG::Std(r) => r.fill_bytes(dest),
227 #[cfg(feature = "core")]
228 RNG::Thread(r) => r.fill_bytes(dest),
229 #[cfg(feature = "core")]
230 RNG::OS(r) => r.fill_bytes(dest),
231 #[cfg(feature = "custom")]
232 RNG::Custom(r) => r.fill_bytes(dest),
233 #[cfg(feature = "custom")]
234 RNG::CustomRaw(r) => {
235 let mut rl = r.lock();
236 rl.fill_bytes(dest)
237 },
238 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
239 RNG::CustomRawSend(r) => {
240 let mut rl = r.lock();
241 rl.fill_bytes(dest)
242 },
243 #[cfg(feature = "custom")]
244 RNG::CustomCrypto(r) => r.fill_bytes(dest),
245 #[cfg(feature = "custom")]
246 RNG::CustomCryptoRaw(r) => {
247 let mut rl = r.lock();
248 rl.fill_bytes(dest)
249 },
250 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
251 RNG::CustomCryptoRawSend(r) => {
252 let mut rl = r.lock();
253 rl.fill_bytes(dest)
254 },
255 _ => panic!("No known random generator")
256 }
257 }
258
259 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
260 match self {
261 #[cfg(any(feature = "core", feature = "send_only"))]
262 RNG::Std(r) => r.try_fill_bytes(dest),
263 #[cfg(feature = "core")]
264 RNG::Thread(r) => r.try_fill_bytes(dest),
265 #[cfg(feature = "core")]
266 RNG::OS(r) => r.try_fill_bytes(dest),
267 #[cfg(feature = "custom")]
268 RNG::Custom(r) => r.try_fill_bytes(dest),
269 #[cfg(feature = "custom")]
270 RNG::CustomRaw(r) => {
271 let mut rl = r.lock();
272 rl.try_fill_bytes(dest)
273 },
274 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
275 RNG::CustomRawSend(r) => {
276 let mut rl = r.lock();
277 rl.try_fill_bytes(dest)
278 },
279 #[cfg(feature = "custom")]
280 RNG::CustomCrypto(r) => r.try_fill_bytes(dest),
281 #[cfg(feature = "custom")]
282 RNG::CustomCryptoRaw(r) => {
283 let mut rl = r.lock();
284 rl.try_fill_bytes(dest)
285 },
286 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
287 RNG::CustomCryptoRawSend(r) => {
288 let mut rl = r.lock();
289 rl.try_fill_bytes(dest)
290 },
291 _ => panic!("No known random generator")
292 }
293 }
294}
295
296
297
298
299#[derive(Clone)]
300pub enum CryptoRNG {
301 #[cfg(any(feature = "core", feature = "send_only"))]
302 Std(StdRng),
303 #[cfg(feature = "core")]
304 Thread(ThreadRng),
305 #[cfg(feature = "core")]
306 OS(OsRng),
307 #[cfg(feature = "custom")]
308 CustomCrypto(CustomCryptoRng),
309 #[cfg(feature = "custom")]
310 CustomCryptoRaw(std::sync::Arc<spin::mutex::Mutex<dyn RngCrypto>>),
311 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
312 CustomCryptoRawSend(std::sync::Arc<spin::mutex::Mutex<dyn RngCrypto + Send>>)
313}
314
315impl CryptoRng for CryptoRNG {}
316
317impl RngCore for CryptoRNG {
318 fn next_u32(&mut self) -> u32 {
319 match self {
320 #[cfg(any(feature = "core", feature = "send_only"))]
321 CryptoRNG::Std(r) => r.next_u32(),
322 #[cfg(feature = "core")]
323 CryptoRNG::Thread(r) => r.next_u32(),
324 #[cfg(feature = "core")]
325 CryptoRNG::OS(r) => r.next_u32(),
326 #[cfg(feature = "custom")]
327 CryptoRNG::CustomCrypto(r) => r.next_u32(),
328 #[cfg(feature = "custom")]
329 CryptoRNG::CustomCryptoRaw(r) => {
330 let mut rl = r.lock();
331 rl.next_u32()
332 },
333 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
334 CryptoRNG::CustomCryptoRawSend(r) => {
335 let mut rl = r.lock();
336 rl.next_u32()
337 },
338 _ => panic!("No known random generator")
339 }
340 }
341
342 fn next_u64(&mut self) -> u64 {
343 match self {
344 #[cfg(any(feature = "core", feature = "send_only"))]
345 CryptoRNG::Std(r) => r.next_u64(),
346 #[cfg(feature = "core")]
347 CryptoRNG::Thread(r) => r.next_u64(),
348 #[cfg(feature = "core")]
349 CryptoRNG::OS(r) => r.next_u64(),
350 #[cfg(feature = "custom")]
351 CryptoRNG::CustomCrypto(r) => r.next_u64(),
352 #[cfg(feature = "custom")]
353 CryptoRNG::CustomCryptoRaw(r) => {
354 let mut rl = r.lock();
355 rl.next_u64()
356 },
357 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
358 CryptoRNG::CustomCryptoRawSend(r) => {
359 let mut rl = r.lock();
360 rl.next_u64()
361 },
362 _ => panic!("No known random generator")
363 }
364 }
365
366 fn fill_bytes(&mut self, dest: &mut [u8]) {
367 match self {
368 #[cfg(any(feature = "core", feature = "send_only"))]
369 CryptoRNG::Std(r) => r.fill_bytes(dest),
370 #[cfg(feature = "core")]
371 CryptoRNG::Thread(r) => r.fill_bytes(dest),
372 #[cfg(feature = "core")]
373 CryptoRNG::OS(r) => r.fill_bytes(dest),
374 #[cfg(feature = "custom")]
375 CryptoRNG::CustomCrypto(r) => r.fill_bytes(dest),
376 #[cfg(feature = "custom")]
377 CryptoRNG::CustomCryptoRaw(r) => {
378 let mut rl = r.lock();
379 rl.fill_bytes(dest)
380 },
381 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
382 CryptoRNG::CustomCryptoRawSend(r) => {
383 let mut rl = r.lock();
384 rl.fill_bytes(dest)
385 },
386 _ => panic!("No known random generator")
387 }
388 }
389
390 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
391 match self {
392 #[cfg(any(feature = "core", feature = "send_only"))]
393 CryptoRNG::Std(r) => r.try_fill_bytes(dest),
394 #[cfg(feature = "core")]
395 CryptoRNG::Thread(r) => r.try_fill_bytes(dest),
396 #[cfg(feature = "core")]
397 CryptoRNG::OS(r) => r.try_fill_bytes(dest),
398 #[cfg(feature = "custom")]
399 CryptoRNG::CustomCrypto(r) => r.try_fill_bytes(dest),
400 #[cfg(feature = "custom")]
401 CryptoRNG::CustomCryptoRaw(r) => {
402 let mut rl = r.lock();
403 rl.try_fill_bytes(dest)
404 },
405 #[cfg(any(feature = "custom", feature = "custom_send_only"))]
406 CryptoRNG::CustomCryptoRawSend(r) => {
407 let mut rl = r.lock();
408 rl.try_fill_bytes(dest)
409 },
410 _ => panic!("No known random generator")
411 }
412 }
413}
414
415
416
417
418
419
420#[derive(Clone)]
421pub struct RandomGenerator {
422 generator: RNG
423}
424
425#[cfg(any(feature = "constrained", feature = "core", feature = "send_only"))]
426impl RandomGenerator {
427 pub fn new(gen: Option<RNG>) -> Self {
428 let generator = {
429 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
430 match gen {
431 Some(g) => g,
432 None => panic!("No generator provided")
433 }
434 }
435 #[cfg(any(feature = "core", feature = "send_only"))] {
436 gen.unwrap_or_else(|| RNG::Std(StdRng::from_entropy()))
437 }
438 };
439
440 Self {
441 generator
442 }
443 }
444
445 pub fn generate_simple_key_one_time(len: usize) -> Vec<u8> {
446 RandomGenerator::generate_key_one_time(len, &mut None::<RNG>)
447 }
448
449 pub fn generate_key_one_time(len: usize, gen: &mut Option<impl RngCore>) -> Vec<u8> {
450 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
451 match gen {
452 Some(rng) => {
453 let mut key = Vec::with_capacity(len);
454 for _ in 0..len { key.push(rng.gen()); }
455 key
456 },
457 None => panic!("No generator provided")
458 }
459 }
460 #[cfg(any(feature = "core", feature = "send_only"))] {
461 match gen {
462 Some(rng) => {
463 let mut key = Vec::with_capacity(len);
464 for _ in 0..len { key.push(rng.gen()); }
465 key
466 },
467 None => {
468 let mut rng = StdRng::from_entropy();
469 let mut key = Vec::with_capacity(len);
470 for _ in 0..len { key.push(rng.gen()); }
471 key
472 }
473 }
474 }
475 }
476
477 pub fn get_simple_random_bytes_one_time(len: usize, gen: &mut Option<impl RngCore>) -> Vec<u8> {
478 RandomGenerator::get_random_bytes_one_time(len, gen)
479 }
480
481 pub fn get_random_bytes_one_time(len: usize, gen: &mut Option<impl RngCore>) -> Vec<u8> {
482 let mut bytes = vec![];
483 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
484 match gen {
485 Some(rng) => {
486 for _ in 0..len {
487 let value: u8 = rng.gen_range(0..=255);
488 bytes.push(value)
489 }
490 },
491 None => panic!("No generator provided")
492 }
493 }
494 #[cfg(any(feature = "core", feature = "send_only"))] {
495 match gen {
496 Some(rng) => {
497 for _ in 0..len {
498 let value: u8 = rng.gen_range(0..=255);
499 bytes.push(value)
500 }
501 },
502 None => {
503 let mut rng = StdRng::from_entropy();
504 for _ in 0..len {
505 let value: u8 = rng.gen_range(0..=255);
506 bytes.push(value)
507 }
508 }
509 }
510 }
511 bytes
512 }
513
514 pub fn get_simple_random_usize_one_time(min: usize, max: usize) -> usize {
515 RandomGenerator::get_random_usize_one_time(min, max, &mut None::<RNG>)
516 }
517
518 pub fn get_random_usize_one_time(min: usize, max: usize, gen: &mut Option<impl RngCore>) -> usize {
519 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
520 match gen {
521 Some(rng) => {
522 rng.gen_range(min..=max)
523 },
524 None => panic!("No generator provided")
525 }
526 }
527 #[cfg(any(feature = "core", feature = "send_only"))] {
528 match gen {
529 Some(rng) => {
530 rng.gen_range(min..=max)
531 },
532 None => {
533 let mut rng = StdRng::from_entropy();
534 rng.gen_range(min..=max)
535 }
536 }
537 }
538 }
539
540 pub fn get_simple_random_string_one_time(len: usize, gen: &mut Option<impl RngCore>) -> String {
541 RandomGenerator::get_random_string_one_time(len, gen)
542 }
543
544 pub fn get_random_string_one_time(len: usize, gen: &mut Option<impl RngCore>) -> String {
545 let mut str = String::with_capacity(len);
546 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
547 match gen {
548 Some(rng) => {
549 for _ in 0..len {
550 let mut value: u8 = rng.gen_range(33..122);
551 if value >= 33 && value <= 37 { value = value + 20; }
552 else if value >= 38 && value <= 47 { value = value + 10; }
553 else if value >= 58 && value <= 64 { value = value + 10; }
554 else if value >= 91 && value <= 96 { value = value + 10; }
555 str.push(char::from(value));
556 }
557 },
558 None => panic!("No generator provided")
559 }
560 }
561 #[cfg(any(feature = "core", feature = "send_only"))] {
562 match gen {
563 Some(rng) => {
564 for _ in 0..len {
565 let mut value: u8 = rng.gen_range(33..122);
566 if value >= 33 && value <= 37 { value = value + 20; }
567 else if value >= 38 && value <= 47 { value = value + 10; }
568 else if value >= 58 && value <= 64 { value = value + 10; }
569 else if value >= 91 && value <= 96 { value = value + 10; }
570 str.push(char::from(value));
571 }
572 },
573 None => {
574 let mut rng = StdRng::from_entropy();
575 for _ in 0..len {
576 let mut value: u8 = rng.gen_range(33..122);
577 if value >= 33 && value <= 37 { value = value + 20; }
578 else if value >= 38 && value <= 47 { value = value + 10; }
579 else if value >= 58 && value <= 64 { value = value + 10; }
580 else if value >= 91 && value <= 96 { value = value + 10; }
581 str.push(char::from(value));
582 }
583 }
584 }
585 }
586 str
587 }
588
589 pub fn get_simple_random_from_characters_one_time(len: usize, characters: &str) -> String {
590 RandomGenerator::get_random_from_characters_one_time(len, characters, &mut None::<RNG>)
591 }
592
593 pub fn get_random_from_characters_one_time(len: usize, characters: &str, gen: &mut Option<impl RngCore>) -> String {
594 let mut str = String::with_capacity(len);
595
596 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
597 match gen {
598 Some(rng) => {
599 for _ in 0..len {
600 str.push_str(characters.chars().nth(rng.gen_range(0..characters.len() - 1)).unwrap().to_string().as_str());
601 }
602 },
603 None => panic!("No generator provided")
604 }
605 }
606 #[cfg(any(feature = "core", feature = "send_only"))] {
607 match gen {
608 Some(rng) => {
609 for _ in 0..len {
610 str.push_str(characters.chars().nth(rng.gen_range(0..characters.len() - 1)).unwrap().to_string().as_str());
611 }
612 },
613 None => {
614 let mut rng = StdRng::from_entropy();
615 for _ in 0..len {
616 str.push_str(characters.chars().nth(rng.gen_range(0..characters.len() - 1)).unwrap().to_string().as_str());
617 }
618 }
619 }
620 }
621
622 str
623 }
624
625 pub fn get_simple_random_alphanumeric_one_time(len: usize) -> String {
626 RandomGenerator::get_random_alphanumeric_one_time(len, &mut None::<RNG>)
627 }
628
629 pub fn get_random_alphanumeric_one_time(len: usize, gen: &mut Option<impl RngCore>) -> String {
630 let mut str = String::with_capacity(len);
631 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
632 match gen {
633 Some(rng) => {
634 for _ in 0..len {
635 let mut value: u8 = rng.gen_range(48..122);
636 if value >= 58 && value <= 64 { value = value + 10; }
637 else if value >= 91 && value <= 96 { value = value + 10; }
638 str.push(char::from(value));
639 }
640 },
641 None => panic!("No generator provided")
642 }
643 }
644 #[cfg(any(feature = "core", feature = "send_only"))] {
645 match gen {
646 Some(rng) => {
647 for _ in 0..len {
648 let mut value: u8 = rng.gen_range(48..122);
649 if value >= 58 && value <= 64 { value = value + 10; }
650 else if value >= 91 && value <= 96 { value = value + 10; }
651 str.push(char::from(value));
652 }
653 },
654 None => {
655 let mut rng = StdRng::from_entropy();
656 for _ in 0..len {
657 let mut value: u8 = rng.gen_range(48..122);
658 if value >= 58 && value <= 64 { value = value + 10; }
659 else if value >= 91 && value <= 96 { value = value + 10; }
660 str.push(char::from(value));
661 }
662 }
663 }
664 }
665 str
666 }
667
668 pub fn get_simple_random_password_one_time(len: usize) -> String {
669 RandomGenerator::get_random_password_one_time(len, &mut None::<RNG>)
670 }
671
672 pub fn get_random_password_one_time(len: usize, gen: &mut Option<impl RngCore>) -> String {
673 let mut str = String::with_capacity(len);
674 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
675 match gen {
676 Some(rng) => {
677 for _ in 0..len {
678 let value: u8 = rng.gen_range(33..122);
679 str.push(char::from(value));
680 }
681 },
682 None => panic!("No generator provided")
683 }
684 }
685 #[cfg(any(feature = "core", feature = "send_only"))] {
686 match gen {
687 Some(rng) => {
688 for _ in 0..len {
689 let value: u8 = rng.gen_range(33..122);
690 str.push(char::from(value));
691 }
692 },
693 None => {
694 let mut rng = StdRng::from_entropy();
695 for _ in 0..len {
696 let value: u8 = rng.gen_range(33..122);
697 str.push(char::from(value));
698 }
699 }
700 }
701 }
702 str
703 }
704
705 #[cfg(feature = "names")]
706 pub fn get_simple_random_email_one_time(num_names: Option<usize>, num_numbers: Option<usize>, separator: Option<String>, domains: Vec<String>) -> String {
707 RandomGenerator::get_random_email_one_time(num_names, num_numbers, separator, domains, &mut None::<RNG>)
708 }
709
710 #[cfg(feature = "names")]
711 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 {
712 let num_names = num_names.unwrap_or_else(|| 2);
713
714 let num_numbers = num_numbers.unwrap_or_else(|| 3);
715
716 let separator = separator.unwrap_or_else(|| String::from(""));
717
718 let mut str = String::new();
719
720 let username = RandomGenerator::get_random_username_one_time(Some(num_names), Some(num_numbers), Some(separator.to_owned()), gen);
721
722 str.push_str(username.as_str());
723 str.push_str("@");
724
725 let num_domains = domains.len();
726
727 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
728 match gen {
729 Some(rng) => {
730 let pick_domain = rng.gen_range(0..num_domains);
731 str.push_str(domains.get(pick_domain).unwrap().as_str());
732 },
733 None => panic!("No generator provided")
734 }
735 }
736 #[cfg(any(feature = "core", feature = "send_only"))] {
737 match gen {
738 Some(rng) => {
739 let pick_domain = rng.gen_range(0..num_domains);
740 str.push_str(domains.get(pick_domain).unwrap().as_str());
741 },
742 None => {
743 let mut rng = StdRng::from_entropy();
744 let pick_domain = rng.gen_range(0..num_domains);
745 str.push_str(domains.get(pick_domain).unwrap().as_str());
746 }
747 }
748 }
749
750 str
751 }
752
753 #[cfg(feature = "names")]
754 pub fn get_simple_random_username_one_time(num_names: Option<usize>, num_numbers: Option<usize>, separator: Option<String>) -> String {
755 RandomGenerator::get_random_username_one_time(num_names, num_numbers, separator, &mut None::<RNG>)
756 }
757
758 #[cfg(feature = "names")]
759 pub fn get_random_username_one_time(num_names: Option<usize>, num_numbers: Option<usize>, separator: Option<String>, gen: &mut Option<impl RngCore>) -> String {
760 let names_count = names::NAMES.len();
761
762 let num_names = num_names.unwrap_or_else(|| 2);
763
764 let num_numbers = num_numbers.unwrap_or_else(|| 5);
765
766 let separator = separator.unwrap_or_else(|| String::from(""));
767
768 let mut str = String::new();
769
770 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
771 match gen {
772 Some(rng) => {
773 for counter in 0..num_names {
774 let value: usize = rng.gen_range(0..names_count);
775 if counter > 0 { str.push_str(separator.as_str()); }
776 str.push_str(names::NAMES[value].to_lowercase().as_str());
777 }
778
779 if num_numbers > 0 { str.push_str(separator.as_str()); }
780
781 for _ in 0..num_numbers {
782 let value: u8 = rng.gen_range(48..57);
783 str.push(char::from(value));
784 }
785 },
786 None => panic!("No generator provided")
787 }
788 }
789 #[cfg(any(feature = "core", feature = "send_only"))] {
790 match gen {
791 Some(rng) => {
792 for counter in 0..num_names {
793 let value: usize = rng.gen_range(0..names_count);
794 if counter > 0 { str.push_str(separator.as_str()); }
795 str.push_str(names::NAMES[value].to_lowercase().as_str());
796 }
797
798 if num_numbers > 0 { str.push_str(separator.as_str()); }
799
800 for _ in 0..num_numbers {
801 let value: u8 = rng.gen_range(48..57);
802 str.push(char::from(value));
803 }
804 },
805 None => {
806 let mut rng = StdRng::from_entropy();
807 for counter in 0..num_names {
808 let value: usize = rng.gen_range(0..names_count);
809 if counter > 0 { str.push_str(separator.as_str()); }
810 str.push_str(names::NAMES[value].to_lowercase().as_str());
811 }
812
813 if num_numbers > 0 { str.push_str(separator.as_str()); }
814
815 for _ in 0..num_numbers {
816 let value: u8 = rng.gen_range(48..57);
817 str.push(char::from(value));
818 }
819 }
820 }
821 }
822
823 str
824 }
825
826 #[cfg(feature = "names")]
827 pub fn get_simple_random_name_one_time() -> String {
828 RandomGenerator::get_random_name_one_time(&mut None::<RNG>)
829 }
830
831 #[cfg(feature = "names")]
832 pub fn get_random_name_one_time(gen: &mut Option<impl RngCore>) -> String {
833 let names_count = names::NAMES.len();
834
835 let mut str = String::new();
836
837 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
838 match gen {
839 Some(rng) => {
840 let value: usize = rng.gen_range(0..names_count);
841 str.push_str(names::NAMES[value]);
842 },
843 None => panic!("No generator provided")
844 }
845 }
846 #[cfg(any(feature = "core", feature = "send_only"))] {
847 match gen {
848 Some(rng) => {
849 let value: usize = rng.gen_range(0..names_count);
850 str.push_str(names::NAMES[value]);
851 },
852 None => {
853 let mut rng = StdRng::from_entropy();
854 let value: usize = rng.gen_range(0..names_count);
855 str.push_str(names::NAMES[value]);
856 }
857 }
858 };
859 str
860 }
861
862 #[cfg(feature = "names")]
863 pub fn get_simple_random_first_name_one_time() -> String {
864 RandomGenerator::get_random_first_name_one_time(&mut None::<RNG>)
865 }
866
867 #[cfg(feature = "first_names")]
868 pub fn get_random_first_name_one_time(gen: &mut Option<impl RngCore>) -> String {
869 let names_count = first_names::FIRSTNAMES.len();
870
871 let mut str = String::new();
872
873 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
874 match gen {
875 Some(rng) => {
876 let value: usize = rng.gen_range(0..names_count);
877 str.push_str(first_names::FIRSTNAMES[value].to_title_case().as_str());
878 },
879 None => panic!("No generator provided")
880 }
881 }
882 #[cfg(any(feature = "core", feature = "send_only"))] {
883 match gen {
884 Some(rng) => {
885 let value: usize = rng.gen_range(0..names_count);
886 str.push_str(first_names::FIRSTNAMES[value].to_title_case().as_str());
887 },
888 None => {
889 let mut rng = StdRng::from_entropy();
890 let value: usize = rng.gen_range(0..names_count);
891 str.push_str(first_names::FIRSTNAMES[value].to_title_case().as_str());
892 }
893 }
894 }
895
896 str
897 }
898
899 #[cfg(feature = "names")]
900 pub fn get_simple_random_last_name_one_time() -> String {
901 RandomGenerator::get_random_last_name_one_time(&mut None::<RNG>)
902 }
903
904 #[cfg(feature = "last_names")]
905 pub fn get_random_last_name_one_time(gen: &mut Option<impl RngCore>) -> String {
906 let names_count = last_names::LASTNAMES.len();
907
908 let mut str = String::new();
909
910 #[cfg(all(feature = "constrained", all(not(feature = "core"), not(feature = "send_only"))))] {
911 match gen {
912 Some(rng) => {
913 let value: usize = rng.gen_range(0..names_count);
914 str.push_str(last_names::LASTNAMES[value].to_title_case().as_str());
915 },
916 None => panic!("No generator provided")
917 }
918 }
919 #[cfg(any(feature = "core", feature = "send_only"))] {
920 match gen {
921 Some(rng) => {
922 let value: usize = rng.gen_range(0..names_count);
923 str.push_str(last_names::LASTNAMES[value].to_title_case().as_str());
924 },
925 None => {
926 let mut rng = StdRng::from_entropy();
927 let value: usize = rng.gen_range(0..names_count);
928 str.push_str(last_names::LASTNAMES[value].to_title_case().as_str());
929 }
930 }
931 }
932
933 str
934 }
935}
936
937
938impl RandomGeneratorTemplate for RandomGenerator {
939 fn get_random_bytes(&mut self, len: usize) -> Vec<u8> {
940 let mut bytes = vec![];
941 for _ in 0..len {
942 let value: u8 = self.generator.gen_range(0..=255);
943 bytes.push(value)
944 }
945 bytes
946 }
947
948 fn get_random_usize(&mut self, min: usize, max: usize) -> usize {
949 self.generator.gen_range(min..=max)
950 }
951
952 fn get_random_u32(&mut self) -> u32 {
953 self.generator.next_u32()
954 }
955
956 fn get_random_u64(&mut self) -> u64 {
957 self.generator.next_u64()
958 }
959
960 fn get_random_string(&mut self, len: usize) -> String {
961 let mut str = String::with_capacity(len);
962 for _ in 0..len {
963 let mut value: u8 = self.generator.gen_range(33..122);
964 if value >= 33 && value <= 37 { value = value + 20; }
965 else if value >= 38 && value <= 47 { value = value + 10; }
966 else if value >= 58 && value <= 64 { value = value + 10; }
967 else if value >= 91 && value <= 96 { value = value + 10; }
968 str.push(char::from(value));
969 }
970 str
971 }
972
973 fn get_random_from_characters(&mut self, len: usize, characters: &str) -> String {
974 let mut str = String::with_capacity(len);
975
976 for _ in 0..len {
977 str.push_str(characters.chars().nth(self.generator.gen_range(0..characters.len() - 1)).unwrap().to_string().as_str());
978 }
979
980 str
981 }
982
983 fn get_random_alphanumeric(&mut self, len: usize) -> String {
984 let mut str = String::with_capacity(len);
985 for _ in 0..len {
986 let mut value: u8 = self.generator.gen_range(48..122);
987 if value >= 58 && value <= 64 { value = value + 10; }
988 else if value >= 91 && value <= 96 { value = value + 10; }
989 str.push(char::from(value));
990 }
991 str
992 }
993
994 fn generate_key(&mut self, len: usize) -> Vec<u8> {
995 let mut key = Vec::with_capacity(len);
996 for _ in 0..len { key.push(self.generator.gen()); }
997 key
998 }
999}
1000
1001
1002pub trait RandomGeneratorTemplate {
1003 fn get_random_bytes(&mut self, len: usize) -> Vec<u8>;
1004 fn get_random_usize(&mut self, min: usize, max: usize) -> usize;
1005 fn get_random_u32(&mut self) -> u32;
1006 fn get_random_u64(&mut self) -> u64;
1007 fn get_random_string(&mut self, len: usize) -> String;
1008 fn get_random_from_characters(&mut self, len: usize, characters: &str) -> String;
1009 fn get_random_alphanumeric(&mut self, len: usize) -> String;
1010 fn generate_key(&mut self, len: usize) -> Vec<u8>;
1011}
1012
1013
1014