-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.py
350 lines (281 loc) · 8.41 KB
/
hash.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
Code to compute hash with 256 bits
"""
from hash_constant import *
#==========================================================================================
# Function useful in the translation
#==========================================================================================
def translate(msg):
'''
Function to translate message in bits
Parameters
----------
msg : string
message to translate
Returns
-------
bits : list
list of bit rappresentation of msg
'''
# Trasform in Unicode values the in binary rappresentation
char_codes = [ord(m) for m in msg]
bytes = []
for c in char_codes:
bytes.append(bin(c)[2:].zfill(8))
# From a list of number to a list with splitted values
# e.g. [11, 01] -> [1, 1, 0, 1]
bits = []
for byte in bytes:
for bit in byte:
bits.append(int(bit))
return bits
def init(number):
'''
Function to initialize NIST's constant
Parameters
----------
number : list
list of strings in hexadecimal format
Returns
-------
result : list
list of list, each inner list contains the binary
rappresentation of the element of number
'''
# Convert values in base 16 and then in binary
# [2:] is to delete the '0b' part
binaries = [bin(int(v, 16))[2:] for v in number]
result = []
# Split all number in a list and pad with zeros
# to obtain the same length for all numbers (32 bits)
for binary in binaries:
w = []
for b in binary:
w.append(int(b))
result.append(pad_zero(w, 32, 'start'))
return result
def pad_zero(bits, length, loc='end'):
'''
Function to add zero to obtain binary number with equal size
Parameters
----------
bits : list
...
length : int
length of the returned list
loc : string, optional, default 'end'
location where append the zeros
Returns
-------
bits : list
padded list
'''
l = len(bits)
# Pad at the end of number
if loc == 'end':
for i in range(l, length):
bits.append(0)
# Pad at the start of number (simeq .zfill)
if loc == 'start':
while l < length:
bits.insert(0, 0)
l = len(bits)
return bits
def chunker(bits, l):
'''
Function to divides list of bits into byte chunks,
Parameters
----------
bits : list
list of bits
l : int
size of the chunk
Returns
-------
chunck : list
list of list, each inner list is a part of bits of length l
'''
chunk = []
for b in range(0, len(bits), l):
chunk.append(bits[b : b + l])
return chunk
def pre_process(msg):
'''
Function to pre process message to compute the hash
Parameters
----------
msg : string
message to translate
Returns
-------
bits : list
pre processed list of bit rappresentation of msg
'''
# translate message into bits
bits = translate(msg)
L = len(bits)
L_bit = [int(b) for b in bin(L)[2:].zfill(64)] # L in bit
# if L < 448 put 1 at the end
# Padd
# Add the 64 bits representing the length of the message
if L < 448:
bits.append(1)
bits = pad_zero(bits, 448, 'end')
bits = bits + L_bit
return [bits]
# If 448 <= L <= 512 bits, put 1 at end, we go to the next multiple of 512 (i.e. 1024)
# Replace the last 64 bits of the multiple of 512 with the original message length
# Divide into two chunks of 512 bits
elif 448 <= L <= 512:
bits.append(1)
bits = pad_zero(bits, 1024, 'end')
bits[-64:] = L_bit
return chunker(bits, 512)
# Otherwise, always put 1 at end
# Loop until multiple of 512 + 64 bit of L_bit
# Add the 64 bits representing the length of the message
# Divide into two chunks of 512 bits
else:
bits.append(1)
while (len(bits)+64) % 512 != 0:
bits.append(0)
bits = bits + L_bit
return chunker(bits, 512)
def b2_to_b16(number):
'''
Function to covert from base=2 to base=16
Parameters
----------
number : list
number in binary in a list (e.g. 2 = [1, 0])
Returns
-------
h : string
number in hexadecimal rappresentation
(e.g. 15 = [1, 1, 1, 1] -> 'f')
'''
#convert to string
value = ''.join([str(x) for x in number])
#creat 4 bit chunks, and add bin-indicator
binaries = []
for d in range(0, len(value), 4):
binaries.append('0b' + value[d:d+4])
#transform to hexadecimal and remove hex-indicator
h = ''
for b in binaries:
h += hex(int(b ,2))[2:]
return h
#==========================================================================================
# Binary functions
#==========================================================================================
def If(i, y, z):
return y if i == 1 else z
def And(i, j):
return If(i, j, 0)
def AND(i, j):
return [And(ii, jj) for ii, jj in zip(i, j)]
def Not(i):
return If(i, 0, 1)
def NOT(i):
return [Not(x) for x in i]
def xor(i, j):
return If(i, Not(j), j)
def XOR(i, j):
return [xor(ii, jj) for ii, jj in zip(i, j)]
def xorxor(i, j, l):
return xor(i, xor(j, l))
def XORXOR(i, j, l):
return [xorxor(ii, jj, ll) for ii, jj, ll, in zip(i, j, l)]
def maj(i,j,k):
''' Get the majority of results, i.e., if 2 or more of three values are the same
'''
return max([i,j,], key=[i,j,k].count)
def rotr(x, n):
''' rotate right
Example
-------
>>> rotr(10011, 2)
>>> 11100
'''
return x[-n:] + x[:-n]
def shr(x, n):
''' shift right
Example
-------
>>> rotr(10011, 2)
>>> 00100
'''
return n * [0] + x[:-n]
def add(i, j):
'''
Function to implement full binary adder
(actually the carry over bit got neglected)
'''
L = len(i)
sums = list(range(L))
# Initial input needs an carry over bit as 0
c = 0
for x in range(L-1,-1,-1):
# Add the inout bits with a double xor gate
sums[x] = xorxor(i[x], j[x], c)
# Carry over bit is equal the most represented, e.g., output = 0,1,0
# then 0 is the carry over bit
c = maj(i[x], j[x], c)
# Returns list of bits
return sums
#==========================================================================================
# SHA function
#==========================================================================================
def sha_256(msg):
'''
Function to compute the fingerprint hash256
Parameters
----------
msg : string
message to translate
Returns
-------
result : string
string in hexadecimal format with the encrypted message
'''
# Initializzetion of constants
k = init(R)
h0, h1, h2, h3, h4, h5, h6, h7 = init(init_h)
# Pre process to prepare/divide the message
chunks = pre_process(msg)
for chunk in chunks: # mail loop
# Create lists of 32 bit words (512 bits / 32 = 16 words)
w = chunker(chunk, 32)
# Extend length of chunk to 64 words, the remaining 48
for _ in range(48): # initialized with zeros
w.append(32 * [0])
for i in range(16, 64):
s0 = XORXOR(rotr(w[i-15], 7), rotr(w[i-15], 18), shr(w[i-15], 3))
s1 = XORXOR(rotr(w[i-2], 17), rotr(w[i-2] , 19), shr(w[i-2], 10))
w[i] = add(add(add(w[i-16], s0), w[i-7]), s1)
# Variables initializzation for the loop
a, b, c, d, e, f, g, h = h0, h1, h2, h3, h4, h5, h6, h7
# And now some magic
for j in range(64):
S1 = XORXOR(rotr(e, 6), rotr(e, 11), rotr(e, 25) )
ch = XOR(AND(e, f), AND(NOT(e), g))
temp1 = add(add(add(add(h, S1), ch), k[j]), w[j])
S0 = XORXOR(rotr(a, 2), rotr(a, 13), rotr(a, 22))
m = XORXOR(AND(a, b), AND(a, c), AND(b, c))
temp2 = add(S0, m)
h = g; g = f; f = e
e = add(d, temp1)
d = c; c = b; b = a
a = add(temp1, temp2)
h0 = add(h0, a)
h1 = add(h1, b)
h2 = add(h2, c)
h3 = add(h3, d)
h4 = add(h4, e)
h5 = add(h5, f)
h6 = add(h6, g)
h7 = add(h7, h)
result = ''
for val in [h0, h1, h2, h3, h4, h5, h6, h7]:
result += b2_to_b16(val)
return result