Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#294 from TheAlgorithms/documented_md…
Browse files Browse the repository at this point in the history
…5_hash

Documented md5 hash
  • Loading branch information
Christian Bender authored Apr 16, 2018
2 parents 060988b + 0494d48 commit dbfc220
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions hashes/md5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
import math

def rearrange(bitString32):
"""[summary]
Regroups the given binary string.
Arguments:
bitString32 {[string]} -- [32 bit binary]
Raises:
ValueError -- [if the given string not are 32 bit binary string]
Returns:
[string] -- [32 bit binary string]
"""

if len(bitString32) != 32:
raise ValueError("Need length 32")
newString = ""
Expand All @@ -10,13 +23,30 @@ def rearrange(bitString32):
return newString

def reformatHex(i):
"""[summary]
Converts the given integer into 8-digit hex number.
Arguments:
i {[int]} -- [integer]
"""

hexrep = format(i,'08x')
thing = ""
for i in [3,2,1,0]:
thing += hexrep[2*i:2*i+2]
return thing

def pad(bitString):
"""[summary]
Fills up the binary string to a 512 bit binary string
Arguments:
bitString {[string]} -- [binary string]
Returns:
[string] -- [binary string]
"""

startLength = len(bitString)
bitString += '1'
while len(bitString) % 512 != 448:
Expand All @@ -26,6 +56,15 @@ def pad(bitString):
return bitString

def getBlock(bitString):
"""[summary]
Iterator:
Returns by each call a list of length 16 with the 32 bit
integer blocks.
Arguments:
bitString {[string]} -- [binary string >= 512]
"""

currPos = 0
while currPos < len(bitString):
currPart = bitString[currPos:currPos+512]
Expand All @@ -34,6 +73,7 @@ def getBlock(bitString):
mySplits.append(int(rearrange(currPart[32*i:32*i+32]),2))
yield mySplits
currPos += 512

def not32(i):
i_str = format(i,'032b')
new_str = ''
Expand All @@ -48,6 +88,13 @@ def leftrot32(i,s):
return (i << s) ^ (i >> (32-s))

def md5me(testString):
"""[summary]
Returns a 32-bit hash code of the string 'testString'
Arguments:
testString {[string]} -- [message]
"""

bs =''
for i in testString:
bs += format(ord(i),'08b')
Expand Down

0 comments on commit dbfc220

Please sign in to comment.