-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.py
More file actions
384 lines (244 loc) · 12.3 KB
/
FileHandler.py
File metadata and controls
384 lines (244 loc) · 12.3 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from dotenv import load_dotenv
from hashlib import sha256
from os import getenv, listdir, path, remove
from requests import exceptions, get
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from uuid import uuid4
from datetime import datetime
from DatabaseHandler import MongoDB
class DataHandler():
def __init__(self):
load_dotenv()
mongodb_token = getenv('MONGODB_TOKEN')
self.MongoDB = MongoDB(mongodb_token)
# Function to check for internet connection.
def __checkConnection(self):
try:
get('http://www.google.com', timeout = 5)
return True
except exceptions.RequestException:
return False
def __fragmentAndEncrypt(self, file_content, fragment_size, key):
fragment_list, fragment_count = listdir('Data'), 0
temporary_fragment_list = []
while True:
fragment = file_content[fragment_count * fragment_size: (fragment_count + 1) * fragment_size]
if not fragment:
break
fragment_count += 1
while True:
random_file_name = str(uuid4())
if random_file_name not in fragment_list:
break
# Stores this as a file in 'Data' directory in the current directory
with open(f'Data/{random_file_name}', 'wb') as stream:
stream.write(fragment)
temporary_fragment_list.append(random_file_name)
fragment_list.append(random_file_name)
fragment_file = '\n'.join(temporary_fragment_list)
cipher = AES.new(key, AES.MODE_EAX)
encrypted_fragment_file, tag = cipher.encrypt_and_digest(fragment_file.encode('utf-8'))
encrypted_fragment_file = cipher.nonce + encrypted_fragment_file + tag
return encrypted_fragment_file
# encrypted_fragment_file = AES.new(key, AES.MODE_EAX).encrypt(fragment_file.encode('utf-8'))
def __encryptFile(self, file, key):
with open(file.name, 'rb') as stream:
file_data = stream.read()
cipher = AES.new(key, AES.MODE_EAX)
encrypted_data, tag = cipher.encrypt_and_digest(file_data)
encrypted_data = cipher.nonce + encrypted_data + tag
return file_data, encrypted_data
def __decryptAndCombineFragment(self, fragment, key):
dcipher = AES.new(key, AES.MODE_EAX, nonce = fragment[:16])
decrypted_data = dcipher.decrypt_and_verify(fragment[16:-16], fragment[-16:])
decrypted_data = decrypted_data.decode('utf-8')
combined_file = b''
for fragment in decrypted_data.split('\n'):
with open(f'Data/{fragment}', 'rb') as stream:
combined_file += stream.read()
return combined_file
def __decryptFragment(self, fragment, key):
dcipher = AES.new(key, AES.MODE_EAX, nonce = fragment[:16])
decrypted_data = dcipher.decrypt_and_verify(fragment[16:-16], fragment[-16:])
decrypted_data = decrypted_data.decode('utf-8')
decrypted_data = decrypted_data.split('\n')
return decrypted_data
def __decryptFile(self, file, key):
cipher = AES.new(key, AES.MODE_EAX, nonce = file[:16])
decrypted_data = cipher.decrypt_and_verify(file[16:-16], file[-16:])
return decrypted_data
def __hashText(self, text):
return sha256(text.encode()).hexdigest()
def __keyDerivationFunction(self, username, password, file_password):
result = ""
for index in range(len(username)):
result += chr(ord(username[index]) ^ ord(password[index]) ^ ord(file_password[index]))
# result = result[:len(result)//2] + result[len(result)//2:]
result = self.__hashText(result)
result = result[:32], result[32:]
new_result = ""
for index in range(32):
new_result += chr(ord(result[0][index]) ^ ord(result[1][index]))
return new_result
def createAccount(self, credentials):
if not self.__checkConnection():
pass
# return '0'
username = credentials['username'].lower()
password = credentials['password']
if not username or not password:
return '1'
display_name = credentials['display_name']
username_hash = self.__hashText(username)
password_hash = self.__hashText(password)
payload = {
'username': username_hash,
'password': password_hash,
'display_name': display_name,
'type': 'regular',
'standing': 10.0,
'status': 'active',
'allowed_storage': 5120,
'consumed_storage': 0,
'files': []
}
if not self.MongoDB.checkExistance(username_hash):
self.MongoDB.createUser(payload)
return self.MongoDB.retrieveUser(username_hash)
else:
return '2'
def loginAccount(self, credentials):
if not self.__checkConnection():
pass
# return '0'
username = credentials['username'].lower()
password = credentials['password']
username_hash = self.__hashText(username)
password_hash = self.__hashText(password)
if self.MongoDB.checkExistance(username_hash):
user_credentials = self.MongoDB.retrieveUser(username_hash)
if user_credentials['password'] == password_hash:
if user_credentials['status'] == 'suspended':
return '3'
return user_credentials
else:
return '1'
else:
return '2'
def deleteAccount(self, credentials):
if not self.__checkConnection():
pass
# return '0'
username = credentials['username'].lower()
password = credentials['password']
username_hash = self.__hashText(username)
password_hash = self.__hashText(password)
if self.MongoDB.checkExistance(username_hash):
user = self.MongoDB.retrieveUser(username_hash)
if user['password'] == password_hash:
self.MongoDB.deleteUser(username_hash, password_hash)
else:
return '1'
else:
return '2'
def updateDisplayName(self, display_name, username):
if self.MongoDB.updateDisplayName(username, display_name):
return True
else:
return False
def uploadFile(self, file, file_password, fragment_size, credentials, file_list, consumed_storage, allowed_storage):
if not self.__checkConnection():
pass
# return '0'
username = credentials['username']
password = credentials['password']
file_password_hash = self.__hashText(file_password)
if self.MongoDB.checkExistance(username):
user = self.MongoDB.retrieveUser(username)
if user['password'] == password:
if user['status'] == 'suspended':
return '3'
name = file.name.split('/')[-1]
extension, name = name.split('.')[-1], '.'.join(name.split('.')[:-1])
size = path.getsize(file.name)
if consumed_storage + round((size / 1024) / 1024, 3) > allowed_storage:
return '4'
file_password_hash = self.__hashText(file_password)
key = self.__keyDerivationFunction(username, password, file_password_hash)
key_hash = self.__hashText(key)
file_details = {
'name': name,
'extension': extension,
'size': round((size / 1024) / 1024, 3),
'file_password_hash': file_password_hash,
'key_hash': key_hash
}
plain_text, encrypted_file = self.__encryptFile(file, key.encode('utf-8'))
encrypted_fragment_file = self.__fragmentAndEncrypt(encrypted_file, fragment_size, key.encode('utf-8'))
fragment_hash = self.__hashText(str(encrypted_fragment_file))
file_details['fragment_hash'] = fragment_hash
date = str(datetime.now())
formated_date = f'{date[:4]}.{date[5:7]}.{date[8:10]}'
file_details['date'] = formated_date
file_list.append(file_details)
self.MongoDB.uploadFile(username, file_list, consumed_storage + round((size / 1024) / 1024, 3))
return file_list, consumed_storage + round((size / 1024) / 1024, 3), encrypted_fragment_file
else:
return '1'
else:
return '2'
def openFile(self, file, file_password, credentials, current_file):
if not self.__checkConnection():
pass
# return '0'
username = credentials['username']
password = credentials['password']
file_password_hash = self.__hashText(file_password)
file_hash = self.__hashText(str(file))
if file_password_hash != current_file['file_password_hash']:
return '1'
if file_hash != current_file['fragment_hash']:
return '2'
key = self.__keyDerivationFunction(username, password, file_password_hash)
key_hash = self.__hashText(key)
if key_hash != current_file['key_hash']:
return '1'
decrypted_fragment_file = self.__decryptAndCombineFragment(file, key.encode('utf-8'))
decrypted_file = self.__decryptFile(decrypted_fragment_file, key.encode('utf-8'))
return decrypted_file
def renameFile(self, new_name, credentials, file_list, index):
file_list[index]['name'] = new_name
self.MongoDB.renameFile(credentials, file_list)
return file_list
def deleteFile(self, fragment_file, file_password, credentials, file_list, index, consumed_storage):
username = credentials['username']
password = credentials['password']
file_password_hash = self.__hashText(file_password)
file_hash = self.__hashText(str(fragment_file))
if file_password_hash != file_list[index]['file_password_hash']:
return '1'
if file_hash != file_list[index]['fragment_hash']:
return '2'
key = self.__keyDerivationFunction(username, password, file_password_hash)
key_hash = self.__hashText(key)
if key_hash != file_list[index]['key_hash']:
return '1'
decrypted_fragment = self.__decryptFragment(fragment_file, key.encode('utf-8'))
for fragment in decrypted_fragment:
path = f'Data/{fragment}'
try:
remove(path)
except:
pass
consumed_storage -= file_list[index]['size']
file_list.pop(index)
self.MongoDB.deleteFile(username, file_list, consumed_storage)
return file_list, consumed_storage
def initiate():
interface = DataHandler()
if __name__ == '__main__':
try:
initiate()
except Exception as error:
pass