0
$\begingroup$

In the hashlib module in python is a function called m.update() where m is a hashlib.md5 object. My question, actually not specific to python, is how does this function work, such that:

m = hashlib.md5()
m.update("hello")
m.update(" world")

which is the same as if I would have done:

a.hashlib.md5()
a.update("hello world")

How does the math work to explain this?

$\endgroup$
  • 1
    $\begingroup$ Welcome to the InfoSec stack exchange! This question is probably better answered by the Cryptography SE, and will most likely get migrated there. $\endgroup$ – RoraΖ Oct 17 '14 at 14:14
2
$\begingroup$

It is an object, not a function. The difference is important: the thing you get with m = hashlib.md5() is a structure in memory that has an internal state. Each update() call updates that state (hence the name).

You can imagine the object has a kind of box. Each update() call accumulates input bytes in that box. When you want to obtain the hash value, the MD5 mathematical function is applied to the complete box contents. That way, it does not matter whether you put the bytes in the box as one or two or seventeen successive calls; what matters is that you ultimately sent all the bytes in the box.

Technically, MD5 processes input data by blocks of 512 bits (64 bytes) and maintains a running state consisting of 128 bytes (16 bytes) along with a counter (the number of bytes that were hashed is used at the end of the function evaluation). Thus, a practical MD5 implementation will indeed contain a "box" (usually called a "buffer") that can accumulate up to 64 bytes; whenever 64 bytes have been accumulated, they are processed as per the MD5 specification, and the box is emptied. That way, the object remains small in RAM, even if you hash gigabytes of data.

Reading the specification to implement your own MD5 code is a very nice programming exercise, that is very enlightening. I recommend that you try it.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thx! Now I got it. I will do over the weekend^^ $\endgroup$ – tagloul Oct 17 '14 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy