Skip to content

Commit dbfc220

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#294 from TheAlgorithms/documented_md5_hash
Documented md5 hash
2 parents 060988b + 0494d48 commit dbfc220

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

hashes/md5.py

+47
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
import math
33

44
def rearrange(bitString32):
5+
"""[summary]
6+
Regroups the given binary string.
7+
8+
Arguments:
9+
bitString32 {[string]} -- [32 bit binary]
10+
11+
Raises:
12+
ValueError -- [if the given string not are 32 bit binary string]
13+
14+
Returns:
15+
[string] -- [32 bit binary string]
16+
"""
17+
518
if len(bitString32) != 32:
619
raise ValueError("Need length 32")
720
newString = ""
@@ -10,13 +23,30 @@ def rearrange(bitString32):
1023
return newString
1124

1225
def reformatHex(i):
26+
"""[summary]
27+
Converts the given integer into 8-digit hex number.
28+
29+
Arguments:
30+
i {[int]} -- [integer]
31+
"""
32+
1333
hexrep = format(i,'08x')
1434
thing = ""
1535
for i in [3,2,1,0]:
1636
thing += hexrep[2*i:2*i+2]
1737
return thing
1838

1939
def pad(bitString):
40+
"""[summary]
41+
Fills up the binary string to a 512 bit binary string
42+
43+
Arguments:
44+
bitString {[string]} -- [binary string]
45+
46+
Returns:
47+
[string] -- [binary string]
48+
"""
49+
2050
startLength = len(bitString)
2151
bitString += '1'
2252
while len(bitString) % 512 != 448:
@@ -26,6 +56,15 @@ def pad(bitString):
2656
return bitString
2757

2858
def getBlock(bitString):
59+
"""[summary]
60+
Iterator:
61+
Returns by each call a list of length 16 with the 32 bit
62+
integer blocks.
63+
64+
Arguments:
65+
bitString {[string]} -- [binary string >= 512]
66+
"""
67+
2968
currPos = 0
3069
while currPos < len(bitString):
3170
currPart = bitString[currPos:currPos+512]
@@ -34,6 +73,7 @@ def getBlock(bitString):
3473
mySplits.append(int(rearrange(currPart[32*i:32*i+32]),2))
3574
yield mySplits
3675
currPos += 512
76+
3777
def not32(i):
3878
i_str = format(i,'032b')
3979
new_str = ''
@@ -48,6 +88,13 @@ def leftrot32(i,s):
4888
return (i << s) ^ (i >> (32-s))
4989

5090
def md5me(testString):
91+
"""[summary]
92+
Returns a 32-bit hash code of the string 'testString'
93+
94+
Arguments:
95+
testString {[string]} -- [message]
96+
"""
97+
5198
bs =''
5299
for i in testString:
53100
bs += format(ord(i),'08b')

0 commit comments

Comments
 (0)