I have tested the speed of md5, sha1, crc32, the result shows that md5 is slower than sha1, I am confused about it becasue sha1 is more compliacted than md5.
I executed my test python script to hash a png image(you can use an image whatever you want) 10000 times like: crc_vs_md5.py C:\Users\bigwalnut\Desktop\def.png 10000
result(unit: s): md5: 0.122737341241 sha1: 0.0984247229759 crc32: 0.0577822398549
Here is the code:
from hashlib import md5, sha1
from zlib import crc32
import sys
import time
numberoftime = 0
content = None
def getMd5():
for x in range(0, numberoftime):
mdfive = md5()
mdfive.update(content)
mdfive.hexdigest()
def getSha1():
for x in range(0, numberoftime):
sha1Obj = sha1()
sha1Obj.update(content)
sha1Obj.hexdigest()
def getCrc32():
for x in range(0, numberoftime):
crc32(content) & 0xffffffff
if len(sys.argv) < 3:
print('You must enter: filename, numberoftime')
exit(1)
elif len(sys.argv) > 3:
print('Only filename, numberoftime is permitted')
exit(1)
filename = sys.argv[1]
numberoftime = int(sys.argv[2])
print filename, numberoftime
with open(filename, 'rb') as f:
content = f.read()
start = time.clock()
getMd5()
stop = time.clock()
print "md5: ", stop - start
start = time.clock()
getSha1()
stop = time.clock()
print "sha1: ", stop - start
start = time.clock()
getCrc32()
stop = time.clock()
print "crc32: ", stop - start