-
Notifications
You must be signed in to change notification settings - Fork 0
/
usman_pwmng.py
329 lines (226 loc) · 7.96 KB
/
usman_pwmng.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
#!/usr/bin/env python
# coding: utf-8
# In[3]:
import sqlite3, hashlib #For database
from tkinter import *
from tkinter import simpledialog
from functools import partial
import uuid #For recovery key
import pyperclip #To copy the recovery key
import base64 #To encrypt the data
import os
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.backends import default_backend
from cryptography.fernet import Fernet
backend = default_backend()
salt = b'2444'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length = 32,
salt = salt,
iterations=100000,
backend=backend
)
encryptionKey = 0
def encrypt(message: bytes, key: bytes) -> bytes:
return Fernet(key).encrypt(message)
def decrypt(message: bytes, token: bytes) -> bytes:
return Fernet(token).decrypt(message)
#Creating a database
with sqlite3.connect("My_Password_Manager.db") as db:
cursor = db.cursor()
#creating a table
cursor.execute("""
CREATE TABLE IF NOT EXISTS masterpassword(
id INTEGER PRIMARY KEY,
password TEXT NOT NULL,
recoveryKey TEXT NOT NULL);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS vault(
id INTEGER PRIMARY KEY,
application TEXT NOT NULL,
username TEXT NOT NULL,
password TEXT NOT NULL);
""")
#Create PopUps
def popUp(text):
answer = simpledialog.askstring("input string", text)
return(answer)
#Initializing the window
window = Tk()
window.update()
window.title("Password Vault")
def Hash_Password(input):
hash1 = hashlib.sha256(input)
hash1 = hash1.hexdigest()
return hash1
def Home_Screen():
for widget in window.winfo_children():
widget.destroy()
window.geometry("500x250")
lbl = Label(window, text="Create a Master Password")
lbl.config(anchor=CENTER)
lbl.pack()
txt = Entry(window, width=50, show = "*")
txt.pack()
txt.focus()
lbl1 = Label(window, text="Re-enter Password")
lbl1.config(anchor=CENTER)
lbl1.pack()
txt1 = Entry(window, width=50, show = "*")
txt1.pack()
txt1.focus()
def Save_Password():
if txt.get() == txt1.get():
sql = "DELETE FROM masterpassword WHERE ID = 1"
cursor.execute(sql)
hashedpassword = Hash_Password(txt.get().encode('utf-8'))
key = str(uuid.uuid4().hex)
recoveryKey = Hash_Password(key.encode('utf-8'))
global encryptionKey
encryptionKey = base64.urlsafe_b64encode(kdf.derive(txt.get().encode()))
insert_password = """INSERT INTO masterpassword(password, recoveryKey)
VALUES(?, ?) """
cursor.execute(insert_password, ((hashedpassword), (recoveryKey)))
db.commit()
Recovery_Screen(key)
else:
lbl1.config(text="Passwords do not match :(")
btn = Button(window, text = "Save", command=Save_Password)
btn.pack(pady=5)
def Recovery_Screen(key):
for widget in window.winfo_children():
widget.destroy()
window.geometry("500x250")
lbl = Label(window, text="Please save this key for you to recover your Vault.")
lbl.config(anchor=CENTER)
lbl.pack()
lbl1 = Label(window, text=key)
lbl1.config(anchor=CENTER)
lbl1.pack()
def Copy_Key():
pyperclip.copy(lbl1.cget("text"))
btn = Button(window, text = "Copy Key", command=Copy_Key)
btn.pack(pady=5)
def Done():
Password_Vault()
btn = Button(window, text = "Done", command=Done)
btn.pack(pady=5)
def Reset_Screen():
for widget in window.winfo_children():
widget.destroy()
window.geometry("500x250")
lbl = Label(window, text="Please Enter Recovery Key")
lbl.config(anchor=CENTER)
lbl.pack()
txt = Entry(window, width=20)
txt.pack()
txt.focus()
lbl1 = Label(window)
lbl1.config(anchor=CENTER)
lbl1.pack()
def Get_Recovery_Key():
recoveryKeyCheck = Hash_Password(str(txt.get()).encode('utf-8'))
cursor.execute('SELECT * FROM masterpassword WHERE id = 1 AND recoveryKey = ?', [(recoveryKeyCheck)])
return cursor.fetchall()
def Check_Recovery_Key():
checked = Get_Recovery_Key()
if checked:
Home_Screen()
else:
txt.delete(0, 'end')
lbl1.config(text='You have entered the wrong key')
btn = Button(window, text = "Check Key", command=Check_Recovery_Key)
btn.pack(pady=5)
def Login_Screen():
window.geometry("350x170")
lbl = Label(window, text="Please Enter Master Password")
lbl.config(anchor=CENTER)
lbl.pack()
txt = Entry(window, width=20, show="*")
txt.pack()
txt.focus()
lbl1 = Label(window)
lbl1.config(anchor=CENTER)
lbl1.pack(side=TOP)
def getMasterPassword():
checkhashedpassword = Hash_Password(txt.get().encode('utf-8'))
global encryptionKey
encryptionKey = base64.urlsafe_b64encode(kdf.derive(txt.get().encode()))
cursor.execute("SELECT * FROM masterpassword WHERE id = 1 AND password = ?", [(checkhashedpassword)])
print(checkhashedpassword)
return cursor. fetchall()
def Check_Password():
match = getMasterPassword()
print(match)
if match:
Password_Vault()
else:
txt.delete(0, 'end')
lbl1.config(text="You have entered the wrong password!")
def Reset_Password():
Reset_Screen()
btn = Button(window, text = "Submit", command=Check_Password)
btn.pack(pady=1)
btn = Button(window, text = "Reset Password", command=Reset_Password)
btn.pack(pady=1)
def Password_Vault():
for widget in window.winfo_children():
widget.destroy()
def addEntry():
text1 = "Application"
text2 = "Username"
text3 = "Password"
application = encrypt(popUp(text1).encode(), encryptionKey)
username = encrypt(popUp(text2).encode(), encryptionKey)
password = encrypt(popUp(text3).encode(), encryptionKey)
insert_fields = """Insert INTO vault(application, username, password)
VALUES(?, ?, ?)"""
cursor.execute(insert_fields, (application, username, password))
db.commit()
Password_Vault()
def removeEntry(input):
cursor.execute("DELETE FROM vault WHERE id = ?", (input,))
db.commit()
Password_Vault()
window.geometry("1050x700")
window.resizable(height=None, width=None)
lbl = Label(window, text="Welcome to Acid's Password Vault!")
lbl.grid(column=1)
btn = Button(window, text = "+", command = addEntry)
btn.grid(column=1, pady=10)
lbl = Label(window, text="Application")
lbl.grid(row=2, column=0, padx=80)
lbl = Label(window, text="Username")
lbl.grid(row=2, column=1, padx=80)
lbl = Label(window, text="Password")
lbl.grid(row=2, column=2, padx=80)
cursor.execute("SELECT * FROM vault")
if(cursor.fetchall() != None):
i = 0
while True:
cursor.execute('SELECT * FROM vault')
array = cursor.fetchall()
if (len(array) == 0):
break
lbl1 = Label(window, text=(decrypt(array[i][1], encryptionKey)))
lbl1.grid(column=0, row=(i+3))
lbl2 = Label(window, text=(decrypt(array[i][2], encryptionKey)))
lbl2.grid(column=1, row=(i+3))
lbl3 = Label(window, text=(decrypt(array[i][3], encryptionKey)))
lbl3.grid(column=2, row=(i+3))
btn = Button(window, text="Delete", command= partial(removeEntry, array[i][0]))
btn.grid(column=3, row=i+3, pady=10)
i=i+1
cursor.execute("SELECT * FROM vault")
if (len(cursor.fetchall()) <= i):
break
cursor.execute("SELECT * FROM masterpassword")
if cursor.fetchall():
Login_Screen()
else:
Home_Screen()
window.mainloop()
# In[ ]: