Created
November 12, 2017 19:21
-
-
Save Sparrow1029/2b436bfe51ea6c7045d3374592a61d9f to your computer and use it in GitHub Desktop.
Password generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
#-*- coding: utf-8 -*- | |
""" | |
Make a password generator with options for weak to strong. | |
Weak should select a word or two from a list, strong should contain | |
uppercase, lowercase, symbols and numbers. | |
""" | |
from sys import exit | |
from random import randint, sample | |
from string import digits, ascii_lowercase, ascii_uppercase, punctuation | |
chars = {1: digits, | |
2: ascii_lowercase, | |
3: ascii_uppercase, | |
4: punctuation} | |
weak = ['correct', 'horse', 'battery', 'staple', 'hello', 'world', 'eggs'] | |
def choose(): | |
choice = """ | |
Choose password strength: | |
0 -- Weak | |
1 -- Average | |
2 -- Strong | |
""" | |
while True: | |
try: | |
strength = int(input(choice)) | |
if strength not in range(3): | |
raise IndexError | |
break | |
except (ValueError, IndexError): | |
print('Invalid entry.\n') | |
continue | |
return strength | |
# print(strength) | |
length = (2, 16, 32) | |
def gen_psswd(kind): | |
"""Main generating function.""" | |
if kind == 2: | |
return '_'.join(sample(weak, 2)) | |
elif kind > 2: | |
psswd = "" | |
for i in range(kind): | |
charset = chars[randint(1, 4)] | |
x = charset[randint(0, len(charset)-1)] | |
psswd += x | |
return psswd | |
running = True | |
while running: | |
strength = choose() | |
new_password = gen_psswd(length[strength]) | |
print("Your new password: \n\n\t{}\n".format(new_password)) | |
again = input("Generate another? y/n ") | |
if again.lower() in "yes": | |
continue | |
else: | |
break | |
exit("\nGoodbye") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment