|
9 | 9 | """Random Data Generators.""" |
10 | 10 | import hashlib |
11 | 11 | import os |
| 12 | +import random |
12 | 13 | import re |
13 | 14 | import secrets |
14 | 15 | import string |
@@ -159,6 +160,25 @@ def generate(cls, length: int = 10) -> str: |
159 | 160 | return ''.join(result) |
160 | 161 |
|
161 | 162 |
|
| 163 | +class BasePasswordGenerator: |
| 164 | + letters = string.ascii_letters |
| 165 | + digits = string.digits |
| 166 | + symbols = '!@#$%&^_' |
| 167 | + |
| 168 | + @classmethod |
| 169 | + def generate(cls, length=10): |
| 170 | + """ |
| 171 | + Generates a random password of the specified length. |
| 172 | +
|
| 173 | + :param length: Length of the password (default is 10). |
| 174 | + :return: A randomly generated password as a string. |
| 175 | + """ |
| 176 | + if length < 4: |
| 177 | + raise ValueError("The length must be at least 3.") |
| 178 | + symbols_string = cls.letters + cls.digits + cls.symbols |
| 179 | + return ''.join((random.choice(symbols_string) for _ in range(length))) |
| 180 | + |
| 181 | + |
162 | 182 | class PasswordGenerator: |
163 | 183 | upper_letters = string.ascii_uppercase |
164 | 184 | lower_letters = string.ascii_lowercase |
@@ -191,6 +211,50 @@ def generate(cls, length: int = 10) -> str: |
191 | 211 | return ''.join(result) |
192 | 212 |
|
193 | 213 |
|
| 214 | +class SmartPasswordGenerator: |
| 215 | + @classmethod |
| 216 | + def generate(cls, seed: str = '', length=15, size: int = 32) -> str: |
| 217 | + """ |
| 218 | + Generates a "smart" password using the specified seed. |
| 219 | +
|
| 220 | + :param seed: Seed for generation (default is an empty string). |
| 221 | + :param length: Length of the password (default is 15). |
| 222 | + :param size: Size of the seed for generation (default is 32). |
| 223 | + :return: A generated "smart" password as a string. |
| 224 | + """ |
| 225 | + if length < 4: |
| 226 | + raise ValueError("The length cannot be less than 4.") |
| 227 | + if not seed: |
| 228 | + seed = cls.get_seed(size) |
| 229 | + cls._set_seed(seed) |
| 230 | + password = BasePasswordGenerator.generate(length) |
| 231 | + seed = str(cls.get_seed()) |
| 232 | + cls._set_seed(seed) |
| 233 | + return password |
| 234 | + |
| 235 | + @classmethod |
| 236 | + def _set_seed(cls, seed): |
| 237 | + """ |
| 238 | + Sets the seed for the random number generator. |
| 239 | +
|
| 240 | + :param seed: Seed to set. |
| 241 | + :return: The set seed as a string. |
| 242 | + """ |
| 243 | + seed = str(seed) |
| 244 | + random.seed(seed) |
| 245 | + return seed |
| 246 | + |
| 247 | + @classmethod |
| 248 | + def get_seed(cls, size=32) -> bytes: |
| 249 | + """ |
| 250 | + Generates a seed of the specified size. |
| 251 | +
|
| 252 | + :param size: Size of the seed (default is 32). |
| 253 | + :return: Generated seed as bytes. |
| 254 | + """ |
| 255 | + return UrandomGenerator.generate(size=size) |
| 256 | + |
| 257 | + |
194 | 258 | class RandomDataGenerator: |
195 | 259 | @staticmethod |
196 | 260 | def generate_random_letters(length: int) -> str: |
@@ -262,6 +326,27 @@ def randomize_text(text: str) -> str: |
262 | 326 | """ |
263 | 327 | return TextRandomizer.randomize(text) |
264 | 328 |
|
| 329 | + @staticmethod |
| 330 | + def generate_base_password(length: int = 10) -> str: |
| 331 | + """ |
| 332 | + Generates a base password using the BasePasswordGenerator class. |
| 333 | +
|
| 334 | + :param length: Length of the password (default is 10). |
| 335 | + :return: A generated base password as a string. |
| 336 | + """ |
| 337 | + return BasePasswordGenerator.generate(length) |
| 338 | + |
| 339 | + @staticmethod |
| 340 | + def generate_smart_password(seed: str, length: int = 10) -> str: |
| 341 | + """ |
| 342 | + Generates a "smart" password using the specified seed. |
| 343 | +
|
| 344 | + :param seed: Seed for generation. |
| 345 | + :param length: Length of the password (default is 10). |
| 346 | + :return: A generated "smart" password as a string. |
| 347 | + """ |
| 348 | + return SmartPasswordGenerator.generate(seed, length) |
| 349 | + |
265 | 350 | @staticmethod |
266 | 351 | def generate_password(length: int = 10) -> str: |
267 | 352 | """ |
|
0 commit comments