Skip to content

Commit 8536ae3

Browse files
#v1.4.0
[FIX] Made the random generator cryptographically safe.
1 parent bc824d6 commit 8536ae3

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# randomisedString v1.3.3
1+
# randomisedString v1.4.0
22

33
```pip install randomisedString --upgrade```
44

55

6-
###### <br>A well maintained program to generate randomised strings. Can be used for assigning unique IDs of any specified size. Can be alpha only or numeric or alphanumeric as specified.
6+
###### <br>A well maintained program to generate cryptographically safe randomised strings. Can be used for assigning unique IDs of any specified size. Can be alpha only or numeric or alphanumeric as specified.
77

88

99
<br>To install:

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "randomisedString"
9-
version = "1.3.3"
10-
description = "A well maintained program to generate randomised strings. Can be used for assigning unique IDs of any specified size. Can be alpha only or numeric or alphanumeric as specified."
9+
version = "1.4.0"
10+
description = "A well maintained program to generate cryptographically safe randomised strings. Can be used for assigning unique IDs of any specified size. Can be alpha only or numeric or alphanumeric as specified."
1111
readme = "README.md"
1212
maintainers = [{ name = "Bhindi", email = "[email protected]" }]
1313
classifiers = [

randomisedString.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.3.3"
1+
__version__ = "1.4.0"
22
__packagename__ = "randomisedstring"
33

44

@@ -15,8 +15,17 @@ def updatePackage():
1515
latest = data['info']['version']
1616
if latest != __version__:
1717
try:
18-
import pip
19-
pip.main(["install", __packagename__, "--upgrade"])
18+
import subprocess
19+
from pip._internal.utils.entrypoints import (
20+
get_best_invocation_for_this_pip,
21+
get_best_invocation_for_this_python,
22+
)
23+
from pip._internal.utils.compat import WINDOWS
24+
if WINDOWS:
25+
pip_cmd = f"{get_best_invocation_for_this_python()} -m pip"
26+
else:
27+
pip_cmd = get_best_invocation_for_this_pip()
28+
subprocess.run(f"{pip_cmd} install {__packagename__} --upgrade")
2029
print(f"\nUpdated package {__packagename__} v{__version__} to v{latest}\nPlease restart the program for changes to take effect")
2130
sleep(3)
2231
except:
@@ -28,21 +37,21 @@ def updatePackage():
2837
print(f"Ignoring version check for {__packagename__} (Failed)")
2938

3039

31-
class Generator:
40+
class Imports:
41+
from secrets import choice, randbelow
42+
43+
44+
class RandomisedString:
3245
def __init__(self):
3346
"""
3447
Initialise the Generator and use the public functions to generate a randomised string.
3548
"""
36-
from random import choice as __choice, randrange as __randrange
37-
self.__choice = __choice
38-
self.__randrange = __randrange
3949
self.LOWER_CASE_ASCIIS = list(range(97, 122 + 1))
4050
self.UPPER_CASE_ASCIIS = list(range(65, 90 + 1))
4151
self.NUMBER_ASCIIS = list(range(48, 57 + 1))
4252
self.ALPHANUMERIC_ASCIIS = self.LOWER_CASE_ASCIIS + self.UPPER_CASE_ASCIIS + self.NUMBER_ASCIIS
4353

44-
45-
def AlphaNumeric(self, _min=10, _max=20)->str:
54+
def AlphaNumeric(self, _min=10, _max=20) -> str:
4655
"""
4756
Generates a string with numbers and alphabets(a-z, A-Z, 0-9)
4857
:param _min: Minimum possible length of generated string
@@ -52,14 +61,12 @@ def AlphaNumeric(self, _min=10, _max=20)->str:
5261
_minLength = min(_min, _max)
5362
_maxLength = max(_min, _max)
5463
if _maxLength == _minLength:
55-
_maxLength+=1
56-
string = ''
57-
for _ in range(self.__randrange(_minLength, _maxLength)):
58-
string += chr(self.__choice(self.ALPHANUMERIC_ASCIIS))
64+
_maxLength += 1
65+
length = Imports.randbelow(_maxLength - _minLength) + _minLength
66+
string = ''.join(chr(Imports.choice(self.ALPHANUMERIC_ASCIIS)) for _ in range(length))
5967
return string
6068

61-
62-
def OnlyNumeric(self, _min=10, _max=20)->str:
69+
def OnlyNumeric(self, _min=10, _max=20) -> str:
6370
"""
6471
Generates a string with only numbers(0-9). Convert the string to int if needed
6572
:param _min: Minimum possible length of generated string
@@ -70,13 +77,11 @@ def OnlyNumeric(self, _min=10, _max=20)->str:
7077
_maxLength = max(_min, _max)
7178
if _maxLength == _minLength:
7279
_maxLength += 1
73-
string = ''
74-
for _ in range(self.__randrange(_minLength, _maxLength)):
75-
string += chr(self.__choice(self.NUMBER_ASCIIS))
80+
length = Imports.randbelow(_maxLength - _minLength) + _minLength
81+
string = ''.join(chr(Imports.choice(self.NUMBER_ASCIIS)) for _ in range(length))
7682
return string
7783

78-
79-
def OnlyAlpha(self, _min=10, _max=20)->str:
84+
def OnlyAlpha(self, _min=10, _max=20) -> str:
8085
"""
8186
Generates a string with only Alphabets(a-z, A-Z)
8287
:param _min: Minimum possible length of generated string
@@ -85,10 +90,7 @@ def OnlyAlpha(self, _min=10, _max=20)->str:
8590
"""
8691
_minLength = min(_min, _max)
8792
_maxLength = max(_min, _max)
88-
if _maxLength == _minLength:
89-
_maxLength += 1
90-
string = ''
91-
for _ in range(self.__randrange(_minLength, _maxLength)):
92-
string += chr(self.__choice(self.LOWER_CASE_ASCIIS+self.UPPER_CASE_ASCIIS))
93+
if _maxLength == _minLength: _maxLength += 1
94+
length = Imports.randbelow(_maxLength - _minLength) + _minLength
95+
string = ''.join(chr(Imports.choice(self.LOWER_CASE_ASCIIS + self.UPPER_CASE_ASCIIS)) for _ in range(length))
9396
return string
94-

0 commit comments

Comments
 (0)