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

Commit

Permalink
Password generation can be done much easier..
Browse files Browse the repository at this point in the history
  • Loading branch information
leonklingele committed Dec 7, 2015
1 parent e18d622 commit 23864ed
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions pick
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,12 @@ class Safe():
class Util():
def generate_password(self, length=50):
''' Generates a random password of length '''
rand_max = 256
rand_excess = (rand_max + 1) % length
rand_limit = rand_max - rand_excess
chars = string.digits + string.letters + string.punctuation
# Use no more than 256 chars in this string, rest will not be used
chars = string.digits + string.punctuation + string.letters
num_chars = len(chars)

def next_index():
while True:
x = ord(urandom(1))
if x <= rand_limit:
return x % length
return ord(urandom(1)) % num_chars

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

Expand Down

0 comments on commit 23864ed

Please sign in to comment.