What is the MD5 Algorithm?
Last Updated :
24 Jul, 2024
In Cryptography, the MD5 message-digest algorithm is a cryptographic hash function designed to convert a message into a 128-bit hash value. Due to its vulnerabilities including hash collision and preimage and second preimage attacks, which makes it possible to reverse-engineer digest value, it is replaced by more secure algorithms like SHA1, SHA256, etc. Even with its insecurity, MD5 is still the most commonly used message digest for non-cryptographic functions, such as used as a checksum to verify data integrity, compressing large files into smaller ones securely, etc. In this article, we will learn about the MD5 algorithm, how it works, its use case, advantages, and disadvantages.
What is MD5?
MD5 is a cryptographic hash function algorithm that takes the message as input of any length and changes it into a fixed-length message of 16 bytes. MD5 algorithm stands for the message-digest algorithm. MD5 was developed in 1991 by Ronald Rivest as an improvement of MD4, with advanced security purposes. The output of MD5 (Digest size) is always 128 bits.
-(1).jpg)
Overview Of MD5 Algorithm
Working of the MD5 Algorithm
MD5 algorithm follows the following stepsÂ
1. Append Padding Bits: In the first step, we add padding bits in the original message in such a way that the total length of the message is 64 bits less than the exact multiple of 512. Â
Suppose we are given a message of 1000 bits. Now we have to add padding bits to the original message. Here we will add 472 padding bits to the original message. Â After adding the padding bits the size of the original message/output of the first step will be 1472 i.e. 64 bits less than an exact multiple of 512 (i.e. 512*3 = 1536).
Length(original message + padding bits) = Â 512 * i – 64 where i = 1,2,3 . . .Â
-(2).jpg)
One Round of MD5 Operation
2. Append Length Bits: In this step, we add the length bit in the output of the first step in such a way that the total number of the bits is the perfect multiple of 512. Simply, here we add the 64-bit as a length bit in the output of the first step.Â
i.e. output of first step = 512 * n – 64Â
length bits = 64.Â
After adding both we will get 512 * n i.e. the exact multiple of 512.
3. Initialize MD buffer: Here, we use the 4 buffers i.e. A B, C, and D. The size of each buffer is 32 bits. Â
- A = 0x67425301
- B = 0xEDFCBA45
- C= 0x98CBADFE
- D = 0x13DCE476
-(1).jpg)
F,G,H and I functions
4. Process Each 512-bit Block: This is the most important step of the MD5 algorithm. Here, a total of 64 operations are performed in 4 rounds. In the 1st round, 16 operations will be performed, 2nd round 16 operations will be performed, 3rd round 16 operations will be performed, and in the 4th round, 16 operations will be performed. We apply a different function on each round i.e. for the 1st round we apply the F function, for the 2nd G function, 3rd for the H function, and 4th for the I function.Â
We perform OR, AND, XOR, and NOT (basically these are logic gates) for calculating functions. We use 3 buffers for each function i.e. B, C, D.
.jpg)
Process P Operation
After applying the function now we perform an operation on each block. For performing operations we needÂ
- add modulo 232
- M[i] – 32 bit message.
- K[i] – 32-bit constant.
- <<<n – Left shift by n bits.
Now take input as initialize MD buffer i.e. A, B, C, D. Output of  B will be fed in C, C will be fed into D, and D will be fed into J. After doing this now we perform some operations to find the output for A.
- In the first step, Outputs of B, C, and D are taken and then the function F is applied to them. We will add modulo 232 Â bits for the output of this with A.
- In the second step, we add the M[i] bit message with the output of the first step.
- Then add 32 bits constant i.e. K[i] to the output of the second step.Â
- At last, we do left shift operation by n (can be any value of n) and addition modulo by 232.
After all steps, the result of A will be fed into B. Now same steps will be used for all functions G, H, and I. After performing all 64 operations we will get our message digest.
Output:
After all, rounds have been performed, the buffer A, B, C, and D contains the MD5 output starting with the lower bit A and ending with Higher bits D.
Code:
Python
# importing the required libraries
import hashlib
# making a message
inputstring = "This is a message sent by a computer user."
# encoding the message using the library function
output = hashlib.md5(inputstring.encode())
# printing the hash function
print("Hash of the input string:")
print(output.hexdigest())
OutputHash of the input string:
922547e866c89b8f677312df0ccec8ee
What is MD5 Algorithm Used For?
- MD5 is used as a checksum to verify the integrity of files and data by comparing the hash of the original file with the file received to check if the files or data has been altered.
- MD5 is used for data security and encryption e.g. Secure password of users in database and non-sensitive data.
- It is used in version control systems to manage different versions of files.
- It was earlier used in digital signatures and certificate but due it’s vulnerabilities, it has been replaced by more secure algorithms like SHA-256.
Advantages of MD5 Algorithm
- MD5 is faster and simple to understand.
- MD5 algorithm generates a strong password in 16 bytes format. All developers like web developers, etc. use the MD5 algorithm to secure the password of users.Â
- To integrate the MD5 algorithm, relatively low memory is necessary.Â
- It is very easy and faster to generate a digest message of the original message.
Disadvantages of MD5 Algorithm
- MD5 generates the same hash function for different inputs (hash collision).Â
- MD5 provides poor security over SHA1, SHA256 and other modern cryptographic algorithms.
- MD5 has been considered an insecure algorithm. So now we are using SHA256 instead of MD5.Â
- MD5 is neither a symmetric nor asymmetric algorithm.
Conclusion
Despite it’s historically significant, MD5 is not suitable for modern security purposes as it is possible to engineer hash collision in this algorithm. It is used in non-critical application for its speed and simplicity. It is advised to use more secure hashing algorithms like SHA256, SHA3, etc. for maintaining better confidentiality, integrity, and authenticity of data.
Frequently Asked Questions on MD5 Algorithm -FAQs
What is the length of an MD5 hash?
An MD5 hash has 128 bits, which is typically represented as a 32-character hexadecimal number.
How is MD5 used in practice?
Despite its vulnerabilities, MD5 is still used in some non-security-critical applications. Common uses include:
- It is used in checksums for ensuring file integrity alteration.
- It is used for hashing password in some legacy systems.
- It is used in digital signatures for non-sensitive documents.
- It is used in hash tables for quicker comparison of data.
What are the alternatives to MD5?
More secure hashing algorithms such as SHA-256, SHA-3 and other members of Secure Hash Algorithm (SHA) family can be used instead of MD5. These algorithm provide better security and resistance to collisions.