Skip to content

Commit f12188a

Browse files
committed
FIX/ADD: error handler/compile.bat
1 parent 2475173 commit f12188a

File tree

3 files changed

+73
-34
lines changed

3 files changed

+73
-34
lines changed

compile.bat

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@echo off
2+
3+
set sourcefile=%1
4+
IF [%sourcefile%]==[] ( goto :NO_PARAMS ) ELSE ( goto :OPERATE )
5+
6+
:NO_PARAMS
7+
echo No sourcefile specified
8+
exit 1
9+
10+
:OPERATE
11+
pyinstaller --onefile %sourcefile%.py >NUL 2>NUL
12+
del "%sourcefile%.spec"
13+
@RD /S /Q "build"
14+
@RD /S /Q "__pycache__"
15+
copy .\dist\%sourcefile%.exe .\ >NUL 2>NUL
16+
@RD /S /Q "dist"
17+
goto :DONE
18+
19+
:DONE
20+
echo Sourcefile compiled successfully
21+
pause

passwdmanager.exe

-9.54 MB
Binary file not shown.

passwdmanager.py

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import base64
33
import getpass
44
import mysql.connector
5+
import cryptography
56
from cryptography.fernet import Fernet
67
from cryptography.hazmat.backends import default_backend
78
from cryptography.hazmat.primitives import hashes
@@ -37,24 +38,28 @@ def setup():
3738
main_passwd = getpass.getpass('Enter main password: ')
3839

3940
# Database connection
40-
mydb = mysql.connector.connect(
41-
host='localhost', # Change this if you have dedicated Database
42-
user=(login_user),
43-
password=(mysql_passwd)
44-
)
41+
try:
42+
mydb = mysql.connector.connect(
43+
host='localhost', # Change this if you have dedicated Database
44+
user=(login_user),
45+
password=(mysql_passwd)
46+
)
47+
except mysql.connector.Error as err:
48+
print(f"\nCan't connect to Database, error: {err}")
49+
exit(1)
4550
mysql_cursor = mydb.cursor(buffered=True)
4651
id_cursor = mydb.cursor(buffered=True)
4752
password_cursor = mydb.cursor(buffered=True)
4853
salt_cursor = mydb.cursor(buffered=True)
49-
db_setup()
54+
return db_setup()
5055

5156
def db_setup():
5257
# Check if Database exist
5358
mysql_cursor.execute(f'CREATE DATABASE IF NOT EXISTS db_password_{login_user}')
5459

5560
# Check if Table exist
5661
mysql_cursor.execute(f'CREATE TABLE IF NOT EXISTS db_password_{login_user}.tb_{login_user} (id INT NOT NULL AUTO_INCREMENT,name VARCHAR(255) NOT NULL,tag VARCHAR(255), password BLOB NOT NULL, salt BLOB NOT NULL, PRIMARY KEY (id))')
57-
menu() # Forward to menu
62+
return menu() # Forward to menu
5863

5964
def menu():
6065
print('\nWelcome!')
@@ -65,30 +70,34 @@ def menu():
6570
cmd = input('> ').lower()
6671

6772
if cmd == 'v':
68-
view()
73+
return view()
6974
elif cmd == 'i':
70-
insert()
75+
return insert()
7176
elif cmd == 'd':
72-
delete()
77+
return delete()
7378
elif cmd == 'q':
7479
print('\nBye!')
75-
return 0
80+
exit(0)
7681
else:
7782
print('\nNot an option!')
7883
return menu()
7984

