-
Notifications
You must be signed in to change notification settings - Fork 0
/
Testudo.py
61 lines (48 loc) · 1.69 KB
/
Testudo.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
# testudo
# https://github.com/hameddanesh/testudo
#
# Copyright 2024, Hamed Danesh
#
# Licensed under the MIT license:
# https://opensource.org/license/mit/
class Testudo:
def form(self, value, key):
seed = self.getSeed(key, value)
return self.xor(self.textToBinary(value), seed)
def unform(self, secret, seed):
return self.binaryToText(self.xor(secret, seed))
def textToBinary(self, text):
a_bytes = bytes(text, "ascii")
return " ".join(["{0:b}".format(x).rjust(7, "0") for x in a_bytes])
def binaryToText(self, _binaryString):
binaryString = _binaryString.replace(" ", "")
strData = " "
for i in range(0, len(binaryString), 7):
tempData = binaryString[i : i + 7]
decimalData = string = int(tempData, 2)
strData = strData + chr(decimalData)
return strData
def getSeed(self, hashBinary, valueBinary):
rep = (int(int(valueBinary.__len__()) / hashBinary.__len__())) + 1
temp = ""
for i in range(0, rep):
temp += hashBinary
hashBinary = temp
return self.textToBinary(hashBinary)[
0 : (self.textToBinary(valueBinary).__len__() + 1)
]
def xor(self, _binaryString, _seed):
xString = ""
binaryString = _binaryString.replace(" ", "")
seed = _seed.replace(" ", "")
counter = 0
for i in range(0, binaryString.__len__()):
counter += 1
if binaryString[i] != seed[i]:
xString += "1"
else:
xString += "0"
if counter >= 7:
xString += " "
counter = 0
return xString