Skip to content

Commit 00e4681

Browse files
smartrandom v0.2.1
1 parent c28dafe commit 00e4681

File tree

6 files changed

+432
-166
lines changed

6 files changed

+432
-166
lines changed

README.md

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# smartrandom <sup>v0.2.0</sup>
1+
# smartrandom <sup>v0.2.1</sup>
22
---
33

44
## Random Data Generators:
55

6-
Allows you to generate random strings of a given length from letters, numbers and symbols, as well as randomize text.
7-
Helps generate passwords, service codes (for example, for sending via SMS), hashes and much more.
6+
Random data generators. Allows you to generate random strings of a given length from letters, numbers and symbols, as well as randomize text. Helps generate passwords, service codes (for example, for sending via SMS), hashes and much more. Generates smart, recoverable passwords.
87

98
---
109

@@ -23,20 +22,11 @@ Author and developer: ___A.A. Suvorov.___
2322

2423
## What is news:
2524

26-
smartrandom 0.2.0 - new improved version of the library.
25+
smartrandom 0.2.1 - new improved version of the library.
2726

28-
> WARNING! This version is not backward compatible with previous versions.
29-
30-
- Completely rewritten and improved code.
31-
- Simplified import
32-
- Uses more secure and cryptographically strong secrets instead of random
33-
- Generated passwords must now contain at least one digit, one lowercase letter, one uppercase letter and one symbol. Length is at least 4.
34-
- Random letter generation is now at least 2. At least 1 lowercase letter and one uppercase letter are required.
35-
- Improved UrandomGenerator default size=128.
36-
- Added TextRandomizer.
37-
- Added tests.
38-
- Test coverage 100%.
39-
- Added documentation.
27+
- Added a regular password generator.
28+
- Added a smart password generator.
29+
- Improved tests
4030

4131
***
4232

data/images/coverage_report.png

40.8 KB
Loading

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = smartrandom
33
version = attr: smartrandom.__version__
44
author = A.A. Suvorov
55
author_email = [email protected]
6-
description = Random Data Generators. Allows you to generate random strings of a given length from letters, numbers and symbols, as well as randomize text. Helps generate passwords, service codes (for example, for sending via SMS), hashes and much more.
6+
description = Random data generators. Allows you to generate random strings of a given length from letters, numbers and symbols, as well as randomize text. Helps generate passwords, service codes (for example, for sending via SMS), hashes and much more. Generates smart, recoverable passwords.
77
long_description = file: README.md
88
long_description_content_type = text/markdown
99
url = https://github.com/smartlegionlab/smartrandom/

smartrandom/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
# https://github.com/smartlegionlab/
88
# --------------------------------------------------------
99
"""
10-
Smart Random - Random Data Generators.
10+
Random data generators.
1111
1212
Allows you to generate random strings of a given length from letters, numbers and symbols, as well as randomize text.
1313
Helps generate passwords, service codes (for example, for sending via SMS), hashes and much more.
14+
Generates smart, recoverable passwords.
1415
1516
"""
1617
from .generators import (
@@ -20,8 +21,10 @@
2021
HashGenerator,
2122
UrandomGenerator,
2223
TextRandomizer,
24+
BasePasswordGenerator,
2325
PasswordGenerator,
26+
SmartPasswordGenerator,
2427
SecretCodeGenerator,
2528
RandomDataGenerator,
2629
)
27-
__version__ = '0.2.0'
30+
__version__ = '0.2.1'

smartrandom/generators.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""Random Data Generators."""
1010
import hashlib
1111
import os
12+
import random
1213
import re
1314
import secrets
1415
import string
@@ -159,6 +160,25 @@ def generate(cls, length: int = 10) -> str:
159160
return ''.join(result)
160161

161162

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+
162182
class PasswordGenerator:
163183
upper_letters = string.ascii_uppercase
164184
lower_letters = string.ascii_lowercase
@@ -191,6 +211,50 @@ def generate(cls, length: int = 10) -> str:
191211
return ''.join(result)
192212

193213

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+
194258
class RandomDataGenerator:
195259
@staticmethod
196260
def generate_random_letters(length: int) -> str:
@@ -262,6 +326,27 @@ def randomize_text(text: str) -> str:
262326
"""
263327
return TextRandomizer.randomize(text)
264328

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+
265350
@staticmethod
266351
def generate_password(length: int = 10) -> str:
267352
"""

0 commit comments

Comments
 (0)