8085
def db_check():
81-
# Query id, name
82-
mysql_cursor.execute(f"SELECT id, name FROM db_password_{login_user}.tb_{login_user}")
83-
myresult = mysql_cursor.fetchall()
84-
85-
# If query return 0
86-
if len(myresult) == 0:
87-
print('Nothing to show.')
88-
else: # If there's data
89-
for item in myresult:
90-
print(item)
91-
pass
86+
try:
87+
# Query id, name
88+
mysql_cursor.execute(f"SELECT id, name FROM db_password_{login_user}.tb_{login_user}")
89+
myresult = mysql_cursor.fetchall()
90+
# If query return 0
91+
if len(myresult) == 0:
92+
print('Nothing to show.')
93+
return menu()
94+
else: # If there's data
95+
for item in myresult:
96+
print(item)
97+
pass
98+
except mysql.connector.Error as err:
99+
print(f'Error query data from database, error: {err}')
100+
exit(1)
92101

93102
def view():
94103
db_check()
@@ -105,6 +114,7 @@ def view():
105114
return view()
106115
except:
107116
print('\nInvalid ID')
117+
return view()
108118

109119
# Query id, name, salt, password
110120
id_cursor.execute(f"SELECT id,name FROM db_password_{login_user}.tb_{login_user} WHERE id={view_id}")
@@ -117,10 +127,14 @@ def view():
117127
password_for_view = password_cursor.fetchall()[0][0]
118128

119129
kdf(salt_for_view) # Pass salt to KDF module
120-
decrypted = crypter.decrypt(password_for_view) # Decrypt password
121-
showed_password = decrypted.decode() # Decode byte object to normal string
122-
print("Password for",id_for_view,"is",showed_password)
123-
menu()
130+
try:
131+
decrypted = crypter.decrypt(password_for_view) # Decrypt password
132+
showed_password = decrypted.decode() # Decode byte object to normal string
133+
print("Password for",id_for_view,"is",showed_password)
134+
except cryptography.fernet.InvalidToken:
135+
print('\nInvalid key (main password), please re-open the application')
136+
exit(1)
137+
return menu()
124138

125139
def insert():
126140
name = input('Name: ')
@@ -129,21 +143,20 @@ def insert():
129143

130144
if name == '' or password == '':
131145
print('Please enter something')
146+
return view()
132147
else:
133148
try:
134149
salt = salter() # Gen Salt
135150
kdf(salt) # Pass salt to KDF module
136151
password = crypter.encrypt(password.encode()) # Encrypt password(.encode() to turn into byte object)
137152

138153
# Insert name, tag, password, salt to Database
139-
sql = f"INSERT INTO db_password_{login_user}.tb_{login_user} (name, tag, password, salt) VALUES (%s, %s, %s, %s)"
140-
value = (name, tag, password, salt)
141-
mysql_cursor.execute(sql, value)
154+
mysql_cursor.execute(f"INSERT INTO db_password_{login_user}.tb_{login_user} (name, tag, password, salt) VALUES ({name}, {tag}, {password}, {salt})")
142155
mydb.commit()
143156
print(mysql_cursor.rowcount, 'password inserted')
144-
menu()
145157
except:
146158
print('Something wrong')
159+
return menu()
147160

148161
def delete():
149162
db_check()
@@ -153,15 +166,20 @@ def delete():
153166

154167
if del_id == '':
155168
print('Invalid id')
169+
return delete()
156170
else:
157171
if del_confirm == 'y':
158172
# Delete entire row by ID
159-
mysql_cursor.execute(f"DELETE FROM db_password_{login_user}.tb_{login_user} WHERE id ={del_id}")
160-
mydb.commit()
161-
print(mysql_cursor.rowcount, "name and password deleted")
162-
menu()
173+
try:
174+
mysql_cursor.execute(f"DELETE FROM db_password_{login_user}.tb_{login_user} WHERE id ={del_id}")
175+
mydb.commit()
176+
print(mysql_cursor.rowcount, "name and password deleted")
177+
except:
178+
print('Something wrong')
179+
return menu()
163180
else:
164181
print("Cancled...")
182+
return menu()
165183

166184
def salter():
167185
return os.urandom(16) # Generate random byte for lenght of 16

0 commit comments

Comments
 (0)