Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not from the known digest, but from the known state. You can use a pure python MD5 implementation and save its state. Here is an example using <a href="https://bitbucket.org/pypy/pypy/raw/tip/lib_pypy/_md5.py" rel="nofollow">_md5.py from from PyPy</a>:</p> <pre><code>import _md5 def md5_getstate(md): return (md.A, md.B, md.C, md.D, md.count + [], md.input + [], md.length) def md5_continue(state): md = _md5.new() (md.A, md.B, md.C, md.D, md.count, md.input, md.length) = state return md m1 = _md5.new() m1.update("hello, ") state = md5_getstate(m1) m2 = md5_continue(state) m2.update("world!") print m2.hexdigest() m = _md5.new() m.update("hello, world!") print m.hexdigest() </code></pre> <p>As e.dan noted, you can also use almost any checksuming algorithm (CRC, Adler, Fletcher), but they do not protect you well from the intentional data modification, only from the random errors.</p> <p>EDIT: of course, you can also re-implement the serialization method using ctypes from the thread you referenced in a more portable way (without magic constants). I believe this should be version/architecture independent (tested on python 2.4-2.7, both i386 and x86_64):</p> <pre><code># based on idea from http://groups.google.com/group/comp.lang.python/msg/b1c5bb87a3ff5e34 try: import _md5 as md5 except ImportError: # python 2.4 import md5 import ctypes def md5_getstate(md): if type(md) is not md5.MD5Type: raise TypeError, 'not an MD5Type instance' return ctypes.string_at(id(md) + object.__basicsize__, md5.MD5Type.__basicsize__ - object.__basicsize__) def md5_continue(state): md = md5.new() assert len(state) == md5.MD5Type.__basicsize__ - object.__basicsize__, \ 'invalid state' ctypes.memmove(id(md) + object.__basicsize__, ctypes.c_char_p(state), len(state)) return md m1 = md5.new() m1.update("hello, ") state = md5_getstate(m1) m2 = md5_continue(state) m2.update("world!") print m2.hexdigest() m = md5.new() m.update("hello, world!") print m.hexdigest() </code></pre> <p>It is not Python 3 compatible, since it does not have an _md5/md5 module.</p> <p>Unfortunately hashlib's openssl_md5 implementation is not suitable for such hacks, since OpenSSL EVP API does not provide any calls/methods to reliably serialize EVP_MD_CTX objects.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload