-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.py
More file actions
189 lines (143 loc) · 5.76 KB
/
App.py
File metadata and controls
189 lines (143 loc) · 5.76 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
import sys
from cryptography.fernet import Fernet
import os
from functions.encryption_function import FilebasicEncrypt
from functions.decryptfile_function import Filebasicdecrypt
from functions.command_available import List_commands
from functions.checkprogramfiles import Check_program_file
class Loadkey:
def returnkey(self):
try:
with open("locals.env" , "r") as file:
key = file.readlines()
if not key:
sys.stderr.write("No key found, please generate a key!")
sys.exit(1)
else:
return key[0]
except Exception :
sys.stderr.write("No key found, please generate a key!")
sys.exit(1)
class BulkEncrypt(Loadkey):
def bulk_encrypt(self, filepath):
try:
if os.path.isdir(filepath):
key = super().returnkey()
fernet = Fernet(key)
if not os.path.isdir(filepath):
sys.stdin.write('Provided path should be a directory')
sys.exit(1)
for root , dir , files in os.walk(filepath):
for file in files:
path = os.path.join(root, file)
if os.path.isfile(path):
FilebasicEncrypt(path , fernet)
else:
sys.stdout.write("Provided path should be a folder")
sys.exit(1)
except Exception:
sys.exit(1)
class Bulkdecrypt(Loadkey):
def bulk_decrypt(self, filepath):
try:
if os.path.isdir(filepath):
key = super().returnkey()
fernet = Fernet(key)
if not os.path.isdir(filepath):
sys.stdin.write('Provided path should be a directory')
sys.exit(1)
for root , dir, files in os.walk(filepath):
for file in files:
full_path = os.path.join(root , file)
if os.path.isfile(full_path):
Filebasicdecrypt(full_path , fernet)
else:
sys.stdout.write("Provided path should be a folder")
sys.exit(1)
except Exception:
sys.exit(1)
class EncryptFile(Loadkey):
def encrypt(self , filename):
key = super().returnkey()
fernet = Fernet(key)
if os.path.exists(filename):
validate_File = filename.split(".")
if "txt" in validate_File:
FilebasicEncrypt(filename , fernet)
else:
filename = f"{filename}.txt"
FilebasicEncrypt(filename , fernet)
else:
sys.stderr.write("No such file found")
class DecryptFile(Loadkey):
def decrypt(self , filename):
key = super().returnkey()
fernet = Fernet(key)
if os.path.exists(filename):
validate_File = filename.split(".")
if "txt" in validate_File:
Filebasicdecrypt(filename , fernet)
else:
filename = f"{filename}.txt"
Filebasicdecrypt(filename , fernet)
else:
sys.stderr.write("No such file found")
class GenerateKey:
def generate_key(self , filepath):
if not os.path.exists(filepath):
key = Fernet.generate_key()
with open(filepath, "wb") as file:
file.write(key)
sys.stdout.write("key generated!")
else:
with open(filepath, "r") as file:
lines = file.readlines()
if len(lines) > 0:
sys.stdout.write("Key already exists!")
else:
key = Fernet.generate_key()
with open(filepath, "wb") as file:
file.write(key)
sys.stdout.write("key generated!")
class User_commands(GenerateKey , EncryptFile , DecryptFile , BulkEncrypt , Bulkdecrypt):
def __init__(self, command , filepath ):
self.filepath = filepath
self.command = command
if self.command == "encrypt":
super().encrypt(filepath)
elif self.command == "decrypt":
super().decrypt(filepath)
elif self.command == "bulkencrypt":
super().bulk_encrypt(filepath)
elif self.command == "generatekey":
super().generate_key(filepath)
elif self.command == "bulkdecrypt":
super().bulk_decrypt(filepath)
else:
sys.stderr.write(f"Unknown command {command}")
if __name__ == "__main__":
try:
if len(sys.argv) < 2:
List_commands()
sys.exit(1)
command = sys.argv[1]
filepath = sys.argv[2] if len(sys.argv) > 2 else None
valid_commands = ["generatekey", "encrypt", "decrypt", "bulkencrypt", "bulkdecrypt"]
if command not in valid_commands:
print("unknown command")
List_commands()
sys.exit(1)
if command in ["encrypt", "decrypt", "bulkencrypt", "bulkdecrypt"] and not filepath:
print(f"Command '{command}' requires a filepath.")
List_commands()
sys.exit(1)
if command == "generatekey":
ucommands = User_commands(command, filepath='locals.env')
sys.exit(0)
if Check_program_file(filepath):
print("Can't perform operations on program files")
sys.exit(1)
ucommands = User_commands( command, filepath)
except Exception as e:
print(e)
List_commands()