Skip to content

SHA512 from Cryptodome.Hash slower than sha512 from hashlib #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vstoykovbg opened this issue Nov 18, 2019 · 3 comments
Closed

SHA512 from Cryptodome.Hash slower than sha512 from hashlib #348

vstoykovbg opened this issue Nov 18, 2019 · 3 comments

Comments

@vstoykovbg
Copy link

vstoykovbg commented Nov 18, 2019

Example code:

#!/usr/bin/python3

from hashlib import sha512
from Cryptodome.Hash import SHA512
import time

initial_digest=b'test.test.test.test.test'
iterations=500000

# *******************

digest=initial_digest

start = time.time()

for counter in range(iterations):
    digest=sha512(digest).digest()


end = time.time()
print("  Time (hashlib sha512):\t", end - start)

# *******************

digest=initial_digest

start = time.time()

for counter in range(iterations):
    digest=SHA512.new(data=digest).digest()


end = time.time()
print("Time (cryptodome sha512):\t", end - start)


# *******************

digest=initial_digest

start = time.time()

MySHA512=SHA512.new

for counter in range(iterations):
    digest=MySHA512(data=digest).digest()


end = time.time()
print("Time (cryptodome sha512):\t", end - start)

Output on my computer:

valentin@computer:~/python/test3$ ./test-sha512.py
  Time (hashlib sha512):	 0.5348014831542969
Time (cryptodome sha512):	 7.416509389877319
Time (cryptodome sha512):	 7.28940749168396
valentin@computer:~/python/test3$ ./test-sha512.py
  Time (hashlib sha512):	 0.5275444984436035
Time (cryptodome sha512):	 7.590082168579102
Time (cryptodome sha512):	 7.583686351776123
valentin@computer:~/python/test3$ ./test-sha512.py
  Time (hashlib sha512):	 0.5171098709106445
Time (cryptodome sha512):	 7.440552234649658
Time (cryptodome sha512):	 7.221668243408203
valentin@computer:~/python/test3$ 

Is this because of Cryptodome using more object oriented approach? Or the underlying implementation of the algorithm is somehow inefficient?

@Legrandin
Copy link
Owner

Please see #277.

@ghost
Copy link

ghost commented Jan 19, 2023

Is there any reason to ever prefer Cryptodome's hash functions over hashlib then?

@Akuli
Copy link

Akuli commented Jan 21, 2023

A couple reasons:

  • A few other functions in pycryptodome want a hash object created with pycryptodome.
  • pycryptodome supports more different kinds of hashes than the standard library.

If all you want to do is to get a sha512 hash as string or bytes, you don't need pycryptodome's hash functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants