Guys i made the md5 function from scratch
I know that there is a library called hashlib but i challenged myself to do it
The script works without problem and i got as an output a hex string 32 in length but it's not the md5
the result that i got with this script is not what i got with hashlib or md5 encryption website
So please need you help to fix this problem
Sorry about my english
anyway that's the code :
import math
def txtbinary(str):
binary_list = []
for i in str:
i = list(bin(ord(i)))
if len(i) > 8:
i.pop(1)
else:
i[1]='0'
binary_list.append(''.join(i))
return ''.join(binary_list)
def decbinary(num):
num = bin(num)
num = list(num)
num[1] = '0'
return ''.join(num)
def append_0_1(x):
x = txtbinary(x)
x = list(x)
x.append('1')
while len(x)%512 != 448:
x.append('0')
return ''.join(x)
def appendlen(q):
length = len(txtbinary(q))
len_p = [decbinary(length)]
for i in range(64-len(len_p[0])):
len_p.insert(0,'0')
q = append_0_1(q) + ''.join(len_p)
return q
def toblocks(b):
b = appendlen(b)
blcks = []
j = 0
i = 0
while i < 16:
blcks.append(b[j:j+32])
j+=32
i+=1
return blcks
def rhex(x):
t = hex(x)
return t[2::]
#MD5 Functions and Constants
def F(x,y,z):
return (x & y) | ((~x) & z)
def G(x,y,z):
return (x & z) | (y & (~z))
def H(x,y,z):
return x ^ y ^ z
def I(x,y,z):
return y ^ (x | (~z))
def rotate_left(x,y):
return (x << y) | (x >> (32 - y))
def RoundFun(function,a,b,c,d,x,t,s):
r = a + function(b,c,d) + int(x,2) + s
r = r & 0xffffffff
r = rotate_left(r, t)
r = r & 0xffffffff
r = r + b
return r & 0xffffffff
gn_cts = []
for i in range(64):
t =math.floor((2**32) * abs(math.sin(i + 1)))
gn_cts.append(t)
def md5hash(txt):
txt = toblocks(txt)
AA = A = 1732584193
BB = B = -271733879
CC = C = -1732584194
DD = D = 271733878
Cts1 = [7, 12, 17, 22]
Cts2 = [5, 9, 14, 20]
Cts3 = [4, 11, 16, 23]
Cts4 = [6, 10, 15, 21]
#round 1
A = RoundFun(F,A,B,C,D,txt[0],Cts1[0],gn_cts[0])
D = RoundFun(F,D,A,B,C,txt[1],Cts1[1],gn_cts[1])
C = RoundFun(F,C,D,A,B,txt[2],Cts1[2],gn_cts[2])
B = RoundFun(F,B,C,D,A,txt[3],Cts1[3],gn_cts[3])
A = RoundFun(F,A,B,C,D,txt[4],Cts1[0],gn_cts[4])
D = RoundFun(F,D,A,B,C,txt[5],Cts1[1],gn_cts[5])
C = RoundFun(F,C,D,A,B,txt[6],Cts1[2],gn_cts[6])
C = RoundFun(F,B,C,D,A,txt[7],Cts1[3],gn_cts[7])
A = RoundFun(F,A,B,C,D,txt[8],Cts1[0],gn_cts[8])
D = RoundFun(F,D,A,B,C,txt[9],Cts1[1],gn_cts[9])
C = RoundFun(F,C,D,A,B,txt[10],Cts1[2],gn_cts[10])
B = RoundFun(F,B,C,D,A,txt[11],Cts1[3],gn_cts[11])
A = RoundFun(F,A,B,C,D,txt[12],Cts1[0],gn_cts[12])
D = RoundFun(F,D,A,B,C,txt[13],Cts1[1],gn_cts[13])
C = RoundFun(F,C,D,A,B,txt[14],Cts1[2],gn_cts[14])
B = RoundFun(F,B,C,D,A,txt[15],Cts1[3],gn_cts[15])
#round2
A = RoundFun(G,A,B,C,D,txt[1],Cts2[0],gn_cts[16])
D = RoundFun(G,D,A,B,C,txt[6],Cts2[1],gn_cts[17])
C = RoundFun(G,C,D,A,B,txt[11],Cts2[2],gn_cts[18])
B = RoundFun(G,B,C,D,A,txt[0],Cts2[3],gn_cts[19])
A = RoundFun(G,A,B,C,D,txt[5],Cts2[0],gn_cts[20])
D = RoundFun(G,D,A,B,C,txt[10],Cts2[1],gn_cts[21])
C = RoundFun(G,C,D,A,B,txt[15],Cts2[2],gn_cts[22])
B = RoundFun(G,B,C,D,A,txt[4],Cts2[3],gn_cts[23])
A = RoundFun(G,A,B,C,D,txt[9],Cts2[0],gn_cts[24])
D = RoundFun(G,D,A,B,C,txt[14],Cts2[1],gn_cts[25])
C = RoundFun(G,C,D,A,B,txt[3],Cts2[2],gn_cts[26])
B = RoundFun(G,B,C,D,A,txt[8],Cts2[3],gn_cts[27])
A = RoundFun(G,A,B,C,D,txt[13],Cts2[0],gn_cts[28])
D = RoundFun(G,D,A,B,C,txt[2],Cts2[1],gn_cts[29])
C = RoundFun(G,C,D,A,B,txt[7],Cts2[2],gn_cts[30])
B = RoundFun(G,B,C,D,A,txt[12],Cts2[3],gn_cts[31])
#Round 3
A = RoundFun(H,A,B,C,D,txt[5],Cts3[0],gn_cts[32])
D = RoundFun(H,D,A,B,C,txt[8],Cts3[1],gn_cts[33])
C = RoundFun(H,C,D,A,B,txt[11],Cts3[2],gn_cts[34])
B = RoundFun(H,B,C,D,A,txt[14],Cts3[3],gn_cts[35])
A = RoundFun(H,A,B,C,D,txt[1],Cts3[0],gn_cts[36])
D = RoundFun(H,D,A,B,C,txt[4],Cts3[1],gn_cts[37])
C = RoundFun(H,C,D,A,B,txt[7],Cts3[2],gn_cts[38])
B = RoundFun(H,B,C,D,A,txt[10],Cts3[3],gn_cts[39])
A = RoundFun(H,A,B,C,D,txt[13],Cts3[0],gn_cts[40])
D = RoundFun(H,D,A,B,C,txt[0],Cts3[1],gn_cts[41])
C = RoundFun(H,C,D,A,B,txt[3],Cts3[2],gn_cts[42])
B = RoundFun(H,B,C,D,A,txt[6],Cts3[3],gn_cts[43])
A = RoundFun(H,A,B,C,D,txt[9],Cts3[0],gn_cts[44])
D = RoundFun(H,D,A,B,C,txt[12],Cts3[1],gn_cts[45])
C = RoundFun(H,C,D,A,B,txt[15],Cts3[2],gn_cts[46])
B = RoundFun(H,B,C,D,A,txt[2],Cts3[3],gn_cts[47])
#Round 4
A = RoundFun(I,A,B,C,D,txt[0],Cts4[0],gn_cts[48])
D = RoundFun(I,D,A,B,C,txt[7],Cts4[1],gn_cts[49])
C = RoundFun(I,C,D,A,B,txt[14],Cts4[2],gn_cts[50])
B = RoundFun(I,B,C,D,A,txt[5],Cts4[3],gn_cts[51])
A = RoundFun(I,A,B,C,D,txt[12],Cts4[0],gn_cts[52])
D = RoundFun(I,D,A,B,C,txt[3],Cts4[1],gn_cts[53])
C = RoundFun(I,C,D,A,B,txt[10],Cts4[2],gn_cts[54])
B = RoundFun(I,B,C,D,A,txt[1],Cts4[3],gn_cts[55])
A = RoundFun(I,A,B,C,D,txt[8],Cts4[0],gn_cts[56])
D = RoundFun(I,D,A,B,C,txt[15],Cts4[1],gn_cts[57])
C = RoundFun(I,C,D,A,B,txt[6],Cts4[2],gn_cts[58])
B = RoundFun(I,B,C,D,A,txt[13],Cts4[3],gn_cts[59])
A = RoundFun(I,A,B,C,D,txt[4],Cts4[0],gn_cts[60])
D = RoundFun(I,D,A,B,C,txt[11],Cts4[1],gn_cts[61])
C = RoundFun(I,C,D,A,B,txt[2],Cts4[2],gn_cts[62])
B = RoundFun(I,B,C,D,A,txt[9],Cts4[3],gn_cts[63])
A = (A + AA) & 0xffffffff
B = (B + BB) & 0xffffffff
C = (C + CC) & 0xffffffff
D = (D + DD) & 0xffffffff
return rhex(A) + rhex(B) + rhex(C) + rhex(D)
user_input = input('text : ')
print(md5hash(user_input))
User contributions licensed under CC BY-SA 3.0