-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaKey.py
242 lines (158 loc) · 6.05 KB
/
SaKey.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
import hashlib, os, random, struct, time, sys, secrets, string
from Crypto.Cipher import AES
from getpass import getpass
from secure_delete import secure_delete
secure_delete.secure_random_seed_init()
sake = '1'
def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
if not out_filename:
out_filename = in_filename + '.db'
iv = os.urandom(16)
encryptor = AES.new(key, AES.MODE_CBC, iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += b' ' * (16 - len(chunk) % 16)
outfile.write(encryptor.encrypt(chunk))
return out_filename
def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
if not out_filename:
out_filename = os.path.splitext(in_filename)[0]
with open(in_filename, 'rb') as infile:
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
iv = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, iv)
with open(out_filename, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(decryptor.decrypt(chunk))
outfile.truncate(origsize)
return out_filename
os.system('clear')
sys.stdout.write("\x1b]2;| SaKey AuthSystem |"'\x07')
print('Please insert your USB key\n')
while True:
if os.path.isdir('/Volumes/SaKey'):
print('USB Key found!\n')
time.sleep(1)
break
else:
continue
while True:
if os.path.exists('/Volumes/SaKey/auth.key'):
print('Loading auth file...\n')
time.sleep(1)
os.system('clear')
break
else:
continue
password = getpass('Please enter your password: ')
os.system('clear')
password = password.encode('utf-8')
hash_object = hashlib.sha512(password)
hex_dig = hash_object.hexdigest()
key = hashlib.sha256(password).digest()
for x in range(3):
print('Authenticating...')
time.sleep(0.5)
os.system('clear')
f = open("/Volumes/SaKey/auth.key", "r")
f = f.read()
if f == hex_dig:
print('Authenticated')
sys.stdout.write("\x1b]2;| SaKey |"'\x07')
time.sleep(2)
os.system('clear')
else:
print('Incorrect password')
os.system('exit')
while sake == '1':
os.system('clear')
print("""
███████╗ █████╗ ██╗ ██╗███████╗██╗ ██╗
██╔════╝██╔══██╗██║ ██╔╝██╔════╝╚██╗ ██╔╝
███████╗███████║█████╔╝ █████╗ ╚████╔╝
╚════██║██╔══██║██╔═██╗ ██╔══╝ ╚██╔╝
███████║██║ ██║██║ ██╗███████╗ ██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═╝
""")
print('=========================================')
print(' Welcome to your SaKey')
print('=========================================')
print('\n1 :: View your database')
print('2 :: Add to your database')
print('3 :: Clear your database')
print('4 :: Generate a password')
print('99 :: Exit')
choice = input('\nChoice: ')
if choice == '1':
os.system('clear')
try:
decrypt_file(key, '/Volumes/SaKey/SaKey.db')
except:
print('ERROR: Could not decrypt database')
continue
try:
contents = open("/Volumes/SaKey/SaKey", "r")
contents = contents.read()
print(contents)
except:
print('ERROR: Could not open database')
continue
secure_delete.secure_delete('/Volumes/SaKey/SaKey')
gone = input('\nPress return to continue')
os.system('clear')
elif choice == '2':
os.system('clear')
addToDB = input('Line to add to database: ')
decrypt_file(key, '/Volumes/SaKey/SaKey.db')
file_out = open("/Volumes/SaKey/SaKey", "a")
file_out.write('\n\n')
file_out.write(addToDB)
file_out.close()
encrypt_file(key, '/Volumes/SaKey/SaKey')
secure_delete.secure_delete('/Volumes/SaKey/SaKey')
os.system('clear')
elif choice == '3':
os.system('clear')
secure_delete.secure_delete('/Volumes/SaKey/SaKey.db')
file_out = open("/Volumes/SaKey/SaKey", "a")
file_out.write('Sake DB:')
file_out.write('\n')
file_out.write('\n')
file_out.close()
encrypt_file(key, '/Volumes/SaKey/SaKey')
secure_delete.secure_delete('/Volumes/SaKey/SaKey')
print('Database has been cleared')
gone = input('\nPress return to continue')
os.system('clear')
elif choice == '4':
os.system('clear')
pass_label = input('Label for the password: ')
pass_length = int(input('Length of the password: '))
alphabet = string.ascii_letters + string.digits + string.punctuation
password = ''.join(secrets.choice(alphabet) for i in range(pass_length))
decrypt_file(key, '/Volumes/SaKey/SaKey.db')
file_out = open("/Volumes/SaKey/SaKey", "a")
file_out.write('\n\n')
file_out.write(f'{pass_label} : {password}')
file_out.close()
encrypt_file(key, '/Volumes/SaKey/SaKey')
secure_delete.secure_delete('/Volumes/SaKey/SaKey')
gone = input('\nPress return to continue')
os.system('clear')
elif choice == '99':
os.system('clear')
sake = 'sake'
os.system('exit')
else:
print('Please enter a valid option')