2

hashlib contains implementations for hash algorithms. Unfortunately two consecuting calls to hashlib.sha256() do not produce two different instances of SHA256 but the same one: If called twice hashlib.sha256() will return the same object each time. So apparently we have a singleton here

This is bad in a all situations where any kind of concurrency is required. Additionally this is very bad in situations where you want to provide a hashing object to some algorithm. In my case: Tests fail as the same object is reused instead of creating a new one.

My question: How an I create two instances of SHA256 (or any other hash algorithm)?

Example:

import hashlib

print(hashlib.sha256())
print(hashlib.sha256())

This will output something like:

<sha256 HASH object @ 0x7fb3611b4710>
<sha256 HASH object @ 0x7fb3611b4710>
4
  • 3
    I cannot reproduce this. I get two different <sha256 HASH object @ 0x101f9a260> objects ... can you provide the code you are using to determine this? I suspect you are misusing id (rather, falling into the all too common trap). Note, for me hashlib.sha256() is hashlib.sha256() returns False Commented Feb 25, 2019 at 23:48
  • That's strange. I get 'False' as well, but it seems to be the same object. Commented Feb 26, 2019 at 6:26
  • 2
    It is not the same object then. It is exactly as I suspected, you are printing the object's representation, which include's it's id, i.e. in this example 0x7fb3611b4710, because in CPython, as soon as the reference count reaches zero, the object is reclaimed. The object only exists long enought to be passed into print. The CPython runtime optimizes allocations, is is perfectly happy to reuse the memory from the last object. Commented Feb 26, 2019 at 6:36
  • Interesting. GC apparently works a bit differently in all other programming languages I ever dealt with. Thanks a lot for your help! Commented Feb 26, 2019 at 6:47

1 Answer 1

1

In your first example, the second hash-object is created after your first hash-object was garbage-collected. Therefore they can have the same memory address. In hashlib.sha256() is hashlib.sha256() the first one can't be garbage-collected since it has to be compared first. You can save the hash-objects in varibles to keep them alive:

h1 = hashlib.sha256()
h2 = hashlib.sha256()
print(h1 is h2)

[Output]
False
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.