8

I have some code that needs to hash certain data, then later on, in another process, continue the hash with more data.

Is there a way to create an object, either from the md5 or hashlib modules, which has a different initial value than 'd41d8cd98f00b204e9800998ecf8427e'? What I mean is something similar to:

x = md5.from_digest('0123456789abcdef')
x.update(new_data)

Note: the less desirable approach would be to save the original md5 object and restore it later, but afaik HASH objects are non-pickleable.

itai
  • 1,566
  • 1
  • 12
  • 25
  • 3
    Looks like the answer is no: http://stackoverflow.com/questions/12592501/serialize-md5-computation-state-and-resume-later and the related issue: http://bugs.python.org/issue16059 – Jon Clements Dec 31 '13 at 12:59
  • Perhaps a rather naive workaround would be a separate process whose only job is to take data digest it, and hang around indefinitely receiving further update/digest requests... – Jon Clements Dec 31 '13 at 13:02
  • @JonClements Thanks for the links! I've looked at a few dozens of questions before posting but couldn't find it. Reading the wiki article for md5, I cant understand the reason the maintainers closed the issue since the "states" they talk about seem to be the digest itself divided into to 4 parts. The worker process solution can't work for me since the initial digest is moving around in different environments.. – itai Dec 31 '13 at 13:12
  • 1
    @itai The problem with getting and setting the internal state is that in various *implementations* of MD5 used (e.g. from OpenSSL), the internal state is not at all accessible, as far as that maintainer knows. In other words, unless you rip out the hash implementations and write your own (very bad idea and a lot of work too), you *can't* affect the internal state in anyway except feeding it data to be hashes. –  Dec 31 '13 at 13:25

1 Answers1

1

They(@jon-clements, @itai, @delnan) are right. Until now , you can't , unless you implement one. There are some examples : http://equi4.com/md5/pymd5.py http://rosettacode.org/wiki/MD5/Implementation

JJP
  • 825
  • 1
  • 7
  • 12