2

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

1 Answers1

0

Python borrows OpenSSL primitives for most cryptographic purposes; while it's not definitive, this post implies that the OpenSSL developers, knowing MD5 is broken, probably haven't put much effort into optimizing MD5, while SHA1 was intensively optimized; MD5 may be simpler algorithmically, but if the code is less heavily optimized than the SHA1 code, then the already small advantage MD5 has won't be noticeable.

On another post comparing OpenSSL's MD5 and SHA1 implementations (without the Python wrapping), it looks like core OpenSSL is usually faster at SHA1, though it has varied by version/build (SHA1 was faster on OSX 10.8 and 10.10, slower on 10.9).

Community
  • 1
  • 1
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271