-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
296 lines (240 loc) · 8.33 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
#! /usr/bin/env python
# coding: utf-8
from Mongo import Mongo
db = Mongo()
from MyCrypto import MyCrypto
from cursesmenu import *
from cursesmenu.items import *
import time
import getpass
import os
from terminaltables import AsciiTable
from halo import Halo
# global variables
menu = []
logged_user = []
def get_password():
while (True):
password = getpass.getpass("Account password:")
# hashing password
hash_password = MyCrypto().hash_password(password)
if(hash_password == logged_user["password"]):
return password
else:
print("Incorrect password! Try again...")
def register():
# create user
user = raw_input("Enter your username:")
while (db.get_user(user)):
user = raw_input("This username exists!\nEnter your username:")
while True:
password = getpass.getpass("Password for " + user + ":")
password_again = getpass.getpass("Password for " + user + " again:")
if(password == password_again):
break
else:
print("The passwords you entered aren't the same!")
# hashing password
password = MyCrypto().hash_password(password)
# create json
data = dict([("user", user), ("password", password),
("web_data", [])])
# insert in my db
try:
x = Halo().start('Creating user')
db.insert_user(data)
x.succeed(user + 'created!')
except:
x.fail("Error on insert, try again.")
time.sleep(1.5)
def sign_in():
user = raw_input("Username:")
while (not db.get_user(user)):
user = raw_input("This username does not exists!\nUsername:")
while (True):
user_on = db.get_user(user)
password = getpass.getpass("Password:")
# hashing password
password = MyCrypto().hash_password(password)
if(password == user_on["password"]):
break
else:
print("Incorrect password! Try again...")
create_logged_menu(user_on)
def change_password():
global logged_user
# type old password
password = getpass.getpass("Your current password:")
# check password
if(MyCrypto().hash_password(password) == logged_user["password"]):
# type new password
new_password = getpass.getpass("New password:")
while True:
password_again = getpass.getpass(
"New password again:")
if(new_password == password_again):
break
else:
print("The passwords you entered aren't the same!")
# update website passwords
new_web_data = []
for x in logged_user["web_data"]:
# decrypt password
decrypt_password = MyCrypto().decrypt(x["password"].decode(
'hex'), password)
# encrypt and change password
x["password"] = MyCrypto().encrypt(
decrypt_password, new_password).encode('hex')
# append
new_web_data.append(x)
logged_user["web_data"] = new_web_data
# update user
logged_user["password"] = MyCrypto().hash_password(new_password)
db.update_password(logged_user)
# update webdata
db.insert_website(logged_user)
print('Changed password!')
time.sleep(1.5)
else:
print("Incorrect password!")
time.sleep(1.5)
def create_website():
# create website
password_acc = get_password()
website_name = raw_input("Website name:")
while True:
password = getpass.getpass("Password for " + website_name + ":")
password_again = getpass.getpass(
"Password for " + website_name + " again:")
if(password == password_again):
break
else:
print("The passwords you entered aren't the same!")
# encrypt password
password = MyCrypto().encrypt(password, password_acc)
# convert to hex
password = password.encode('hex')
# create json
logged_user["web_data"].append(
{"website": website_name, "password": password})
# insert in my db
try:
x = Halo().start('Creating website...')
db.insert_website(logged_user)
x.succeed("Website " + website_name + " added!")
except:
x.fail("Error on insert, try again.")
time.sleep(1)
def read_website():
website_name = raw_input("Website name:")
password = get_password()
# get website data
website = db.get_website(logged_user, website_name)
# website doesn't exist
if(len(website) == 1):
print("This website does not exists!")
time.sleep(1)
else:
# get pass(hex) -> decode hex -> decrypt
print(MyCrypto().decrypt(website["web_data"][0]["password"].decode(
'hex'), password))
time.sleep(3)
def remove_website():
global logged_user
website_name = raw_input("Website name:")
password = get_password()
# get website data
website = db.get_website(logged_user, website_name)
# website doesn't exist
if(len(website) == 1):
print("This website does not exists!")
time.sleep(1)
else:
# get pass(hex) -> decode hex -> decrypt
db.remove_website(logged_user, website["web_data"][0]["website"])
print("Website removed!")
time.sleep(1.5)
# update logged user
logged_user = db.get_user(logged_user["user"])
def show_table():
password = get_password()
table_data = [["Site", "Password"]]
for x in logged_user["web_data"]:
# insert each website
table_data.append([x["website"], MyCrypto().decrypt(x["password"].decode(
'hex'), password)])
table = AsciiTable(table_data)
print(table.table)
gambs = raw_input("(Press enter to back)")
def modify_website():
global logged_user
password_acc = get_password()
website_name = raw_input("Website name:")
# get website data
website = db.get_website(logged_user, website_name)
# website doesn't exist
if(len(website) == 1):
print("This website does not exists!")
time.sleep(1)
else:
# get pass(hex) -> decode hex -> decrypt
# remove website
db.remove_website(logged_user, website["web_data"][0]["website"])
logged_user = db.get_user(logged_user["user"])
# insert website
website_name = raw_input("New website name:")
while True:
password = getpass.getpass(
"New password for " + website_name + ":")
password_again = getpass.getpass(
"New password for " + website_name + " again:")
if(password == password_again):
break
else:
print("The passwords you entered aren't the same!")
# encrypt password
password = MyCrypto().encrypt(password, password_acc)
# convert to hex
password = password.encode('hex')
# create json
logged_user["web_data"].append(
{"website": website_name, "password": password})
# insert in my db
db.insert_website(logged_user)
print("Website " + website_name + " updated!")
time.sleep(1)
##
# Menu Functions
##
def create_logged_menu(user):
# remove sign in
menu.items.pop(0)
# change subtitle
menu.subtitle = "User:" + user["user"]
show_all = FunctionItem("Show all website accounts", show_table)
create_web = FunctionItem("Create website account", create_website)
read_web = FunctionItem("Read website account", read_website)
read_web = FunctionItem("Modify website account", modify_website)
delete_web = FunctionItem("Delete website account", remove_website)
manage_account = FunctionItem("Change your password", change_password)
menu.append_item(show_all)
menu.append_item(create_web)
menu.append_item(read_web)
menu.append_item(delete_web)
menu.append_item(manage_account)
# remove register
menu.items.pop(0)
# set 'global' user
global logged_user
logged_user = user
def create_menu():
global menu
menu = CursesMenu(
"Password Manager - Created by: Vinicius Romano, Higor Rozan", 'Disconnected')
item_register = FunctionItem("Sign in", sign_in)
item_login = FunctionItem("Register", register)
menu.append_item(item_register)
menu.append_item(item_login)
menu.show()
if __name__ == '__main__':
create_menu()