1

So I am trying to implement the MD5 algorithm in Python. I've read reams of sources, the original document from ietf by Rivest, and I still have some things I don't get:

So this is the code:

import math
T=[]
for i in range(64):
    el=hex(int(math.floor(pow(2,32)*math.fabs(math.sin(i+1)))))
    el=el.rstrip('L')
    T.append(el)

#print T
S=[7,12,17,22,  7,12,17,22,  7,12,17,22,  7,12,17,22,
   5,9,14,20,   5,9,14,20,   5,9,14,20,   5,9,14,20,
   4,11,16,23,  4,11,16,23,  4,11,16,23,  4,11,16,23,
   6,10,15,21,  6,10,15,21,  6,10,15,21,  6,10,15,21]


A=0x67452301
B=0xefcdab89
C=0x98badcfe
D=0x10325476
msg="This is a message"
#M - working string
M=[]
for letter in msg:
    M.append('{0:08b}'.format(ord(letter)))

AA,BB,CC,DD=A,B,C,D
for i in range(64):
    if i<15:
        F=(B and C) or((not B) and D)
        g=i
    elif i<31:
        F=(D and B) or ((not D) and C)
        g=(5*i+1)%16
    elif i<47:
        F= B and not C or D
    else:
        F=not C or(B or not D)
        g=(7*i)%16
    inter=hex(int(hex(A),16)+int(S[i]))
    inter=int(inter.rstrip('L'),16)
    F=hex((F+inter+int(M[g],2)%pow(2,32)))
    A=D
    F=F.rstrip('L')
    #print F, 'F1'
    F=int(F,16)<<S[i]
    #print hex(F),'F2'
    B=(B+F)%pow(2,32)
    C=B
    D=A
    print 'AA',hex(AA),' ','BB',hex(BB),' ','CC',hex(CC),' ','DD',hex(DD)
    AA+=A
    BB+=B
    CC+=C
    DD+=D
print len(str(hex(AA)))+len(str(hex(BB)))+len(str(CC))+len(str(DD))
print '  ',str(hex(AA))[2:11],str(hex(BB))[2:12],str(hex(CC))[2:12]
FINAL= str(hex(AA)+hex(BB)+hex(CC)+hex(DD))
FINAL=FINAL.replace("0x","")
FINAL=FINAL.replace("L","")

The thing is that the length of AA,BB,CC,DD differ at the end (9,10 characters) in hex, which is not right. What I assume is that whenever the shifting in the for loop, the number increases significantly.
The question is Where does the program fails to do what i intended? What did I do wrong?
Any help would be highly appreciated.

  • Why can't you use MD5 from `hashlib`? – Seer.The Apr 23 '18 at 14:07
  • And what about the built-in [`hash`](https://docs.python.org/2/library/functions.html#hash) function? Or is this exercise for science and learning? – Sunny Patel Apr 23 '18 at 14:10
  • exactly. It's an exercise for me to understand it. Starting with MD5 and going through SHA and I'll see what's next. –  Apr 23 '18 at 14:13
  • I do know about the hashlib, but this is an exercise for me to understand the algorithm and to make it work –  Apr 23 '18 at 14:13
  • Why are you not setting `g` when `elif i<47:` is it intentional? – gipsy Apr 23 '18 at 14:15
  • It's not. there should be a (3*i+5)%16. My mistake –  Apr 23 '18 at 14:24

0 Answers0