How does MD5 (Message Digest 5) work?

MD5 takes as input a secret text that will be hashed:



Which has the following representation in bits

Try to fill the gaps yourself, or click the complete fields button:


Your message contains bits. The message will be padded to make it 448 mod 512 bits long. Since your message is we will add because: MOD 512 = We will add ONE 1 and ZEROS. After this operation a 64 bit value which represents the original length of the message is added. For this particular message the input value added will be . So the message after all appending operations are complete will look like, at the bit level the text box below. If you look at the end of message you should be able to identify the added bits.


A 128 bit buffer (4 registers, 32 bits each) is used to hold the intermediate and final result of the hash function. They are initialized to the following values:

A = 1732584193
B = -271733879
C = -1732584194
D = 271733878

We first define four auxiliary functions that each take as input three 32-bit words and produce as output one 32-bit word.

F(X,Y,Z) = XY v not(X) Z
G(X,Y,Z) = XZ v Y not(Z)
H(X,Y,Z) = X xor Y xor Z
I(X,Y,Z) = Y xor (X v not(Z))

In each bit position F acts as a conditional: if X then Y else Z. The function F could have been defined using + instead of v since XY and not(X)Z will never have 1's in the same bit position.) It is interesting to note that if the bits of X, Y, and Z are independent and unbiased, the each bit of F(X,Y,Z) will be independent and unbiased.

The functions G, H, and I are similar to the function F, in that they act in "bitwise parallel" to produce their output from the bits of X, Y, and Z, in such a manner that if the corresponding bits of X, Y, and Z are independent and unbiased, then each bit of G(X,Y,Z), H(X,Y,Z), and I(X,Y,Z) will be independent and unbiased. Note that the function H is the bit-wise "xor" or "parity" function of its inputs.

At this point the message is passed in blocks of 512 bits through the the compression function as seen below:

Figure taken from Cryptography and Network Security: Principles and Practice by William Stallings


Calculate rounds:

After all the 512 bit blocks have been processed a 128 bit message digest is produced, which is a function of all the bits of your message.
To produce this digest just sum A, B, C and D and convert it to hexadecimal.

The final result is: