Skip to content

Instantly share code, notes, and snippets.

@charisschomba
Created February 2, 2019 20:19
Show Gist options
  • Save charisschomba/f25020ed2a6cfb85bd47114abb6750f9 to your computer and use it in GitHub Desktop.
Save charisschomba/f25020ed2a6cfb85bd47114abb6750f9 to your computer and use it in GitHub Desktop.
Password Generator generates highly secure passwords that are difficult to crack or guess.
import secrets
import string
from pyperclip import copy
from validators import validators
secure_random = secrets.SystemRandom()
letters = string.ascii_letters
digits = string.digits
special = string.punctuation
validator = [
validators.long_enough,
validators.has_lowercase,
validators.has_uppercase,
validators.has_numeric,
validators.has_special
]
def test_password(pw, validators = validator):
for test in validators:
if not test(pw):
return False
return True
def password_generator(password_len, name=None):
if validators.short_enough(password_len):
password_scope = letters + digits + special
list_of_random_characters = secure_random.sample(password_scope, password_len)
password = ''.join(list_of_random_characters)
if not test_password(password):
password_generator(password_len)
else:
# copies generated password to the clipbord
copy(password)
if name == None:
print( password)
else:
print('Generated password for {} account is {} '.format(name, password))
else:
print(validators.short_enough.__doc__)
from password_generator import password_generator
password_generator(20)
import string
class validators:
def long_enough(password):
'Password must be at least 6 characters'
check = len(password) >= 6
if check:
return check
else:
return validators.long_enough.__doc__
def short_enough(password):
'Password cannot be more than 32 characters'
if password > 32:
return False
return True
def has_lowercase(password):
'Password must contain a lowercase letter'
check = len(set(string.ascii_lowercase).intersection(password)) > 0
if check:
return check
else:
return validators.has_lowercase.__doc__
def has_uppercase(password):
'Password must contain an uppercase letter'
check = len(set(string.ascii_uppercase).intersection(password)) > 0
if check:
return check
else:
return validators.has_uppercase.__doc__
def has_numeric(password):
'Password must contain a digit'
check = len(set(string.digits).intersection(password)) > 0
if check:
return check
else:
return validators.has_numeric.__doc__
def has_special(password):
'Password must contain a special character'
check = len(set(string.punctuation).intersection(password)) > 0
if check:
return check
else:
return validators.has_special().__doc__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment