-
Notifications
You must be signed in to change notification settings - Fork 0
/
EOS.py
66 lines (51 loc) · 1.57 KB
/
EOS.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
from Cryptodome.Cipher import AES
import os
import ast
import hashlib
def encrypt(data, privateKey: str):
iv = os.urandom(16)
key = str(privateKey).encode()
a = AES.new(key, AES.MODE_CBC, iv=iv)
e = a.encrypt(str(data).encode()*16)
return iv+e
def decrypt(data, privateKey: str):
iv = data[0:16]
data = data[16::]
a = AES.new(str(privateKey).encode(), AES.MODE_CBC, iv=iv)
try:
d = a.decrypt(data).decode()
return d[0:int(len(d)/16)]
except Exception:
return None
class Eos:
def __init__(self, name: str, privateKey: str):
self.path = name+'.eos'
self.pk = hashlib.sha256(str(privateKey).encode()).hexdigest()[16:32]
if not os.path.exists(self.path):
with open(self.path, 'wb') as f:
f.write(encrypt("{}", self.pk))
def load(self)->dict:
with open(self.path, 'rb') as f:
r = f.read()
try:
r = decrypt(r, self.pk)
if r:
return ast.literal_eval(r)
else:
return None
except Exception:
return None
def dump(self, obj):
c = self.load()
r = dict()
if c:
r.update(c)
r.update(obj)
with open(self.path, 'wb') as f:
f.write(encrypt(str(r), self.pk))
def remove(self, key):
r = self.load()
if key in r:
r.pop(key)
with open(self.path, 'wb') as f:
f.write(encrypt(str(r), self.pk))