-
Notifications
You must be signed in to change notification settings - Fork 1
/
tiger.py
144 lines (125 loc) · 3.67 KB
/
tiger.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import array
import struct
from sboxes import t1, t2, t3, t4
def tiger_round(a, b, c, x, mul):
c ^= x
c &= 0xffffffffffffffff
a -= t1[(c >> (0 * 8)) & 0xFF] ^ t2[(c >> (2 * 8)) & 0xFF] ^ t3[(c >> (4 * 8)) & 0xFF] ^ t4[
(c >> (6 * 8)) & 0xFF]
b += t4[(c >> (1 * 8)) & 0xFF] ^ t3[(c >> (3 * 8)) & 0xFF] ^ t2[(c >> (5 * 8)) & 0xFF] ^ t1[
(c >> (7 * 8)) & 0xFF]
b *= mul
a &= 0xffffffffffffffff
b &= 0xffffffffffffffff
c &= 0xffffffffffffffff
return {"a": a, "b": b, "c": c}
def tiger_pass(a, b, c, mul, mystr):
values = tiger_round(a, b, c, mystr[0], mul)
values = tiger_round(values["b"], values["c"], values["a"], mystr[1], mul)
values = {"b": values["a"], "c": values["b"], "a": values["c"]}
values = tiger_round(values["c"], values["a"], values["b"], mystr[2], mul)
values = {"c": values["a"], "a": values["b"], "b": values["c"]}
values = tiger_round(values["a"], values["b"], values["c"], mystr[3], mul)
values = tiger_round(values["b"], values["c"], values["a"], mystr[4], mul)
values = {"b": values["a"], "c": values["b"], "a": values["c"]}
values = tiger_round(values["c"], values["a"], values["b"], mystr[5], mul)
values = {"c": values["a"], "a": values["b"], "b": values["c"]}
values = tiger_round(values["a"], values["b"], values["c"], mystr[6], mul)
values = tiger_round(values["b"], values["c"], values["a"], mystr[7], mul)
values = {"b": values["a"], "c": values["b"], "a": values["c"]}
return values
def tiger_compress(message, res):
# setup
a = res[0]
b = res[1]
c = res[2]
x = []
for j in range(0, 8):
if isinstance(message, array.array):
message = message.tostring()
elif isinstance(message, str):
message = message.encode()
tmp = struct.unpack('Q', message[j * 8:j * 8 + 8])[0]
x.append(tmp)
# compress
aa = a
bb = b
cc = c
allf = 0xFFFFFFFFFFFFFFFF
for i in range(0, 3):
if i != 0:
x[0] = (x[0] - (x[7] ^ 0xA5A5A5A5A5A5A5A5) & allf) & allf
x[1] ^= x[0]
x[2] = (x[2] + x[1]) & allf
x[3] = (x[3] - (x[2] ^ (~x[1] & allf) << 19) & allf) & allf
x[4] ^= x[3]
x[5] = (x[5] + x[4]) & allf
x[6] = (x[6] - (x[5] ^ (~x[4] & allf) >> 23) & allf) & allf
x[7] ^= x[6]
x[0] = (x[0] + x[7]) & allf
x[1] = (x[1] - (x[0] ^ (~x[7] & allf) << 19) & allf) & allf
x[2] ^= x[1]
x[3] = (x[3] + x[2]) & allf
x[4] = (x[4] - (x[3] ^ (~x[2] & allf) >> 23) & allf) & allf
x[5] ^= x[4]
x[6] = (x[6] + x[5]) & allf
x[7] = (x[7] - (x[6] ^ 0x0123456789ABCDEF) & allf) & allf
if i == 0:
vals = tiger_pass(a, b, c, 5, x)
a = vals['a']
b = vals['b']
c = vals['c']
elif i == 1:
vals = tiger_pass(a, b, c, 7, x)
a = vals['a']
b = vals['b']
c = vals['c']
else:
vals = tiger_pass(a, b, c, 9, x)
a = vals['a']
b = vals['b']
c = vals['c']
tmpa = a
a = c
c = b
b = tmpa
a ^= aa
b = (b - bb) & allf
c = (c + cc) & allf
# map values out
res[0] = a
res[1] = b
res[2] = c
def hash(message):
i = 0
res = [0x0123456789ABCDEF, 0xFEDCBA9876543210, 0xF096A5B4C3B2E187]
offset = 0
length = len(message)
while i < length - 63:
tiger_compress(message[i:i + 64], res)
i += 64
temp = array.array('b', message[i:].encode())
j = len(temp)
temp.append(0x01)
j += 1
while j & 7 != 0:
temp.append(0)
j += 1
if j > 56:
while j < 64:
temp.append(0)
j += 1
tiger_compress(temp, res)
j = 0
# make the first 56 bytes 0
temp.extend([0 for i in range(0, 56 - j)])
while j < 56:
temp[j] = 0
j += 1
while len(temp) > 56:
temp.pop(56)
temp.fromstring(struct.pack('Q', length << 3))
tiger_compress(temp, res)
return "%016X %016X %016X" % (res[0], res[1], res[2])
if __name__ == '__main__':
print(hash("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-sdvkjsadbaslmd;asds"))