Repository navigation
Expand file tree
/
Copy pathORE.py
More file actions
47 lines (39 loc) · 1.31 KB
/
Copy pathORE.py
File metadata and controls
47 lines (39 loc) · 1.31 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
import hashlib
class ORE:
def __init__(self, key, size = 16, LEN = 16):
self.key = key
self.LEN = LEN
self.size = size
def __prf(self, msg):
pad = "0" * (self.LEN - len(msg))
return int(hashlib.sha256((str(msg) + pad + str(self.key)).encode()).hexdigest(), 16)
def encode(self, data):
m = bin(data)[2:]
m = "0"*(self.size - len(m)) + m
tmp_m = ""
tmpres = ""
for i in m:
tmp_m += i
tmpres += str((self.__prf(tmp_m[:-1]) + int(tmp_m[-1])) % 3)
return tmpres
def compare(self, u, v):
if u == v:
return 0
L = len(u)
cnt = 0
while u[cnt] == v[cnt]:
cnt += 1
if (int(u[cnt]) + 1) % 3 == int(v[cnt]):
return -1
else:
return 1
def decode(self, c):
tmp_m = ""
for i in range(len(c)):
for j in [0, 1]:
if (self.__prf(tmp_m) + j) % 3 == int(c[i]):
tmp_m += str(j)
break
return int(tmp_m, 2)
# Inspired by https://github.com/kpatsakis/OrderRevealingEncryption
# and https://icerm.brown.edu/materials/Slides/tw19-1-es/Order-Revealing_Encryption_-_Definitions,_Constructions,_and_Challenges_]_David_Wu,_University_of_Virginia.pdf