forked from Hritish42/TSHA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT-SHA.py
153 lines (130 loc) · 4.72 KB
/
T-SHA.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
145
146
147
148
149
150
151
152
153
try:
def TSHA(TEXT):
def sha1(data):
bytes = ""
h0 = 0x67452301
h1 = 0xEFCDAB89
h2 = 0x98BADCFE
h3 = 0x10325476
h4 = 0xC3D2E1F0
for n in range(len(data)):
bytes += '{0:08b}'.format(ord(data[n]))
bits = bytes + "1"
pBits = bits
while len(pBits) % 512 != 448:
pBits += "0"
pBits += '{0:064b}'.format(len(bits) - 1)
def chunks(l, n):
return [l[i:i + n] for i in range(0, len(l), n)]
def rol(n, b):
return ((n << b) | (n >> (32 - b))) & 0xffffffff
for c in chunks(pBits, 512):
words = chunks(c, 32)
w = [0] * 80
for n in range(0, 16):
w[n] = int(words[n], 2)
for i in range(16, 80):
w[i] = rol((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1)
a = h0
b = h1
c = h2
d = h3
e = h4
for i in range(0, 80):
if 0 <= i <= 19:
f = (b & c) | ((~b) & d)
k = 0x5A827999
elif 20 <= i <= 39:
f = b ^ c ^ d
k = 0x6ED9EBA1
elif 40 <= i <= 59:
f = (b & c) | (b & d) | (c & d)
k = 0x8F1BBCDC
elif 60 <= i <= 79:
f = b ^ c ^ d
k = 0xCA62C1D6
temp = rol(a, 5) + f + e + k + w[i] & 0xffffffff
e = d
d = c
c = rol(b, 30)
b = a
a = temp
h0 = h0 + a & 0xffffffff
h1 = h1 + b & 0xffffffff
h2 = h2 + c & 0xffffffff
h3 = h3 + d & 0xffffffff
h4 = h4 + e & 0xffffffff
return '%08x%08x%08x%08x%08x' % (h0, h1, h2, h3, h4)
def AND(r1, x1):
result = ''
for i in range(len(r1)):
if r1[i] == '1' and x1[i] == '1':
result = result + '1'
if r1[i] == '0' and x1[i] == '1':
result = result + '0'
if r1[i] == '1' and x1[i] == '0':
result = result + '0'
if r1[i] == '0' and x1[i] == '0':
result = result + '0'
return result
def hexadecimalTObinary(n):
res = "{0:08b}".format(int(n, 16))
return res.zfill(160)
def binaryToDecimal(n):
return int(n, 2)
def decimal_to_binary(value):
k = ''.join(format(ord(i), 'b') for i in value)
return k
def XOR(r1, x1):
result = ''
for i in range(len(r1) - 1):
if r1[i] == '1' and x1[i] == '1':
result = result + '0'
if r1[i] == '0' and x1[i] == '1':
result = result + '1'
if r1[i] == '1' and x1[i] == '0':
result = result + '1'
if r1[i] == '0' and x1[i] == '0':
result = result + '0'
return result
def cirularshift(hash, r_no):
y = hash[0:r_no]
x = hash[r_no + 1:]
x = x + y
return x
def f1(hash):
x = sha1(sha1(hash))
y = cirularshift(x, 5)
return hexadecimalTObinary(y)
def f2(hash):
x = sha1(sha1(hash))
y = cirularshift(x, 9)
y2 = sha1(sha1(sha1(y)))
return hexadecimalTObinary(y2)
def round(message):
full = hexadecimalTObinary(sha1(message))
l = full[:80]
r1 = f2(l)
a1 = r1[0:80]
x1 = r1[80:]
r = full[80:]
l1 = f1(r1)
a2 = l1[80:]
x2 = l1[:80]
k1 = AND(a1, a2)
k2 = XOR(x1, x2)
return k1 + k2
def finalHash(TEXT):
gen_hashes = []
gen_hashes.append(round(TEXT))
i = 0
while i <= 160:
j = round(gen_hashes[-1])
gen_hashes.append(j)
i = i + 1
return gen_hashes[-1]
return sha1(finalHash(TEXT))
print("[+] Generated Hash: ",TSHA(input("[+] Text: ")))
except(Exception):
print("\n\n[-] Invaild Input\n")
exit("[-]Exiting Program")