|
1 | | -''' |
2 | | -The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, |
3 | | -account authentication, security tokens, and related secrets. |
4 | | -''' |
5 | | -import string |
| 1 | +import string as str |
6 | 2 | import secrets |
7 | 3 |
|
8 | | -def secure_password_gen(passlength): |
9 | | - password = ''.join((secrets.choice(string.ascii_letters) for i in range(passlength))) |
10 | | - return password |
| 4 | +class PasswordGenerator(): |
11 | 5 |
|
12 | | -n = int(input('Enter length of password : ')) |
13 | | -print('Password generated is :', secure_password_gen(n)) |
| 6 | + @staticmethod |
| 7 | + def gen_sequence(conditions): #must have conditions (in a list format), for each member of the list possible_characters |
| 8 | + possible_characters=[str.ascii_lowercase, str.ascii_uppercase, str.digits, str.punctuation] |
| 9 | + sequence="" |
| 10 | + for x in range(len(conditions)): |
| 11 | + if conditions[x]: |
| 12 | + sequence+=possible_characters[x] |
| 13 | + else: |
| 14 | + pass |
| 15 | + return sequence |
| 16 | + |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def gen_password(sequence, passlength=8): |
| 20 | + password = ''.join((secrets.choice(sequence) for i in range(passlength))) |
| 21 | + return password |
| 22 | + |
| 23 | + |
| 24 | +class Interface(): |
| 25 | + has_characters={ |
| 26 | + "lowercase":True, |
| 27 | + "uppercase":True, |
| 28 | + "digits":True, |
| 29 | + "punctuation":True |
| 30 | + } |
| 31 | + @classmethod |
| 32 | + def change_has_characters(cls, change): |
| 33 | + try: |
| 34 | + cls.has_characters[change] #to check if the specified key exists in the dicitonary |
| 35 | + except: |
| 36 | + print("Invalid") |
| 37 | + else: |
| 38 | + cls.has_characters[change]= not cls.has_characters[change] #automaticly changres to the oppesite value already there |
| 39 | + print(f"{change} is now set to {cls.has_characters[change]}") |
| 40 | + @classmethod |
| 41 | + def show_has_characters(cls): |
| 42 | + print(cls.has_characters) |
| 43 | + |
| 44 | + |
| 45 | + def generate_password(self, lenght): |
| 46 | + sequence = PasswordGenerator.gen_sequence(list(self.has_characters.values())) |
| 47 | + print(PasswordGenerator.gen_password(sequence, lenght)) |
| 48 | + |
| 49 | +def list_to_vertical_string(list): |
| 50 | + to_return ="" |
| 51 | + for member in list: |
| 52 | + to_return += f"{member}\n" |
| 53 | + return to_return |
| 54 | + |
| 55 | +class Run(): |
| 56 | + def decide_operation(self): |
| 57 | + user_input = input(": ") |
| 58 | + try: |
| 59 | + int(user_input) |
| 60 | + except: |
| 61 | + Interface.change_has_characters(user_input) |
| 62 | + else: |
| 63 | + Interface().generate_password(int(user_input)) |
| 64 | + finally: |
| 65 | + print("\n\n") |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + def run(self): |
| 70 | + menu = \ |
| 71 | +f"""Welcome to the PassGen App! |
| 72 | +Commands: |
| 73 | + generate password -> |
| 74 | + <lenght of the password> |
| 75 | +
|
| 76 | +commands to change the characters to be used to generate passwords: |
| 77 | +{list_to_vertical_string(Interface.has_characters.keys())} |
| 78 | + """ |
| 79 | + print(menu) |
| 80 | + while True: |
| 81 | + self.decide_operation() |
| 82 | + |
| 83 | + |
| 84 | +Run().run() |
0 commit comments