1

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>
Regis May
  • 3,070
  • 2
  • 30
  • 51
  • 3
    I cannot reproduce this. I get two different `` 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` – juanpa.arrivillaga Feb 25 '19 at 23:48
  • That's strange. I get 'False' as well, but it seems to be the same object. – Regis May Feb 26 '19 at 06: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. – juanpa.arrivillaga Feb 26 '19 at 06:36
  • Possible duplicate of [Unnamed Python objects have the same id](https://stackoverflow.com/questions/24802740/unnamed-python-objects-have-the-same-id) – juanpa.arrivillaga Feb 26 '19 at 06:38
  • Interesting. GC apparently works a bit differently in all other programming languages I ever dealt with. Thanks a lot for your help! – Regis May Feb 26 '19 at 06:47

1 Answers1

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
Aemyl
  • 1,501
  • 1
  • 19
  • 34