Skip to content

Commit

Permalink
For python 3.6 and above using secrets module
Browse files Browse the repository at this point in the history
  • Loading branch information
suryasr007 committed Jun 27, 2019
1 parent 8944577 commit 6b11b22
Showing 1 changed file with 72 additions and 22 deletions.
94 changes: 72 additions & 22 deletions password_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import string
# import random
from copy import deepcopy
from random import shuffle, randint

Expand All @@ -17,7 +16,7 @@
from random import choice


class PasswordGenerator():
class PasswordGenerator:
"""
We can set properties such as
Expand Down Expand Up @@ -54,46 +53,97 @@ def __init__(self):
self.lower_chars = string.ascii_lowercase
self.upper_chars = string.ascii_uppercase
self.numbers_list = string.digits
self._schars = ['!','#','$','%', '^', '&', '*', '(', ')', ',', '.', '-', '_', '+', '=', '<', '>', '?']
self._allchars = list(self.lower_chars) + list(self.upper_chars) + list(self.numbers_list) + self._schars

self._schars = [
"!",
"#",
"$",
"%",
"^",
"&",
"*",
"(",
")",
",",
".",
"-",
"_",
"+",
"=",
"<",
">",
"?",
]
self._allchars = (
list(self.lower_chars)
+ list(self.upper_chars)
+ list(self.numbers_list)
+ self._schars
)

def generate(self):
"""Generates a password using default or custom properties"""
if self.minlen < 0 or self.maxlen < 0 or self.minuchars < 0 or self.minlchars < 0 or self.minnumbers < 0 or self.minschars < 0 :
if (
self.minlen < 0
or self.maxlen < 0
or self.minuchars < 0
or self.minlchars < 0
or self.minnumbers < 0
or self.minschars < 0
):
raise ValueError("Character length should not be negative")

if self.minlen > self.maxlen:
raise ValueError("Minimum length cannot be greater than maximum length. The default maximum length is 16.")
raise ValueError(
"Minimum length cannot be greater than maximum length. The default maximum length is 16."
)

collectiveMinLength = self.minuchars + self.minlchars + self.minnumbers + self.minschars
collectiveMinLength = (
self.minuchars + self.minlchars + self.minnumbers + self.minschars
)

if collectiveMinLength > self.minlen:
self.minlen = collectiveMinLength

final_pass = [choice(list(set(self.lower_chars) - set(self.excludelchars))) for i in range(self.minlchars)]
final_pass += [choice(list(set(self.upper_chars) - set(self.excludeuchars))) for i in range(self.minuchars)]
final_pass += [choice(list(set(self.numbers_list) - set(self.excludenumbers))) for i in range(self.minnumbers)]
final_pass += [choice(list(set(self._schars) - set(self.excludeschars))) for i in range(self.minschars)]

final_pass = [
choice(list(set(self.lower_chars) - set(self.excludelchars)))
for i in range(self.minlchars)
]
final_pass += [
choice(list(set(self.upper_chars) - set(self.excludeuchars)))
for i in range(self.minuchars)
]
final_pass += [
choice(list(set(self.numbers_list) - set(self.excludenumbers)))
for i in range(self.minnumbers)
]
final_pass += [
choice(list(set(self._schars) - set(self.excludeschars)))
for i in range(self.minschars)
]

currentpasslen = len(final_pass)
all_chars = list(set(self._allchars) - set(list(self.excludelchars) + list(self.excludeuchars) + list(self.excludenumbers) + list(self.excludeschars)))
all_chars = list(
set(self._allchars)
- set(
list(self.excludelchars)
+ list(self.excludeuchars)
+ list(self.excludenumbers)
+ list(self.excludeschars)
)
)

if len(final_pass) < self.maxlen:
randlen = randint(self.minlen, self.maxlen)
final_pass += [choice(all_chars) for i in range(randlen-currentpasslen)]
final_pass += [choice(all_chars) for i in range(randlen - currentpasslen)]

shuffle(final_pass)
return ''.join(final_pass)

return "".join(final_pass)

def shuffle_password(self, password, maxlen):
"""Shuffle the given charactes and return a password from given characters."""
final_pass = [choice(list(password)) for i in range(int(maxlen))]
shuffle(final_pass)
return ''.join(final_pass)

return "".join(final_pass)

def non_duplicate_password(self, maxlen):
"""Generate a non duplicate key of given length"""
Expand All @@ -106,7 +156,7 @@ def non_duplicate_password(self, maxlen):
final_pass.append(character)
allchars.pop(element_index)
except IndexError as e:
raise ValueError('Length should less than 77 characters.')
raise ValueError("Length should less than 77 characters.")

shuffle(final_pass)
return ''.join(final_pass)
return "".join(final_pass)

0 comments on commit 6b11b22

Please sign in to comment.