Skip to content
This repository was archived by the owner on Nov 9, 2019. It is now read-only.

Commit a0094c1

Browse files
committed
Merge pull request #13 from leonklingele/fix-password-generation
Fix password generation
2 parents 1771460 + 23864ed commit a0094c1

1 file changed

Lines changed: 4 additions & 8 deletions

File tree

pick

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,12 @@ class Safe():
132132
class Util():
133133
def generate_password(self, length=50):
134134
''' Generates a random password of length '''
135-
rand_max = 256
136-
rand_excess = (rand_max + 1) % length
137-
rand_limit = rand_max - rand_excess
138-
chars = string.uppercase + string.lowercase + string.punctuation + string.letters
135+
# Use no more than 256 chars in this string, rest will not be used
136+
chars = string.digits + string.punctuation + string.letters
137+
num_chars = len(chars)
139138

140139
def next_index():
141-
while True:
142-
x = ord(urandom(1))
143-
if x <= rand_limit:
144-
return x % length
140+
return ord(urandom(1)) % num_chars
145141

146142
return ''.join(chars[next_index()] for _ in range(length))
147143

0 commit comments

Comments
 (0)