I want to implement MD5 in my program (for education).
And i have a problem with understanding.
On Wikipedia there is written:
- append “1” bit to message
- append “0” bits until message length in bits ≡ 448 (mod 512)
- append length mod (2 pow 64) to message
And my idea is like that:
- get size of msg
- until size >= 64 ==> memcpy 64 bytes to buffer and calculate functions
- if size < 64, memcpy “size” bytes and prepare padding.
And now, lets say, message is 127 bytes. Program takes first 64 bytes, calculate, and then wants to take second part. Took 63 bytes (504 bits), so need to add padding. Adds 1, so now its 505 bits.
What now? I should add zeroes up to 448 (mod 512), so 448+7 zeroes? So there will be two chunks again? Or what?
Im asking becouse it looks werid for me 😉 Probably its good, but.. i dont know.
Thanks in advance, M
The Wikipedia description is correct. There is no possible way to design a padding scheme that won’t add an extra block at least some of the time. (Provable trivially by counting.)