Skip to content

Commit 121b3ab

Browse files
committed
Changed structure
Now main script is single file instead of Python module
1 parent 695185b commit 121b3ab

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed
Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import random
55
import sqlite3
66
import string
7+
import sys
78
from getpass import getpass
89

910
import pyperclip
@@ -67,7 +68,7 @@ def show_records(records=None):
6768
if records is None:
6869
records = query('SELECT `name` FROM `passwords`;')
6970

70-
if records is None:
71+
if not records:
7172
print('Nothing to show!')
7273

7374
else:
@@ -81,21 +82,21 @@ def check_files():
8182

8283
# Settings file
8384
try:
84-
from PyPassword import settings
85-
except ImportError:
85+
open(file('salt.key'), 'rb')
86+
except FileNotFoundError:
8687
print('Settings file not found! Creating one...')
87-
open(file('settings.py'), 'x')
88+
open(file('salt.key'), 'x')
8889

8990
custom_salt = input(
9091
'Provide custom salt or leave empty for random (it should be super secret and super safe): ')
9192
if custom_salt == '':
9293
custom_salt = os.urandom(16)
9394

9495
else:
95-
custom_salt = "b'" + custom_salt + "'"
96+
custom_salt.encode()
9697

97-
with open(file('settings.py'), 'w') as f:
98-
f.write(f"# -*- coding: utf-8 -*-\nsalt = {custom_salt}\n")
98+
with open(file('salt.key'), 'wb') as f:
99+
f.write(custom_salt)
99100
finally:
100101
print('Settings OK!')
101102

@@ -168,28 +169,29 @@ def generate_key():
168169
password = password_input.encode()
169170

170171
try:
171-
from PyPassword import settings
172+
open(file('salt.key'), 'rb')
172173

173-
except ImportError:
174+
except FileNotFoundError:
174175
print('No settings are found! Please restart the program, so it could create them for you')
175176
confirm()
176177

177178
else:
178-
kdf = PBKDF2HMAC(
179-
algorithm=hashes.SHA256(),
180-
length=32,
181-
salt=settings.salt,
182-
iterations=100000,
183-
backend=default_backend()
184-
)
179+
with open(file('salt.key'), 'rb') as f:
180+
kdf = PBKDF2HMAC(
181+
algorithm=hashes.SHA256(),
182+
length=32,
183+
salt=f.read(),
184+
iterations=100000,
185+
backend=default_backend()
186+
)
185187

186188
key = base64.urlsafe_b64encode(kdf.derive(password))
187189

188190
try:
189191
with open(file('master.key'), 'wb') as f:
190192
f.write(key)
191193
except FileNotFoundError:
192-
print('File removed! Please do not remove it')
194+
print('File removed! Please do not remove it!')
193195
else:
194196
print('Key file generated')
195197

@@ -250,25 +252,23 @@ def del_password():
250252
show_records()
251253

252254
password_input = input('>>> ')
253-
to_del = query('SELECT `password` FROM `passwords` WHERE `name` LIKE ?;', [password_input])[0][0]
254-
if (n := type(to_del)) is not None:
255-
if n is bytes:
256-
del_confirm = input('If you want to proceed, please type once more password name: ')
257-
258-
with open(file('master.key'), 'rb') as f:
259-
key = f.read()
260-
f = Fernet(key)
261-
to_del = f.encrypt(to_del).decode('utf-8')
255+
if password_input != '':
256+
to_del = query('SELECT `password` FROM `passwords` WHERE `name` LIKE ?;', [password_input])[0][0]
257+
if (n := type(to_del)) is not None:
258+
if n is bytes:
259+
del_confirm = input('If you want to proceed, please type once more password name: ')
262260

263-
if to_del == del_confirm:
264-
query('DELETE FROM `passwords` WHERE `name` LIKE ?;', [to_del])
261+
if password_input == del_confirm:
262+
query('DELETE FROM `passwords` WHERE `name` LIKE ?;', [password_input])
265263

264+
else:
265+
print('Action cancelled')
266266
else:
267-
print('Action cancelled')
267+
raise SyntaxError('Bad password type, that\'s an error')
268268
else:
269-
raise SyntaxError('Bad password type, that\'s an error')
269+
raise KeyError('That password does not exits')
270270
else:
271-
raise KeyError('That password does not exits')
271+
print('Action cancelled')
272272

273273

274274
# Option 5
@@ -321,7 +321,9 @@ def quick_start():
321321
quick_start()
322322

323323
elif choice in ('0', 'E', 'e'):
324+
clear()
324325
print(f' * Thanks for using {Program.name}! *')
325-
exit()
326+
confirm()
327+
sys.exit()
326328

327329
confirm()

PyPassword/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)