Cryptography Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.00

Message Digest - MD5 Algorithm

Part:   1  2  3  4  5 

This tutorial helps you to understand

  • What is MD5?
  • MD5 Algorithm Overview
  • MD5 Implementation in Java
  • MD5 Implementation in PHP
  • MD5 Implementation in Perl

What is MD5?

Based on the MD5 RFC document, MD5 is message-digest algorithm, which takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. It is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given prespecified target message digest.

MD5 was designed by Ronald Rivest in 1991 to replace an earlier hash function, MD4. MD5 is more secure than MD4. However a number of weaknesses have been found in recent years. The most recent paper published in this area shows that a collision of MD5 can be found within one minute on a standard notebook PC, using a method called tunneling.

Despite its weaknesses, MD5 is widely used in digital signature processes. It's been implemented in many programming languages.

MD5 Algorithm Overview

MD5 algorithm is well described in RFC 1321 - The MD5 Message-Digest Algorithm, see http://www.ietf.org/rfc/rfc1321.txt. Below is a quick overview of the algorithm.

MD5 algorithm consists of 5 steps:

Step 1. Appending Padding Bits. The original message is "padded" (extended) so that its length (in bits) is congruent to 448, modulo 512. The padding rules are:

  • The original message is always padded with one bit "1" first.
  • Then zero or more bits "0" are padded to bring the length of the message up to 64 bits fewer than a multiple of 512.

Step 2. Appending Length. 64 bits are appended to the end of the padded message to indicate the length of the original message in bytes. The rules of appending length are:

  • The length of the original message in bytes is converted to its binary format of 64 bits. If overflow happens, only the low-order 64 bits are used.
  • Break the 64-bit length into 2 words (32 bits each).
  • The low-order word is appended first and followed by the high-order word.

(Continued on next part...)

Part:   1  2  3  4  5 

Dr. Herong Yang, updated in 2007
Cryptography Tutorials - Herong's Tutorial Notes - Message Digest - MD5 Algorithm