forked from AvgBlank/PasswordManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
504 lines (440 loc) · 15.8 KB
/
main.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
##
###################! Imports ###################
# ? String -> For Generating Password
import string
# ? OS -> For Running Commands
from os import name as OSName, system
# ? Random -> For Generating Password
from random import choice as randChoice, shuffle as randShuffle
# ? Time -> For Pausing the Script
from time import sleep
# ? SQLite3 -> For Connecting to MySQL
from sqlite3 import connect
# ? Pyperclip -> For Copying to Clipboard
import pyperclip
# ? Re --> For checking if characters exist in string
from re import compile
# ? Cryptography -> For encrpytion
from cryptography.fernet import Fernet, InvalidToken
# ? Questionary -> For better command line interface
from questionary import (
Style,
password,
select,
text,
confirm,
checkbox,
press_any_key_to_continue as cont,
)
# ? Rich --> For a box and loading bar
from rich import print
from rich.console import Console
from rich.panel import Panel
from rich.progress import (
BarColumn,
Progress,
TextColumn,
)
###################! Functions ###################
######? Connecting to SQLite3 #####
def conSQL():
global conn, cur, table
conn = connect("Manager.db")
cur = conn.cursor()
#####* Checking if table exists
table = "passwords"
cur.execute(
rf"Create table if not exists {table}(IndexNo int, Name varchar(244), Email varchar(244), Username varchar(244), Password varchar(244))"
)
######? Encryption #####
def encryption():
global fernet
while True:
#####* Clearing The Screen #####
ClearScreen()
inp = confirm("Do you have an encryption key?", style=minimalStyle).ask()
if inp:
key = password("Enter the key:", style=minimalStyle).ask()
else:
while True:
GenKey = confirm(
"Do you want to generate a key?\n[You will lose access to previous data]",
style=minimalStyle,
).ask()
if GenKey is False:
exit()
elif GenKey:
key = Fernet.generate_key()
key = key.decode("utf-8")
print(
f"Given below is the key, you will not get access to it again.\n{key}"
)
while True:
copyKey = confirm(
"Do you want to copy the key?", style=minimalStyle
).ask()
if copyKey:
pyperclip.copy(key)
break
break
else:
continue
try:
fernet = Fernet(key)
break
except ValueError:
print("The key you entered is invalid.")
######? Clear Screen #####
def ClearScreen():
global minimalStyle
#####* For Rich #####
console = Console()
#####* For Questionary Style #####
minimalStyle = Style(
[
("answer", "fg:#00FFFF italic"), # ? White
("question", "fg:#FFFFFF bold"), # ? White
("pointer", "fg:#00FFFF bold"), # ? Cyan
("highlighted", "fg:#00FFFF"), # ? White
("selected", "fg:#A9A9A9"), # ? Grey
("qmark", "fg:#77DD77"), # ? Green
]
)
#####* Clearing the Screen #####
system("clear" if OSName == "posix" else "cls")
#####* Printing Password Manager #####
console.print(Panel.fit("[bold italic #77DDD4]Password Manager", padding=(0, 22)))
######? Loading bar ######
def StatBar(time: float, desc: str):
progress_bar = Progress(
TextColumn(f"{desc} "),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
)
with progress_bar as p:
for _ in p.track(range(100), description=desc):
sleep(time / 100)
sleep(0.5)
######? Generating Passwords ######
def GenPass(p):
genop = []
while True:
gen = confirm("Do you want to generate a password?", style=minimalStyle).ask()
if gen:
while True:
try:
genlen = int(
text(
"How long should the password be?", style=minimalStyle
).ask()
)
if genlen <= 0:
raise ValueError
break
except ValueError or TypeError:
print("Please enter a valid positive number.")
while True:
for _ in range(genlen):
genop.append(
randChoice(
string.ascii_letters + string.digits + "@!_#$%^&*()<>?/}{~:"
)
)
regex = compile("[@!_#$%^&*()<>?/}{~:]")
if regex.search("".join(str(v) for v in genop)) is None:
genop = []
continue
if any(i.isdigit() for i in genop) is False:
genop = []
continue
if any(i.isalpha() for i in genop) is False:
genop = []
continue
break
randShuffle(genop)
genop = "".join(str(v) for v in genop)
ClearScreen()
print(f"Generated Password: {genop}")
return genop
elif gen is False:
return password(p).ask()
######? Adding Entry ######
def AddEntry(t):
#####* Variables #####
result = []
NameResult = []
#####* Clearing the Screen #####
ClearScreen()
#####* Going Through Table #####
cur.execute(rf"select * from {t}")
res = cur.fetchall()
for _ in res:
result.append(_)
cur.execute(rf"select Name from {t}")
res = cur.fetchall()
if len(res) != 0:
for _ in res:
_ = _[0].lower()
NameResult.append(_)
#####* Getting Values #####
while True:
name = text(
"What do you want the entry to be called?", style=minimalStyle
).ask()
if name == "":
print("Name cannot be left empty.")
continue
if name.lower() in NameResult:
print("That name already exists.")
continue
break
email = text("Enter Email ID [Optional]:", style=minimalStyle).ask()
usrname = text("Enter Username [Optional]:", style=minimalStyle).ask()
passwd = GenPass("Enter Password:")
passwd = fernet.encrypt(passwd.encode())
#####* Adding Values to Table #####
cur.execute(
rf'insert into {t} values({len(result) + 1}, "{name}", "{email}", "{usrname}", "{passwd}")'
)
conn.commit()
print(f"Entry for {name} has been successfully added!")
cont().ask()
######? Editting Entry ######
def EditEntry(t):
try:
#####* Variables #####
result = []
options = []
Name = "-"
Email = "-"
Username = "-"
Passwd = "-"
#####* Going Through Table #####
cur.execute(rf"select * from {t}")
res = cur.fetchall()
for _ in res:
result.append(_)
#####* Printing Options #####
###? Clearing the Screen ###
ClearScreen()
###? Options ###
for i in result:
options.append(f"{i[0]}. {i[1]}".title())
options.append("0. Back")
choice = select(
"Which entry do you want to edit?", options, style=minimalStyle
).ask()
choice = int(choice[0])
if choice == 0:
raise AttributeError
#####* Getting Values #####
opt = checkbox(
"What all do you want to edit?",
["Name", "Email", "Username", "Password"],
style=minimalStyle,
).ask()
###? Name ###
if "Name" in opt:
while True:
Name = text("What should the name be?", style=minimalStyle).ask()
if Name == "":
print("Name cannot be left empty.")
continue
cur.execute(rf"update {t} set Name='{Name}' where IndexNo={choice}")
break
###? Email ###
if "Email" in opt:
Email = text("What should the email be?", style=minimalStyle).ask()
cur.execute(rf"update {t} set Email='{Email}' where IndexNo={choice}")
###? Username ###
if "Username" in opt:
Username = text("What should the username be?", style=minimalStyle).ask()
cur.execute(rf"update {t} set Username='{Username}' where IndexNo={choice}")
###? Password ###
if "Password" in opt:
Passwd = GenPass("What should the password be?")
Passwd = fernet.encrypt(Passwd.encode())
cur.execute(rf'update {t} set Password="{Passwd}" where IndexNo={choice}')
if Name == "-" and Email == "-" and Username == "-" and Passwd == "-":
print("The entry has not been modified.")
else:
conn.commit()
print("The entry has been successfully modified!")
cont().ask()
except AttributeError:
PrintOptions()
######? Deleting Entry ######
def DelEntry(t):
try:
#####* Variables #####
result = []
options = []
Names = []
#####* Going Through Table #####
cur.execute(rf"select * from {t}")
res = cur.fetchall()
for _ in res:
result.append(_)
#####* Printing Options #####
###? Clearing the Screen ###
ClearScreen()
###? Options ###
for i in result:
options.append(f"{i[0]}. {i[1]}".title())
options.append("0. Back")
choice = select(
"Which entry do you want to edit?", options, style=minimalStyle
).ask()
choice = int(choice[0])
if choice == 0:
raise AttributeError
#####* Confirmation #####
while True:
cur.execute(rf"select Name from {t} where IndexNo={choice}")
name = cur.fetchall()[0][0]
conf = confirm(
f"Are you sure you want to delete the entry for {name}?",
style=minimalStyle,
).ask()
if conf:
cur.execute(rf"delete from {t} where IndexNo={choice}")
conn.commit()
###? Setting Proper Index Numbers ###
cur.execute(rf"select Name from {t}")
names = cur.fetchall()
for _ in names:
Names.append(_[0])
for index, val in enumerate(Names):
cur.execute(
rf"update {t} set IndexNo={index + 1} where Name='{val}'"
)
conn.commit()
###? Confirmation ###
print(f"The entry for {name} has been successfully deleted!")
cont().ask()
break
elif conf is False:
break
except AttributeError:
PrintOptions()
######? Copying Entry ######
def CopyEntry(t):
try:
#####* Variables #####
result = []
options = []
#####* Going Through Table #####
cur.execute(rf"select * from {t}")
res = cur.fetchall()
for _ in res:
result.append(_)
#####* Printing Options #####
###? Clearing the Screen ###
ClearScreen()
###? Options ###
for i in result:
options.append(f"{i[0]}. {i[1]}".title())
options.append("0. Back")
choice = select(
"Which entry do you want to copy?", options, style=minimalStyle
).ask()
choice = int(choice[0])
if choice == 0:
raise AttributeError
#####* Copying to Clipboard #####
###? Getting Name ###
cur.execute(rf"select Name from {t} where IndexNo={choice}")
name = cur.fetchall()[0][0]
while True:
###? Getting What User Wants to Copy to Clipboard ###
conf = select(
"What do you want to copy?",
["Email", "Username", "Password"],
style=minimalStyle,
).ask()
###? Email ###
if conf == "Email":
cur.execute(rf"select Email from {t} where IndexNo={choice}")
result = cur.fetchall()[0][0]
if result == "":
ClearScreen()
print(f"The email in {name} does not exist.")
continue
pyperclip.copy(result)
sleep(1)
print("The email has been successfully copied to your clipboard!")
sleep(0.5)
cont().ask()
break
###? Username ###
elif conf == "Username":
cur.execute(rf"select Username from {t} where IndexNo={choice}")
result = cur.fetchall()[0][0]
if result == "":
ClearScreen()
print(f"The Username in {name} does not exist.")
continue
pyperclip.copy(result)
sleep(1)
print("The username has been successfully copied to your clipboard!")
sleep(0.5)
cont().ask()
break
###? Password ###
elif conf == "Password":
cur.execute(rf"select Password from {t} where IndexNo={choice}")
result = cur.fetchall()[0][0][2:-1]
decPass = fernet.decrypt(result).decode("utf-8")
if result == "":
ClearScreen()
print(f"The Password in {name} does not exist.")
continue
print(decPass)
pyperclip.copy(decPass)
sleep(1)
print("The password has been successfully copied to your clipboard")
sleep(0.5)
cont().ask()
break
except AttributeError:
PrintOptions()
except InvalidToken:
sleep(1)
print("Please enter the correct encryption key.")
cont().ask()
######? Printing Options ######
def PrintOptions():
while True:
global minimalStyle
####* Clearing the Screen ####
ClearScreen()
####* Printing Options ####
choice = select(
"What is your choice?",
["Add Entry", "Edit Entry", "Delete Entry", "Copy Entry", "Quit"],
style=minimalStyle,
).ask()
#######? Calling Functions #######
if choice == "Quit":
system("clear" if OSName == "posix" else "cls")
quit()
elif choice == "Add Entry":
AddEntry(table)
elif choice == "Edit Entry":
EditEntry(table)
elif choice == "Delete Entry":
DelEntry(table)
elif choice == "Copy Entry":
CopyEntry(table)
###################! Connecting To SQL ###################
if __name__ == "__main__":
#####* Clearing the Screen #####
ClearScreen()
#####* Loading bar #####
StatBar(2, desc="[cyan]Loading Password Manager")
#####* Encryption #####
encryption()
#####* Connecting To SQLite3 #####
conSQL()
#####* Printing Options #####
PrintOptions()