-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
262 lines (199 loc) · 7.42 KB
/
core.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
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
# Import goes brrr
from time import sleep
from utils.auth import *
from utils.log_neko import message_info
from utils.file_utilities import *
from utils.config_utilities import *
from tools import *
from utils.validation import *
import threading
user_token = ""
user_private_key = ""
config = load_config("config.json")
"""
INIT HELPER METHODS
"""
def loadSigma():
from sigma_ciphers_cryptograms import core as sigma_core
_sigma = sigma_core.Sigma()
message_info("Succesfully loaded sigma")
return _sigma
def load_user_token():
# make sure it's empty
user_token = ""
user_private_key = ""
# make sure it's empty
message_info("Loading token...")
tokenFile = load_config(config["cipher-token-file"])
user_token = tokenFile["token"]
message_info("Loading private key...")
user_private_key_hash = tokenFile["private-key"]
message_info("Validating private key")
user_private_key = validate_private_key(user_token, user_private_key_hash)
if user_private_key:
message_info("Succesfully loaded token and private key")
return user_token
else:
message_warn("Failed to load token and private key")
message_warn("Please restart the application")
sys.exit()
def validate_private_key(token, privateKeyHash):
sigma=loadSigma()
privateKey = sigma.generate_private_key(token)
if validate_string_hash(privateKey, privateKeyHash):
message_info("Private key is valid")
return privateKey
else:
message_warn("Private key is invalid")
return False
def scan_credentials_dir():
message_info("Scanning for a new file in .\\credentials")
newFileList = scan_for_new_file(config["cred-input-dir"])
if newFileList is not None:
message_info(newFileList)
message_info("Encrypting new files")
#Encrypt new file
for file in newFileList:
try:
encrypt_file(file)
except Exception as e:
message_warn(e)
print()
message_info("Succesfully encrypted new file")
clear_dir(load_config("config.json")["cred-input-dir"])
"""
INIT HELPER METHODS
"""
"""
INIT METHODS
"""
def isFirstRun():
return config["isFirstRun"]
def init_first_run_sequence():
message_info("Initializing first run sequence")
message_info("Loading config file")
satisfy_dependencies()
message_info("Initializing empty user-config files")
init_config(filenames = config)
message_info("changed first run status to false")
add_entry_to_config("config.json","isFirstRun", False)
message_info("succesfully runned first run sequence")
def init_config(filenames):
if is_empty(filenames["cipher-token-file"]):
open(filenames["cipher-token-file"], "w").close()
if is_empty(filenames["hashes-file"]):
open(filenames["hashes-file"], "w").close()
if is_empty(filenames["usr-cred-file"]):
open(filenames["usr-cred-file"], "w").close()
def init_user_auth():
message_info("Initializing User Authentication")
tokenFile = load_config(config["cipher-token-file"])
user_token = tokenFile["token"] #get user token
user_cred_config = config["usr-cred-file"]
username = input("Enter your username: ")
password = getpass("Enter your master password: ")
hashedUsrname = hash_string(username + user_token)
hashedPassword = hash_string(password + user_token)
add_entry_to_config(user_cred_config, "username", hashedUsrname)
add_entry_to_config(user_cred_config, "password", hashedPassword)
message_info("succesfully added user credentials")
def init_token():
message_info("Initializing Token Generation")
token = ""
privateKey = ""
message_info("generating user token")
from sigma_ciphers_cryptograms import core
sigma = core.Sigma()
tokenLen = int(input("Enter your token length: "))
token = sigma.generate_token(tokenLen)
privateKey = sigma.generate_private_key(token)
add_entry_to_config(config["cipher-token-file"], "token", token)
add_entry_to_config(config["cipher-token-file"], "private-key", hash_string(privateKey))
message_info("succesfully generated and saved user token")
def init_on_start():
message_info("Initializing")
if(cli_do_auth()):
if os.path.exists(config["backup-path"]):
message_info("checking for backup")
import_backup()
threading.Thread(scan_credentials_dir()).start()
else:
message_warn("User Authentication failed")
message_warn("Exiting")
sys.exit()
def init_core_module():
message_info("Initializing core")
if isFirstRun():
init_first_run_sequence()
sleep(4)
if is_empty(config["hashes-file"]):
init_token()
init_user_auth()
message_info("Initialization completed")
"""
INIT METHODS
"""
"""
METHOD THAT CONNECTS INTO SIGMA MODULE
"""
def encrypt_file(filename):
user_token = load_user_token()
sigma = loadSigma()
message_info("Encrypting file : " , filename)
data = ""
encrypted_data = ""
secured_path = config["cred-output-dir"]
file_path = config["cred-input-dir"] + "\\" + filename
data = readFileContent(file_path)
# encrypted_data = sigma.start_encode(data, user_token, False)
encrypted_data = sigma.start_encode(data, user_token) # default new sigma version
message_info(f"Moving {filename} file to .\\secured-credentials")
encrypted_filename = makeCopyOfFile(filename, encrypted_data, path=secured_path, retrieve_fileName=True)
message_info(f"succesfully moved {filename} files to .\\secured-credentials")
message_info(f"generating hash for {filename}")
file_hash = hash_file(encrypted_filename)
if(file_hash is None):
raise Exception(f"Failed to generate hash for {filename}")
message_info("store file's hash")
hash_config = config["hashes-file"]
add_entry_to_config(hash_config, encrypted_filename, file_hash)
message_info("succesfully encrypted file and stored file's hash")
def read_encrypted_file(filename):
sigma = loadSigma()
message_info("Reading file : " , filename)
user_token = load_user_token()
user_private_key = sigma.generate_private_key(user_token)
data = ""
decrypted_data = ""
secured_path = config["cred-output-dir"]
file_path = secured_path + "\\" + filename
message_info("Retrieving file's hash")
hash_config = config["hashes-file"]
file_hash = load_config(hash_config)[file_path]
message_info("Validating file's hash")
if (validate_file_hash(file_path, file_hash)):
message_info("File's hash is valid")
data = readFileContent(file_path)
decrypted_data = sigma.start_decode(data, user_private_key) #default sigma version
return decrypted_data
else:
message_warn("File's hash is invalid")
"""
METHOD THAT CONNECTS INTO SIGMA MODULE
"""
def start():
"""
Wrapper method for starting the program
"""
init_core_module()
load_user_token()
init_on_start()
if __name__ == '__main__':
print("THIS IS CORE MODULE")
start()
# do_backup()
# import_backup()
#init_user_auth()
#username = str(input("Enter your username: "))
#password = str(input("Enter your master password: "))
#do_login(username, password)