-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIHA.py
140 lines (124 loc) Β· 3.69 KB
/
IHA.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
"""
||=========================================||
|| ||
|| Iconic Hashing Algorithm ||
|| Author: Deep Dhakate (InTruder) ||
|| Version: 1.0.1 ||
||=========================================||
"""
def encode(string): # Converts each characters to its ascii characters
cipher = ""
for char in string:
ascii = str(ord(char))
cipher += ascii
finalhash = padding(cipher)
return finalhash
def key_gen1(new_cipher): # Generate keys for encrypting
sum = 0
for i in range(len(new_cipher)):
sum += int(new_cipher[i])
key = 0
for char in str(sum):
key += int(char)
return key
def key_gen2(new_cipher):
length = int(len(new_cipher))
if length % 2 == 0:
key2 = new_cipher[int(length/2)]
else:
key2 = new_cipher[(length+1)/2]
return key2
def key_gen3(new_cipher):
length = len(new_cipher)
key3 = new_cipher[0]+new_cipher[length - 1]
return key3
# PADDING
def padding(cipher): # Changes cipher text into padded text
length = len(cipher)
if length < 64:
t = 1
for i in range(length, 64): # Addes base 10 no if length of cipher text is less than 64
if t < 10:
cipher += str(t)
t = t + 1
if t >= 10:
t = 1
if length > 64:
l = 1
for i in range(32):
# If length is greater than 64 it addes base 10 no to nearest 32 divisible no
if len(cipher) % 32 != 0:
if l >= 10:
l = 1
cipher += str(l)
l = l+1
if len(cipher) % 32 == 0:
break
cipher_array = []
for char in cipher:
cipher_array.append(char)
split = []
div = len(cipher)//32
n = 0
for m in range(div):
t = ""
for i in range(n, 32 + n): # Divides cipher text into 32 parts and stores in array
t = t + str(cipher_array[i])
n += 32
split.append(t)
leng = len(split)
temp = split[0]
split[0] = split[leng - 1]
split[leng - 1] = temp
n_cipher = ""
for i in range(len(split)):
n_cipher += split[i]
cipher_array = []
for char in n_cipher:
cipher_array.append(char)
# Generate keys
key = key_gen1(cipher_array)
key2 = key_gen2(cipher_array)
key3 = key_gen3(cipher_array)
finalhash = hash(key, key2, key3, cipher_array)
return finalhash
def format(hash): # Change the hash into length of 38
l = 1
for i in range(38):
if len(hash) % 38 != 0:
if l >= 10:
l = 1
hash += str(l)
l = l+1
if len(hash) % 38 == 0:
break
split = []
a = ""
for i in range(len(hash)):
if (i+1) % 38 == 0:
a = a + hash[i]
split.append(a)
a = ""
else:
a = a + hash[i]
t = ""
for i in range(38):
sum = 0
for m in range(len(split)):
k = split[m][i]
sum = int(sum) + int(k)
no = sum % 10
t = t + str(no)
return t
def hash(key, key2, key3, cipher_array): # Hashes the cipher text with keys
form = ""
hash = ""
for i in range(len(cipher_array)):
temp = (int(cipher_array[i])+int(key)) % 11
form += str(temp)
temp = 0
for i in range(len(form)):
var = (int(form[i])+(int(key3)*int(key2))) % 11
hash += str(var)
finalhash = format(hash)
return finalhash