-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_manager.py
268 lines (229 loc) · 9.89 KB
/
password_manager.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
import sqlite3
from prettytable import from_db_cursor
from sqlite3 import Error
from passlib.hash import pbkdf2_sha256
from cryptography.fernet import Fernet
import getpass
DB_FILE = "password_manager.db"
class PasswordManager:
def __init__(self) -> None:
self.password_manager = "password_manager"
self.master_password = "master_password"
if not self.isSqlite3():
self.createTables()
self.createMasterPassword()
def isSqlite3(self):
from os.path import isfile
if isfile(DB_FILE):
return True
return False
def createMasterPassword(self):
print(5 * "=" + " Create Master Password " + "=" * 5)
user = input("[+] Enter username(Unique): ")
master_pass1 = getpass.getpass("Enter password(Master): ")
master_pass2 = getpass.getpass("Confirm password: ")
if master_pass2 == master_pass1:
self.generateMasterHash(user, master_pass1)
else:
print("opps.. please try again")
self.createMasterPassword()
def createConnection(self):
"""
creating database connection with sqlite file
you can use local host mysql or another database.
"""
conn = None
try:
conn = sqlite3.connect(DB_FILE)
return conn
except Error as e:
print(e)
return conn
def generateMasterHash(self, user, master_pass):
try:
conn = self.createConnection()
sql_qury_master = f"insert into {self.master_password} (user_name,password,master_key) VALUES(?,?,?)"
cursor = conn.cursor()
# creating encrypted pass hash
encrypted_hash = pbkdf2_sha256.hash(master_pass)
key = Fernet.generate_key()
insert_data = (user, encrypted_hash, key)
cursor.execute(sql_qury_master, insert_data)
conn.commit()
print("[+] Master password hash is generated.")
except Error as e:
print(e)
def createTables(self):
"""
Creating mater password for password manager
this is the one time password for
"""
sql_manager_table = f"CREATE TABLE IF NOT EXISTS {self.password_manager} (user_id INTEGER PRIMARY KEY AUTOINCREMENT,user_name varchar(50) UNIQUE,app_name varchar(100),pass_hash varchar(255), password varchar(255))"
sql_master = f"CREATE TABLE IF NOT EXISTS {self.master_password} (user_name varchar(50) UNIQUE, password varchar(255),master_key varchar(255))"
try:
conn = self.createConnection()
cursor = conn.cursor()
cursor.execute(sql_manager_table)
cursor.execute(sql_master)
except Error as e:
print(e)
def insertData(self, data):
"""
Insert data to the database with tuple parameter,
like. username, app or website and password
"""
conn = self.createConnection()
sql = f"INSERT INTO {self.password_manager}(user_name,app_name,pass_hash,password) VALUES(?,?,?,?)"
cursor = conn.cursor()
cursor.execute(sql, data)
conn.commit()
print("[+] Data Inserted successfully.")
def updateData(self, data):
"""
update data to the database with tuple parameter,
like. username, app or website and password
while update data, you will be asked master password
"""
conn = self.createConnection()
sql = f"UPDATE {self.password_manager} SET user_name = ?, app_name = ?, password = ?,pass_hash =? WHERE password=? and user_name=?"
cursor = conn.cursor()
cursor.execute(sql, data)
conn.commit()
print("Data successfully Updated")
def displayAll(self, master_pass):
_, master_hash, _ = self.getMasterPasswordHash()
master = pbkdf2_sha256.verify(master_pass, master_hash)
if master:
conn = self.createConnection()
try:
with conn:
cur = conn.cursor()
cur.execute(
f"select user_id,user_name, app_name,pass_hash from {self.password_manager}"
)
x = from_db_cursor(cur)
print(x)
except Error as e:
print(e)
except IndexError as ie:
return "Data Not found."
def getMasterPasswordHash(self):
"""
Retrieving master password hash from the table
"""
conn = self.createConnection()
cur = conn.cursor()
cur.execute(f"select * from {self.master_password}")
data = cur.fetchall()
return data[0]
def verifyPassword(self, master_pass):
_, hash, _ = self.getMasterPasswordHash()
result = pbkdf2_sha256.verify(master_pass, hash)
return result
def showPassword(self, data):
conn = self.createConnection()
with conn:
cur = conn.cursor()
cur.execute(
f"select user_id,user_name, app_name,password from {self.password_manager} where user_name=? and app_name=?",
data,
)
x = from_db_cursor(cur)
print(x)
def main():
psm = PasswordManager()
while True:
home_banner = """
[============= Home Menu =============]
[1] Manage Applications passwords
[2] Update Master Password
[0] Exit
"""
app_banner = """
[============= Application Menu =============]
[1] Add password for Application or Website
[2] update Password
[3] Show Password
[4] Show all data
[0] Exit
"""
print(home_banner)
choice = input("[+] Choice: ")
# match case work only python >= 3.10
match choice:
case "0":
print("\nBye....")
break
case "2":
print("update master password")
print("implement this by own")
case "1":
while True:
app_extension = "[App][+] "
print(app_banner)
choice2 = input(app_extension + "choice: ")
match choice2:
case "0":
print("[+] Application Menu Quit...")
break
case "1":
print(6 * "=" + " To Add data to the database " + "=" * 6)
user = input(app_extension + "Enter username: ")
app_name = input(
app_extension + "Enter (App name or Website): "
)
password = getpass.getpass(
app_extension + "Enter password: "
)
_, _, key = psm.getMasterPasswordHash()
f = Fernet(key)
encryped_hash = f.encrypt(bytes(password, "utf-8"))
data = (user, app_name, encryped_hash, password)
psm.insertData(data=data)
case "2":
print(6 * "=" + " To Update data " + "=" * 6)
master = getpass.getpass(
app_extension + "Enter password(Master): "
)
result = psm.verifyPassword(master)
if result:
print("please enter existing data first")
user = input("[+] Enter username(existing): ")
password = getpass.getpass(
"[+] Enter password(existing web or app): "
)
print(6 * "=" + " Please Enter new data " + "=" * 6)
new_user = input("[+] Enter new username: ")
new_app_name = input(
"[+] Enter new (App name or Website): "
)
new_password = getpass.getpass(
"[+] Enter new password: "
)
_, _, key = psm.getMasterPasswordHash()
f = Fernet(key)
encryped_hash = f.encrypt(bytes(password, "utf-8"))
data = (
new_user,
new_app_name,
new_password,
encryped_hash,
password,
user,
)
psm.updateData(data)
case "3":
print(6 * "=" + " To Show Password " + "=" * 6)
user = input("[+] Enter username(existing): ")
app = input("[+] Enter (existing web or app): ")
data = (user, app)
psm.showPassword(data)
case "4":
master = getpass.getpass("[+] Enter password(Master): ")
psm.displayAll(master_pass=master)
case default:
return "[+] Invalid Choice!"
case default:
return
if __name__ == "__main__":
main()