mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-01 00:41:09 +00:00
Added pytests to hashes/md5.py (#1100)
* Added pytests to sha1.py * tweaking md5 * Added Pytests to hashes/md5.py
This commit is contained in:
parent
89acf5d017
commit
d21b4cfb48
|
@ -1,6 +1,7 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
def rearrange(bitString32):
|
def rearrange(bitString32):
|
||||||
"""[summary]
|
"""[summary]
|
||||||
Regroups the given binary string.
|
Regroups the given binary string.
|
||||||
|
@ -13,6 +14,8 @@ def rearrange(bitString32):
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
[string] -- [32 bit binary string]
|
[string] -- [32 bit binary string]
|
||||||
|
>>> rearrange('1234567890abcdfghijklmnopqrstuvw')
|
||||||
|
'pqrstuvwhijklmno90abcdfg12345678'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if len(bitString32) != 32:
|
if len(bitString32) != 32:
|
||||||
|
@ -22,12 +25,15 @@ def rearrange(bitString32):
|
||||||
newString += bitString32[8*i:8*i+8]
|
newString += bitString32[8*i:8*i+8]
|
||||||
return newString
|
return newString
|
||||||
|
|
||||||
|
|
||||||
def reformatHex(i):
|
def reformatHex(i):
|
||||||
"""[summary]
|
"""[summary]
|
||||||
Converts the given integer into 8-digit hex number.
|
Converts the given integer into 8-digit hex number.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
i {[int]} -- [integer]
|
i {[int]} -- [integer]
|
||||||
|
>>> reformatHex(666)
|
||||||
|
'9a020000'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
hexrep = format(i, '08x')
|
hexrep = format(i, '08x')
|
||||||
|
@ -36,6 +42,7 @@ def reformatHex(i):
|
||||||
thing += hexrep[2*i:2*i+2]
|
thing += hexrep[2*i:2*i+2]
|
||||||
return thing
|
return thing
|
||||||
|
|
||||||
|
|
||||||
def pad(bitString):
|
def pad(bitString):
|
||||||
"""[summary]
|
"""[summary]
|
||||||
Fills up the binary string to a 512 bit binary string
|
Fills up the binary string to a 512 bit binary string
|
||||||
|
@ -46,7 +53,6 @@ def pad(bitString):
|
||||||
Returns:
|
Returns:
|
||||||
[string] -- [binary string]
|
[string] -- [binary string]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
startLength = len(bitString)
|
startLength = len(bitString)
|
||||||
bitString += '1'
|
bitString += '1'
|
||||||
while len(bitString) % 512 != 448:
|
while len(bitString) % 512 != 448:
|
||||||
|
@ -55,6 +61,7 @@ def pad(bitString):
|
||||||
bitString += rearrange(lastPart[32:]) + rearrange(lastPart[:32])
|
bitString += rearrange(lastPart[32:]) + rearrange(lastPart[:32])
|
||||||
return bitString
|
return bitString
|
||||||
|
|
||||||
|
|
||||||
def getBlock(bitString):
|
def getBlock(bitString):
|
||||||
"""[summary]
|
"""[summary]
|
||||||
Iterator:
|
Iterator:
|
||||||
|
@ -74,7 +81,12 @@ def getBlock(bitString):
|
||||||
yield mySplits
|
yield mySplits
|
||||||
currPos += 512
|
currPos += 512
|
||||||
|
|
||||||
|
|
||||||
def not32(i):
|
def not32(i):
|
||||||
|
'''
|
||||||
|
>>> not32(34)
|
||||||
|
4294967261
|
||||||
|
'''
|
||||||
i_str = format(i, '032b')
|
i_str = format(i, '032b')
|
||||||
new_str = ''
|
new_str = ''
|
||||||
for c in i_str:
|
for c in i_str:
|
||||||
|
@ -82,11 +94,15 @@ def not32(i):
|
||||||
return int(new_str, 2)
|
return int(new_str, 2)
|
||||||
|
|
||||||
def sum32(a, b):
|
def sum32(a, b):
|
||||||
|
'''
|
||||||
|
|
||||||
|
'''
|
||||||
return (a + b) % 2**32
|
return (a + b) % 2**32
|
||||||
|
|
||||||
def leftrot32(i, s):
|
def leftrot32(i, s):
|
||||||
return (i << s) ^ (i >> (32-s))
|
return (i << s) ^ (i >> (32-s))
|
||||||
|
|
||||||
|
|
||||||
def md5me(testString):
|
def md5me(testString):
|
||||||
"""[summary]
|
"""[summary]
|
||||||
Returns a 32-bit hash code of the string 'testString'
|
Returns a 32-bit hash code of the string 'testString'
|
||||||
|
@ -107,7 +123,7 @@ def md5me(testString):
|
||||||
c0 = 0x98badcfe
|
c0 = 0x98badcfe
|
||||||
d0 = 0x10325476
|
d0 = 0x10325476
|
||||||
|
|
||||||
s = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, \
|
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, \
|
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, \
|
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 ]
|
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 ]
|
||||||
|
@ -142,14 +158,19 @@ def md5me(testString):
|
||||||
c0 = sum32(c0, C)
|
c0 = sum32(c0, C)
|
||||||
d0 = sum32(d0, D)
|
d0 = sum32(d0, D)
|
||||||
|
|
||||||
digest = reformatHex(a0) + reformatHex(b0) + reformatHex(c0) + reformatHex(d0)
|
digest = reformatHex(a0) + reformatHex(b0) + \
|
||||||
|
reformatHex(c0) + reformatHex(d0)
|
||||||
return digest
|
return digest
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
assert md5me("") == "d41d8cd98f00b204e9800998ecf8427e"
|
assert md5me("") == "d41d8cd98f00b204e9800998ecf8427e"
|
||||||
assert md5me("The quick brown fox jumps over the lazy dog") == "9e107d9d372bb6826bd81d3542a419d6"
|
assert md5me(
|
||||||
|
"The quick brown fox jumps over the lazy dog") == "9e107d9d372bb6826bd81d3542a419d6"
|
||||||
print("Success.")
|
print("Success.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test()
|
test()
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user