-
Notifications
You must be signed in to change notification settings - Fork 0
/
edc
110 lines (96 loc) · 3.8 KB
/
edc
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
#!/data/data/com.termux/files/usr/bin/python3
import datetime, fernet, getpass, hashlib, os, sys
class EDC():
def __init__(self, keystr: str, file: str) -> None:
key = fernet.base64.urlsafe_b64encode(hashlib.sha256(keystr.encode()).digest())
self.fernet = fernet.Fernet(key)
self.hash = hashlib.sha256(hashlib.sha256(keystr.encode()).hexdigest().encode()).hexdigest()
self.file = file
self.encryptData = lambda data: self.fernet.encrypt(data)
self.decryptData = lambda data: self.fernet.decrypt(data)
def encrypt(self) -> None:
with open(self.file, 'r') as file:
lines = file.readlines()
if len(lines) != 0:
if lines[0][0] == ':':
raise Exception("EncryptError: {} is already encrypted ".format(self.file))
with open(self.file, 'rb') as file:
data = file.read()
data = self.encryptData(data)
with open(self.file, 'wb') as file:
file.write(data)
with open(self.file, 'r') as file:
lines = file.readlines()
lines = [':' + self.hash + '\n'] + lines
with open(self.file, 'w') as file:
file.writelines(lines)
def decrypt(self) -> None:
with open(self.file, 'r') as file:
lines = file.readlines()
if lines[0] != ':' + self.hash + '\n':
print(lines[0])
raise Exception("KeyError: Invalid key")
dt = str(datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S"))
with open(self.file, 'rb') as outfile:
with open('/data/data/com.termux/files/home/.config/edm/cache/'+dt, 'wb') as infile:
infile.write(outfile.read())
lines = lines[1:]
with open(self.file, 'w') as file:
file.writelines(lines)
with open(self.file, 'rb') as file:
data = file.read()
try:
data = self.decryptData(data)
except fernet.InvalidToken:
with open(self.file, 'r') as file:
lines = file.readlines()
lines = [':' + self.hash + '\n' ] + lines
with open(self.file, 'w') as file:
file.writelines(lines)
raise KeyboardInterrupt
with open(self.file, 'wb') as file:
file.write(data)
def editor(self, estr = "vi {} {}") -> None:
self.decrypt()
os.system(estr.format('-n', self.file))
self.encrypt()
def new(self) -> None:
open(self.file, 'a')
self.encrypt()
def main(args):
file = args[0]
try:
open(file)
except FileNotFoundError:
if 'y' == input("Do you want to create file: {} (y/n)".format(file)).lower():
key1 = getpass.getpass("Key : ")
key2 = getpass.getpass("Re Enter Key: ")
if key1 == key2:
edc = EDC(key1, file)
edc.new()
edc.editor()
sys.exit()
if '-a' in args:
try:
action = args[args.index('-a') +1]
if action == 'e':
key1 = getpass.getpass("Key : ")
key2 = getpass.getpass("Re Enter Key: ")
if key1 == key2:
edc = EDC(key1, file)
edc.encrypt()
elif action == 'd':
key = getpass.getpass("Key: ")
edc = EDC(key, file)
edc.decrypt()
elif action.lower() == 'o':
key = getpass.getpass("Key: ")
edc = EDC(key, file)
edc.editor()
except IndexError:
raise Exception("InvalidOptionError")
if __name__ == '__main__':
try:
main(sys.argv[1:])
except IndexError:
raise Exception("FileNotMentionedError")