19

How do you flush (or reset) and reuse an instance of hashlib.md5 in python? If I am performing multiple hashing operations in a script, it seems inefficient to use a new instance of hashlib.md5 each time, but from the python documentation I don't see any way to flush or reset the instance.

Steve
  • 199
  • 1
  • 3

2 Answers2

8

Why do you think it's inefficient to make a new one? It's a small object, and objects are created and destroyed all the time. Use a new one, and don't worry about it.

Ned Batchelder
  • 323,515
  • 67
  • 518
  • 625
  • 3
    There are other reasons than speed to reuse a hashlib object. For example, you may recieve the hashlib object (which may be md5, sha1, ...) and want to reuse it to create the checksum of multiple files. In that case, one could pass a lambda wich will create a new hashlib object when called, but that's cumbersome. – Suzanne Soy Sep 30 '13 at 22:51
  • 1
    This isn't an answer, it's a rebuke of the question. At best a comment – Neowizard May 05 '20 at 12:15
  • @Neowizard feel free to answer the question however you wish. – Ned Batchelder May 05 '20 at 17:48
-2

Here's what I did, just write a little wrapper that reinitializes the hash object. Handles the clunkiness of the code writing, but maybe not the efficiency at runtime.

def Hasher(object):
    def __init__(self):
        self.md5 = hashlib.md5()

    def get_hash(self, o):
        self.md5.update(o)
        my_hash = self.md5.digest()
        self.md5 = hashlib.md5()
        return my_hash
Kevin Hill
  • 364
  • 4
  • 15
  • ha, that's not reusing the object. I guess there's no `reset` method in this api, so it's not possible to reuse?, or is there a value you can pass to the `update` method to reset the object? – Gubatron Nov 04 '14 at 03:11