Compare them all in 5 mins with a sample
In the previous post, we went through the sha256 algorithm step by step. however, it is not perfect, at least not as secure as the sha3 family.
It may not be the best choice to use in production. let’s compare with other hashing algorithms and find out when to use what.
When choosing hashing algorithm, the trade-off is always between security and performance.
Let’s find out.
Speed
When talking about performance, Always measure.
Let’s do a simple one.
import hashlib
import zlib
import time
import uuid
def test(hash_func):
ts = time.time()
for _ in range(500):
hash_func((str(uuid.uuid4()) *500).encode('utf-8')).hexdigest()
print(f'{hash_func} took {time.time()-ts} ms.')
def crc():
ts = time.time()
for _ in range(100):
hex(zlib.crc32( (str(uuid.uuid4())*500).encode('utf-8') ) )
print(f'crc32 took {time.time()-ts} ms.')
algos = [hashlib.sha1, hashlib.sha256, hashlib.sha384, hashlib.sha512, hashlib.sha3_256, hashlib.sha3_384,hashlib.sha3_512, hashlib.md5]
for a in algos:
test(a)
crc()
<built-in function openssl_sha1> took 0.019008159637451172 ms.
<built-in function openssl_sha256> took 0.035021066665649414 ms.
<built-in function openssl_sha384> took 0.026027441024780273 ms.
<built-in function openssl_sha512> took 0.030031681060791016 ms.
<built-in function openssl_sha3_256> took…