|
6 | 6 | # -------------------------------------------------------- |
7 | 7 | # https://github.com/smartlegionlab |
8 | 8 | # -------------------------------------------------------- |
| 9 | +"""Random Data Generators.""" |
9 | 10 | import hashlib |
10 | 11 | import random |
11 | 12 | import string |
12 | 13 |
|
13 | 14 |
|
14 | | -class RandomStringMaster: |
| 15 | +class RandomLettersMaster: |
15 | 16 | letters = string.ascii_letters |
16 | 17 |
|
17 | 18 | @classmethod |
18 | 19 | def create(cls, length=10): |
19 | 20 | return ''.join((random.choice(cls.letters) for _ in range(length))) |
20 | 21 |
|
21 | 22 |
|
22 | | -class RandomNumberMaster: |
| 23 | +class RandomNumericMaster: |
23 | 24 | numbers = string.digits |
24 | 25 |
|
25 | 26 | @classmethod |
26 | 27 | def create(cls, length=10): |
27 | 28 | return ''.join((random.choice(cls.numbers) for _ in range(length))) |
28 | 29 |
|
29 | 30 |
|
30 | | -class RandomSymbolMaster: |
| 31 | +class RandomSymbolsMaster: |
31 | 32 | symbols = '@$!%*#?&-' |
32 | 33 |
|
33 | 34 | @classmethod |
34 | 35 | def create(cls, length=10): |
35 | 36 | return ''.join((random.choice(cls.symbols) for _ in range(length))) |
36 | 37 |
|
37 | 38 |
|
38 | | -class RandomPasswordMaster: |
39 | | - letters = string.ascii_letters |
40 | | - numbers = string.digits |
41 | | - symbols = '@$!%*#?&-' |
| 39 | +class HashMaster: |
| 40 | + @classmethod |
| 41 | + def create(cls, text: str): |
| 42 | + text = str(text) |
| 43 | + sha = hashlib.sha3_512(text.encode('utf-8')) |
| 44 | + new_hash = sha.hexdigest() |
| 45 | + return new_hash |
| 46 | + |
| 47 | + |
| 48 | +class RandomStringMaster: |
| 49 | + letters_master = RandomLettersMaster() |
| 50 | + numeric_master = RandomNumericMaster() |
| 51 | + symbols_master = RandomSymbolsMaster() |
| 52 | + hash_master = HashMaster() |
42 | 53 |
|
43 | 54 | @classmethod |
44 | | - def create(cls, length=10): |
45 | | - return ''.join((random.choice(cls.letters + cls.numbers + cls.symbols) for _ in range(length))) |
| 55 | + def create_string(cls, length=10): |
| 56 | + random_string = cls.letters_master.letters + cls.numeric_master.numbers + cls.symbols_master.symbols |
| 57 | + return ''.join((random.choice(random_string) for _ in range(length))) |
46 | 58 |
|
| 59 | + @classmethod |
| 60 | + def create_hash(cls, text): |
| 61 | + return cls.hash_master.create(text) |
47 | 62 |
|
48 | | -class RandomHashMaster: |
49 | 63 | @classmethod |
50 | | - def create(cls, text): |
51 | | - sha = hashlib.sha3_512(text.encode('utf-8')) |
52 | | - new_hash = sha.hexdigest() |
53 | | - return new_hash |
| 64 | + def create_numeric_code(cls, length): |
| 65 | + return cls.numeric_master.create(length) |
54 | 66 |
|
| 67 | + @classmethod |
| 68 | + def create_letters_code(cls, length): |
| 69 | + return cls.letters_master.create(length) |
55 | 70 |
|
56 | | -class RandomMaster: |
57 | | - string = RandomStringMaster() |
58 | | - number = RandomNumberMaster() |
59 | | - symbol = RandomSymbolMaster() |
60 | | - password = RandomPasswordMaster() |
61 | | - hash = RandomHashMaster() |
| 71 | + @classmethod |
| 72 | + def create_symbols_code(cls, length): |
| 73 | + return cls.symbols_master.create(length) |
62 | 74 |
|
63 | 75 | @classmethod |
64 | | - def create_code(cls, length=10, number_flag=False, string_flag=False, symbol_flag=False): |
| 76 | + def create_code(cls, length=10, numeric_flag=False, letters_flag=False, symbols_flag=False): |
65 | 77 | data = '' |
66 | | - if string_flag: |
67 | | - data += cls.string.letters |
68 | | - if number_flag: |
69 | | - data += cls.number.numbers |
70 | | - if symbol_flag: |
71 | | - data += cls.symbol.symbols |
72 | | - return ''.join((random.choice(data) for _ in range(length))) |
| 78 | + if letters_flag: |
| 79 | + data += cls.letters_master.letters |
| 80 | + if numeric_flag: |
| 81 | + data += cls.numeric_master.numbers |
| 82 | + if symbols_flag: |
| 83 | + data += cls.symbols_master.symbols |
| 84 | + if data: |
| 85 | + return ''.join((random.choice(data) for _ in range(length))) |
| 86 | + return None |
0 commit comments