-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.py
128 lines (105 loc) · 3.55 KB
/
message.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
import config
import hashlib
import os
import util
import time
class POWerror(Exception):
pass
def mkproof(msgid):
proof = 0
while True:
if hashlib.sha256(
int.to_bytes(proof, 8, "big") +
msgid
).digest().startswith(b"\x00"*config.POW_DIGITS):
break
proof += 1
return proof
class messagestore():
def __getitem__(self, key):
itempath = self.path + util.tohex(key)
with open(itempath, "rb") as f:
data = f.read()
return message.from_serialised(data)
def __setitem__(self, key, value):
if self.path == "---null---/":
return
itempath = self.path + util.tohex(key)
with open(itempath, "wb") as f:
f.write(value.serialise())
def keys(self):
if self.path == "---null---/":
return []
paths = list(os.listdir(self.path))
bytepaths = []
for item in paths:
try:
bytepaths.append(util.fromhex(item, 32))
except ValueError:
pass
# Likely not a real key, ignore it.
return bytepaths
def ignorekey(self, key):
with open("ignored-keys", "ab") as f:
f.write(key)
fname = util.tohex(key)
if config.PRUNE_DELETE:
os.remove(self.path + fname)
else:
os.rename(self.path + fname, self.path + "archive/" + fname)
def prune(self):
for msgid in self.keys():
ctime = os.stat(self.path + util.tohex(msgid)).st_ctime
diff = time.time() - ctime
if diff > config.PRUNE_TIME:
self.ignorekey(msgid)
print("{} is too old, ignoring".format(util.tohex(msgid)))
def __init__(self, path):
if not os.path.exists(path) and not path == "---null---":
os.mkdir(path)
self.path = path
if not(self.path.endswith("/")):
self.path = self.path + "/"
self.ignored = set()
with open("ignored-keys", "rb") as f:
while True:
msgid = f.read(32)
if not msgid:
break
self.ignored.add(msgid)
class message:
def from_serialised(sbytes):
msgid = sbytes[:32]
proof = sbytes[32:40]
gpg = sbytes[40:]
return message(gpg, int.from_bytes(proof, "big"), msgid)
def serialise(self):
x = bytes()
x += self.msgid
x += int.to_bytes(self.proof, 8, "big")
x += self.gpg
return x
def __init__(self, gpg, proof=-1, msgid=""):
if msgid == "":
self.msgid = hashlib.sha256(gpg).digest()
else:
self.msgid = msgid
expectedmsgid = hashlib.sha256(gpg).digest()
if not(self.msgid == expectedmsgid):
print("Given MSGID: {}".format(self.msgid))
print("Expected MSGID: {}".format(expectedmsgid))
raise ValueError("Message ID does not match expected ID")
if proof == -1:
self.proof = mkproof(self.msgid)
else:
self.proof = proof
self.gpg = gpg
proofhash = hashlib.sha256(
int.to_bytes(self.proof, 8, "big") +
self.msgid).digest()
if not proofhash.startswith(b"\x00"*config.POW_DIGITS):
raise POWerror("Invalid proof when creating message")
def __repr__(self):
return "message {}:{}".format(
hex(self.proof),
hex(int.from_bytes(self.msgid, "big")))