[go: up one dir, main page]

Skip to content

Commit

Permalink
Merge pull request geekcomputers#833 from DiogoCastroAlmeida/master
Browse files Browse the repository at this point in the history
Added features to the password generator
  • Loading branch information
geekcomputers authored Sep 25, 2020
2 parents bcca248 + ae30706 commit acaad5e
Showing 1 changed file with 81 additions and 10 deletions.
91 changes: 81 additions & 10 deletions Password Generator/pass_gen.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,84 @@
'''
The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords,
account authentication, security tokens, and related secrets.
'''
import string
import string as str
import secrets

def secure_password_gen(passlength):
password = ''.join((secrets.choice(string.ascii_letters) for i in range(passlength)))
return password
class PasswordGenerator():

n = int(input('Enter length of password : '))
print('Password generated is :', secure_password_gen(n))
@staticmethod
def gen_sequence(conditions): #must have conditions (in a list format), for each member of the list possible_characters
possible_characters=[str.ascii_lowercase, str.ascii_uppercase, str.digits, str.punctuation]
sequence=""
for x in range(len(conditions)):
if conditions[x]:
sequence+=possible_characters[x]
else:
pass
return sequence


@staticmethod
def gen_password(sequence, passlength=8):
password = ''.join((secrets.choice(sequence) for i in range(passlength)))
return password


class Interface():
has_characters={
"lowercase":True,
"uppercase":True,
"digits":True,
"punctuation":True
}
@classmethod
def change_has_characters(cls, change):
try:
cls.has_characters[change] #to check if the specified key exists in the dicitonary
except:
print("Invalid")
else:
cls.has_characters[change]= not cls.has_characters[change] #automaticly changres to the oppesite value already there
print(f"{change} is now set to {cls.has_characters[change]}")
@classmethod
def show_has_characters(cls):
print(cls.has_characters)


def generate_password(self, lenght):
sequence = PasswordGenerator.gen_sequence(list(self.has_characters.values()))
print(PasswordGenerator.gen_password(sequence, lenght))

def list_to_vertical_string(list):
to_return =""
for member in list:
to_return += f"{member}\n"
return to_return

class Run():
def decide_operation(self):
user_input = input(": ")
try:
int(user_input)
except:
Interface.change_has_characters(user_input)
else:
Interface().generate_password(int(user_input))
finally:
print("\n\n")



def run(self):
menu = \
f"""Welcome to the PassGen App!
Commands:
generate password ->
<lenght of the password>
commands to change the characters to be used to generate passwords:
{list_to_vertical_string(Interface.has_characters.keys())}
"""
print(menu)
while True:
self.decide_operation()


Run().run()

0 comments on commit acaad5e

Please sign in to comment.