-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
219 lines (199 loc) · 9.77 KB
/
utils.py
File metadata and controls
219 lines (199 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import os
from rich.console import Console
import rich
import json
from datetime import datetime
import main
console = Console(color_system="auto")
def print_spaces(n: int):
for i in range(0, n):
print(" ")
class Windows:
def __init__(self):
self.main_menu()
def clear_screen(self):
os.system('cls')
def back_to_menu(self):
print_spaces(2)
back = input("Zurück zum Menü [j/n]: ")
if back.lower() in ["j", "ja", "y", "yes"]:
self.main_menu()
else:
self.clear_screen()
welcomeText = open("welcomeText.txt", "r").read()
console.print(welcomeText, style="cyan")
return
def main_menu(self):
print_spaces(3)
os.system('cls')
welcomeText = open("welcomeText.txt", "r").read()
console.print(welcomeText, style="cyan")
print_spaces(2)
print("""
+ -------------------------------------------- +
| [0] - Programm beenden |
| [1] - Passwort Generieren |
| [2] - Passwort Bewerten |
| [3] - Passwort Speichern |
| [4] - Gespeicherte Passwörter anzeigen |
| [5] - Gespeicherte Passwörter löschen |
+ -------------------------------------------- +
""")
print_spaces(2)
selected_menu = input("Bitte wählen Sie eine Option: ")
if selected_menu == "0":
return
if selected_menu == "1":
self.generate_password()
elif selected_menu == "2":
self.rate_password()
elif selected_menu == "3":
self.save_password()
elif selected_menu == "4":
self.show_passwords()
elif selected_menu == "5":
self.delete_all()
else:
self.main_menu()
def generate_password(self):
# Get Parameters for password generation
while True: # Password length
try:
length = int(input("Bitte geben Sie die Länge des Passworts ein: ")) # noqa
break
except ValueError:
console.print(
"Bitte geben Sie eine [bold]Zahl[/bold] ein", style="red")
while True: # Password amount
try:
anzahl = int(input("Bitte geben Sie die Anzahl der Passwörter ein: ")) # noqa
break
except ValueError:
console.print(
"Bitte geben Sie eine [bold]Zahl[/bold] ein", style="red")
while True: # Password characters
print("Welche Zeichen sollen im Passwort enthalten sein?")
chosen_characters = input("[1]: Buchstaben\n[2]: Zahlen\n[3]: Sonderzeichen\n") # noqa
if all(char in "123" for char in chosen_characters):
break
else:
console.print(
"Bitte geben Sie eine [bold]gültige Option[/bold] ein", style="red") # noqa
print_spaces(2)
# Generate and rate passwords to console
passwords = main.generate_password(length, anzahl, chosen_characters)
for i, password in enumerate(passwords):
try:
console.print(f"[bold]Passwort {
i+1}[/bold]: [default]{password}[/default]", style="green") # noqa
except rich.errors.MarkupError:
print(f"Passwort {i+1}: {password}")
rating = main.rate_password(password)
if rating == "Stark":
console.print(f"Das Passwort ist: {rating}", style="green")
elif rating == "Mittelstark":
console.print(f"Das Passwort ist: {rating}", style="yellow")
else:
console.print(f"Das Passwort ist: {rating}", style="red")
console.print("[bold]---------------------[/bold]", style="cyan")
print_spaces(2)
# Print passwords for txt file
save_to_file = input(
"Möchten Sie die Passwörter in einer Datei speichern? [j/n] ")
if save_to_file.lower() in ["j", "ja", "y", "yes"]:
filename = input("Bitte geben Sie den Dateinamen ein: ")
with open(f"{filename}.txt", "w") as file:
for i, password in enumerate(passwords):
file.write(f"Passwort {i + 1}: {password}\n")
console.print(f"Passwörter wurden in [bold]{
filename}.txt[/bold] gespeichert", style="green")
# Save Passwords in JSON File
save_to_json = input("Sollen die Passwörter in der Anwendung gespeichert werden? [j/n] ") # noqa
console.print("[bold]---------------------[/bold]", style="cyan")
if save_to_json.lower() in ["j", "ja", "y", "yes"]:
for i, password in enumerate(passwords):
console.print(f"[bold]Password {
i+1}[/bold]: [default]{password}[/default]", style="green") # noqa
console.print(
"[bold]---------------------[/bold]", style="cyan")
name = input("Bitte geben Sie den Namen der Website ein: ")
username = input("Bitte geben Sie den Benutzernamen ein: ")
main.save_password(name, username, password)
self.back_to_menu()
def rate_password(self):
password = input("Bitte geben Sie das Passwort zum bewerten ein: ")
rating = main.rate_password(password)
if rating == "Stark":
console.print(f"Das Passwort ist: [green bold]{rating}[/green bold]") # noqa
elif rating == "Mittelstark":
console.print(f"Das Passwort ist: [yellow bold]{rating}[/yellow bold]") # noqa
else:
console.print(f"Das Passwort ist: [red bold]{rating}[/red bold]")
self.back_to_menu()
def save_password(self):
name = input("Bitte geben Sie den Namen der Website ein: ")
username = input("Bitte geben Sie den Benutzernamen ein: ")
password = input("Bitte geben Sie das Passwort ein: ")
print_spaces(2)
main.save_password(name, username, password)
self.back_to_menu()
def show_passwords(self):
passwords = main.get_passwords()
if not passwords:
print("Keine gespeicherten Passwörter gefunden.")
else:
output_choice = input(
"Möchten Sie die Passwörter in der Konsole oder in einer Textdatei anzeigen? [konsole/datei] [k/d] ") # noqa
if output_choice.lower() in ["konsole", "k"]:
console.print(
"[bold]---------------------[/bold]", style="cyan")
for entry in passwords:
console.print(
f"[cyan]Name:[/cyan] [green1]{entry['name']}[/green1]", style="bold") # noqa
console.print(
f" [cyan]Username:[/cyan] [green1]{entry['username']}[/green1]", style="bold") # noqa
console.print(
f" [cyan]Password:[/cyan] [green1]{entry['password']}[/green1]", style="bold") # noqa
if main.rate_password(entry['password']) == "Stark":
console.print("Das Passwort ist [green bold]Stark[/green bold]", style="bold") # noqa
if main.rate_password(entry['password']) == "Mittelstark":
console.print("Das Passwort ist [yellow bold]Mittelstark[/yellow bold]", style="bold") # noqa
if main.rate_password(entry['password']) == "Schwach":
console.print("Das Passwort ist [red bold]Schwach[/red bold]", style="bold") # noqa
console.print(
"[bold]---------------------[/bold]", style="cyan")
elif output_choice.lower() in ["datei", "d"]:
now = datetime.now()
filename = now.strftime("passwords_output_%Y%m%d_%H%M%S.txt")
with open(filename, 'w') as output_file:
for entry in passwords:
output_file.write(f"Name: {entry['name']}\n")
output_file.write(f" Username: {entry['username']}\n")
output_file.write(f" Password: {entry['password']}\n")
print(f"Passwörter wurden in {filename} gespeichert.")
delete = input(
"Möchten Sie die gespeicherten Passwörter löschen? [j/n] ")
if delete.lower() in ["j", "ja", "y", "yes"]:
which = input("Welches Passwort möchten Sie löschen? (Name) ")
for entry in passwords:
if entry['name'] == which:
passwords.remove(entry)
with open('passwords.json', 'w') as file:
json.dump(passwords, file, indent=4)
print("Passwort gelöscht")
self.back_to_menu()
def delete_all(self):
console.print("Bist du sicher, dass du alles [bold red]LÖSCHEN[/bold red] willst?") # noqa
delete = input("[j/n] ")
if delete in ["j", "ja", "y", "yes"]:
try:
os.remove("passwords.json")
except FileNotFoundError:
console.print("\nEs wurde [bold red]keine Passwörter[/bold red] gefunden!\n") # noqa
self.back_to_menu()
return
console.print("\n[bold]ALLE Passwörter wurden [red]GELÖSCHT[/red][/bold]\n") # noqa
self.back_to_menu()
else:
console.print("\n[bold]Passwörter wurden [red]NICHT[/red] gelöscht![/bold]\n") # noqa
self.back_to_menu()