-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
144 lines (119 loc) · 4 KB
/
database.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
# This file contains functions that read and write to the admin database as well as
# each user's individual database
import getpass
import csv
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import os
import base64
import ast
# This function reads the encrypted info in the connect admin database file
# It then decrypts using the admin key and returns the list of user accounts, their
# hashed device serial number, and their hashed password
def readAdminDB(filename, key):
f = Fernet(key)
newUsernames = []
newSNs = []
newPasswords = []
inputData = ''
with open(filename, "r") as inFile:
inputData = inFile.read()
if inputData == "":
print("Initializing New Database")
output = []
output.append(newUsernames)
output.append(newSNs)
output.append(newPasswords)
return output
encryptedList = ast.literal_eval(inputData)
decryptedLines = []
for line in encryptedList:
decryptedLines.append(f.decrypt(bytes(line, "utf-8")).decode("utf-8"))
for i in range(len(decryptedLines)):
decryptedLine = ast.literal_eval(decryptedLines[i])
newUsernames.append(decryptedLine[0])
newSNs.append(decryptedLine[1])
newPasswords.append(decryptedLine[2])
output = []
output.append(newUsernames)
output.append(newSNs)
output.append(newPasswords)
return output
# This function updates the admin database after a new user is added.
# It writes their username, hashed device serial number, and hashed password
def updateAdminDB(filename, key, usernames, SNs, passwords):
f = Fernet(key)
assert(len(usernames) == len(SNs) == len(passwords)), "Lenghts of usernames, SNs, and passwords don't match"
newLines = []
encryptedLines = []
for i in range(len(usernames)):
line = []
line.append(usernames[i])
line.append(SNs[i])
line.append(passwords[i])
encryptedLines.append(f.encrypt(bytes(str(line), "utf-8")).decode("utf-8"))
with open(filename, 'w+') as outFile:
outFile.write(str(encryptedLines))
# This function reads an individual user's list of tracked files and their associated keys
def readFileDB(fileFile, keyFile, key):
files = []
keys = []
f = Fernet(key)
try:
with open(keyFile, 'r') as keyFile:
inputData = keyFile.read()
except:
file = open(keyFile, 'w+')
return files, keys
if((len(inputData) == 0 ) or (inputData == '[]')):
print("User file is empty. Must be a new user with no tracked files")
return files, keys
inputData = inputData[:len(inputData)-1]
rawData = inputData.split(',')
decrypted = []
for line in rawData:
realLine = line[3:len(line)-1]
try:
decryptedLine = f.decrypt(bytes(realLine, "utf-8"))
except Exception as e:
print("Couldn't decrypt file in readFileDB ", e)
return files, keys
decrypted.append(decryptedLine)
for i in range(len(decrypted)):
keys.append(decrypted[i])
with open(fileFile, 'r') as fileFile:
line = fileFile.readline().replace("\n", "")
while line:
files.append(line)
line = fileFile.readline().replace("\n", "")
return files, keys
# This function updates a specific user's list of tracked files if they choose to
# add or remove a file. It will also update the encryption key every time the file is re-encrypted
def updateFileDB(files, keys, user, key):
f = Fernet(key)
if(len(keys) is not len(files)):
print("Error! Number of keys didn't match number of files")
return
newData = []
for i in range(len(files)):
print("Appending key " + str(keys[i]))
newData.append(keys[i])
outputData = []
for i in range(len(newData)):
print(type(newData[i]))
if "bytes" in str(type(newData[i])):
newData[i] = newData[i].decode("utf-8")
try:
encrypted = f.encrypt(bytes(newData[i], "utf-8"))
except:
print("Couldn't re-encrypt. Bad key")
return
outputData.append(encrypted)
with open(user+"keys.txt.enc", "w+") as o:
o.write(str(outputData))
with open(user+"files.txt", "w+") as o:
for i in files:
o.write(i)
o.write("\n")