-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_lab1.py
144 lines (112 loc) · 3.53 KB
/
_lab1.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
import hashlib
import json
import os
import pyAesCrypt
import glob
from threading import Thread
import time
global stop
stop = False
def set_new_password(password):
hash_object = hashlib.md5(password.encode())
with open('template.tbl', 'r') as file:
old = json.load(file)
with open('template.tbl', 'w') as file:
old["password"] = hash_object.hexdigest()
json.dump(old, file)
def add_new_file(file_name):
with open('template.tbl', 'r') as file:
old = json.load(file)
with open('template.tbl', 'w') as file:
old["files"].append(file_name)
json.dump(old, file)
def del_file(file_name):
with open('template.tbl', 'r') as file:
old = json.load(file)
with open('template.tbl', 'w') as file:
old["files"].remove(file_name)
json.dump(old, file)
def crypt_files(password):
bufferSize = 64 * 1024
with open('template.tbl', 'r') as file:
fi = json.load(file)
if hashlib.md5(password.encode()).hexdigest() == fi['password']:
for i in fi['files']:
for j in glob.glob(i):
pyAesCrypt.encryptFile(j, j + ".enc", password, bufferSize)
os.remove(j)
return fi['files']
def decrypt_files(password):
bufferSize = 64 * 1024
with open('template.tbl', 'r') as file:
fi = json.load(file)
if hashlib.md5(password.encode()).hexdigest() == fi['password']:
for i in fi['files']:
for j in glob.glob(i + '.enc'):
pyAesCrypt.decryptFile(j, '.'.join(j.split('.')[0:-1]), password, bufferSize)
os.remove(j)
def init(password):
if 'template.tbl.enc' in os.listdir():
bufferSize = 64 * 1024
pyAesCrypt.decryptFile('template.tbl.enc', 'template.tbl', password, bufferSize)
def end(password):
bufferSize = 64 * 1024
pyAesCrypt.encryptFile('template.tbl', 'template.tbl.enc', password, bufferSize)
os.remove('template.tbl')
def check_and_delete(files):
for i in files:
for j in glob.glob(i):
if '.enc' not in j:
os.remove(j)
def checker(files):
global stop
while True:
time.sleep(5)
if stop:
return 0
check_and_delete(files)
def set_files_right_no(password):
with open('template.tbl', 'r') as file:
fi = json.load(file)
for i in fi['files']:
for j in glob.glob(i + '.enc'):
os.chmod(j, 0o000)
def set_files_right_yes(password):
with open('template.tbl', 'r') as file:
fi = json.load(file)
for i in fi['files']:
for j in glob.glob(i):
os.chmod(j, 0o777)
for j in glob.glob(i + '.enc'):
os.chmod(j, 0o777)
def start_def(password):
init(password)
files = crypt_files(password)
set_files_right_no(password)
end(password)
return files
def stop_def(password):
init(password)
set_files_right_yes(password)
decrypt_files(password)
# set_files_right_yes(password)
def cli():
global stop
password = input("Password: ")
cmd = input('>')
while cmd != 'exit':
if cmd == 'start':
files = start_def(password)
my_thread = Thread(target=checker, args=(files,))
my_thread.start()
elif cmd == 'stop':
stop_def(password)
stop = True
elif cmd == 'env':
print(os.environ['saidumaroff'])
cmd = input('>')
if __name__ == "__main__":
cli()
else:
set_new_password(os.environ['saidumaroff'])
start_def(os.environ['saidumaroff'])