The Wayback Machine - https://web.archive.org/web/20241002103703/https://www.geeksforgeeks.org/what-is-the-md5-algorithm/
Open In App

What is the MD5 Algorithm?

Last Updated : 24 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Overview Of MD5 Algorithm

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 . . . 

One Round of MD5 Operation

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
F,G,H and I functions

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.

Process P Operation

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())

Output
Hash 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:

  1. It is used in checksums for ensuring file integrity alteration.
  2. It is used for hashing password in some legacy systems.
  3. It is used in digital signatures for non-sensitive documents.
  4. 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.



Similar Reads

Difference between MD5 and SHA1
MD5 stands for Message Digest and SHA1 stands for Secure Hash Algorithm both are cryptographic hash algorithms used for security purposes. SHA-1 or Secure Hash Algorithm 1 is a cryptographic algorithm that takes an input and produces a 160-bit (20-byte) hash value. This hash value is known as a message digest. In this article, we will discuss the d
5 min read
MD5 hash in Python
Cryptographic hashes are used in day-day life like in digital signatures, message authentication codes, manipulation detection, fingerprints, checksums (message integrity check), hash tables, password storage and much more. They are also used in sending messages over network for security or storing messages in databases. There are many hash functio
5 min read
Different Types of Clustering Algorithm
The introduction to clustering is discussed in this article and is advised to be understood first. The clustering Algorithms are of many types. The following overview will only list the most prominent examples of clustering algorithms, as there are possibly over 100 published clustering algorithms. Not all provide models for their clusters and can
5 min read
Probabilistic shortest path routing algorithm for optical networks
Data transfer operations is a crucial aspect in case of networking and routing. So efficient data transfer operations is a must need, with minimum hardware cost (Optical Cables, WDM Network components, Decoders, Multiplexers) and also in the minimum time possible. Thus, the need is to propose an algorithm that finds the shortest path between two no
5 min read
RSA Algorithm using Multiple Precision Arithmetic Library
Public Key Cryptography is also known as Asymmetric Cryptography is the type of cryptography which involves the usage of two keys namely Public Key and Private Key. The Public Key of the receiver is used to encrypt the plaintext by the sender while the Private Key of the receiver is used to decrypt the encrypted message and hence can be decrypted o
4 min read
RC4 Encryption Algorithm
RC4 is a stream cipher and variable-length key algorithm. This algorithm encrypts one byte at a time (or larger units at a time). A key input is a pseudorandom bit generator that produces a stream 8-bit number that is unpredictable without knowledge of input key, The output of the generator is called key-stream, is combined one byte at a time with
5 min read
Back-off Algorithm for CSMA/CD
Prerequisite - Basics of CSMA/ CD, Collision Detection in CSMA/CD Back-off algorithm is a collision resolution mechanism which is used in random access MAC protocols (CSMA/CD). This algorithm is generally used in Ethernet to schedule re-transmissions after collisions. If a collision takes place between 2 stations, they may restart transmission as s
7 min read
Algorithm for Dynamic Time out timer Calculation
Prerequisite – Computer Network | TCP Timers Calculating Time out timer (TOT) at transport layer is tricky as propagation delay is not constant i.e. path may change continuously and traffic is dynamic. So, static TOT cannot be used at TCP. And unnecessarily retransmitting the same data packet multiple times may cause congestion. Solution for this i
3 min read
Cristian's Algorithm
Cristian's Algorithm is a clock synchronization algorithm is used to synchronize time with a time server by client processes. This algorithm works well with low-latency networks where Round Trip Time is short as compared to accuracy while redundancy-prone distributed systems/applications do not go hand in hand with this algorithm. Here Round Trip T
8 min read
Berkeley's Algorithm
Berkeley's Algorithm is a clock synchronization technique used in distributed systems. The algorithm assumes that each machine node in the network either doesn't have an accurate time source or doesn't possess a UTC server.Algorithm 1) An individual node is chosen as the master node from a pool node in the network. This node is the main node in the
6 min read
Welsh Powell Graph colouring Algorithm
In graph theory, vertex colouring is a way of labelling each individual vertex such that no two adjacent vertex have same colour. But we need to find out the number of colours we need to satisfy the given condition. It is not desirable to have a large variety of colours or labels. So, We have an algorithm called welsh Powell algorithm that gives th
3 min read
Algorithm for CSMA and Rules for CSMA/CD
Rules for CSMA/CD: If medium is idle, transmit, otherwise go to 2If the medium is busy, continue to listen until the channel is idle, then transmit immediatelyIf collision is detected during transmission then transmit a brief jamming signal to assure that all station knows that there has been a collision so that stations can cease the transmissionA
2 min read
How does Ripple Protocol Consensus Algorithm work ?
Ripple Transaction protocol (RTXTP) is a blockchain protocol that possesses most of the characteristics of the more popular protocols like Bitcoin and Ethereum. But what makes it unique is its streamlining in accordance with the aim of affordable and efficient transfer of funds, globally. The protocol achieves this efficiency, especially in terms o
4 min read
Shamir's Secret Sharing Algorithm | Cryptography
Cryptography is a technique of securing information and communications through the use of codes so that only those person for whom the information is intended can understand it and process it. Thus preventing unauthorized access to information. The prefix “crypt” means “hidden” and suffix graphy means “writing”. In this article, a type of cryptogra
7 min read
Implementation of a Back-off Algorithm for CSMA/CD
Prerequisite - Back-off Algorithm for CSMA/CD Ethernet network may be used to provide shared access by a group of attached nodes to the physical medium which connects the nodes. These nodes are said to form a Collision Domain. When there is data waiting to be sent, each transmitting NIC also monitors its own transmission. If it observes a collision
5 min read
CYK Algorithm for Context Free Grammar
Prerequisite - Converting Context Free Grammar to Chomsky Normal Form CYK algorithm is a parsing algorithm for context free grammar. In order to apply CYK algorithm to a grammar, it must be in Chomsky Normal Form. It uses a dynamic programming algorithm to tell whether a string is in the language of a grammar. Algorithm : Let w be the n length stri
2 min read
ANN - Bidirectional Associative Memory (BAM) Learning Algorithm
Prerequisite: ANN | Bidirectional Associative Memory (BAM) There are three main steps to construct the BAM model. Learning Testing Retrieval Each step has been described with mathematical formulation in the article ANN | Bidirectional Associative Memory (BAM). Here, this learning algorithm is explained iteratively with an example. Assume, Set A: In
4 min read
Perceptron Algorithm for Logic Gate with 3-bit Binary Input
In the field of Machine Learning, the Perceptron is a Supervised Learning Algorithm for binary classifiers. The Perceptron Model implements the following function: [Tex]\[\begin{array}{c}\hat{y}=\Theta\left(w_{1} x_{1}+w_{2} x_{2}+\ldots+w_{n} x_{n}+b\right) \\ =\Theta(\mathbf{w} \cdot \mathbf{x}+b) \\ \text { where } \Theta(v)=\left\{\begin{array}
4 min read
Ethernet Transmitter Algorithm
Prerequisite - Ethernet and Ethernet Frame Format Ethernet Adapter or Network Interface Card (NIC) is hardware device/component, that enables you to send and receive data, to and from your computer. It will be connected to the network via wired or wireless connection. Let us assume we have host computer, which has Ethernet Adapter. The Ethernet cab
6 min read
Toivonen's algorithm in data analytics
In this article, we are going to discuss Toivonen's algorithm in data analytics. Toivonen's algorithm : It uses fickleness in a different way from the simple sampling algorithm. This algorithm, given adequate main memory, will use one pass over a small sample and one full pass over the data. It will neither give false negatives nor positives, but t
3 min read
Computer Graphics - Scan Line Algorithm in 3D (Hidden Surface Removal)
This algorithm is based on the Image-space method and concept of coherence. As its name suggests itself Scan-line algorithm, so it processes one line at a time rather than processing one pixel(a point on raster display) at a time. The algorithm works as follows: Here each point at which the scan- line intersects the polygon surfaces are examined(pr
4 min read
Computer Graphics - Area Subdivision Algorithm in 3D(Hidden Surface Removal)
Mr. John Warnock proposed an area subdivision algorithm, that's why also known as the Warnock algorithm. This algorithm extensively uses the concept of area coherence in computing the visible surface in the scene, which is closer to the viewing plane, area coherence avoids the computation of the visibility detection of the common surface, that has
3 min read
Preparata Algorithm
Preparata's algorithm is a recursive Divide and Conquer Algorithm where the rank of each input key is computed and the keys are outputted according to their ranks. C/C++ Code for 1 <= i, j <= n in parallel; for r : = 1 to logn do { Step 1. In parallel set q[i, j, k] := m[i, j] + m[j, k] for 1 <= i, j, k <= n. Step 2. In parallel set m[i
14 min read
Slow Start Restart Algorithm For Congestion Control
Once a connection is established between client and server, the sender sends the data to the receiver in form of packets. If the network is congested then packets get dropped. This happens when the buffer of intermediaries devices such as Router is filled. TCP sender has to detect whether it has filled up ‘buffers at the intermediate devices’, and
5 min read
Performance Metrics For Mutual Exclusion Algorithm
Mutual exclusion is a program object that refers to the requirement of satisfying that no two concurrent processes are in a critical section at the same time. It is presented to intercept the race condition. If a current process is accessing the critical section then it prevents entering another concurrent process there. So, in a single word, just
4 min read
Proportional Rate Reduction - TCP Loss Recovery Algorithm
TCP CUBIC is becoming the default in Linux Kernel and the Rate Halving technique for congestion avoidance was becoming the default in Linux Kernel. So, there was a mismatch between these two. Because Rate Halving was good for TCP Reno, Tahoe, and NewReno. It is good for those TCPs only which reduce the cwnd by half when packet loss occurs. But TCP
9 min read
Impact of Three-way Handshake and Slow Start Algorithm
Internet became something without which our lives are incomplete. Every day each internet user makes 100s of queries on the internet, be it a simple google search or visiting any website and no one likes the delay in the response. In this article, we will discuss time delay that affects internet speed. A three-way handshake was introduced in the in
8 min read
AIMD Algorithm
AIMD is a feedback control algorithm, it is used by TCP protocols in the transport layer of the TCP/IP stack. The AI phase is responsible for the linear growth of the sender's congestion window where there is no congestion detected. The MD phase is responsible for the exponential cut-down of the sender's congestion window when congestion is detecte
6 min read
CAST Algorithm in Cryptography
CAST is a general procedure for creating a family of block ciphers. Individual ciphers have names like CAST-128 and CAST-256. The article focuses on discussing the CAST algorithm in cryptography. What is CAST Algorithm?The CAST algorithm is generally used in IP security and follows the Fiestal structure, as the Fiestal structure divides the plain t
4 min read
Karn's Algorithm For Optimizing TCP
Karn's approach solves the issue of obtaining accurate round-trip time estimates for messages while utilizing the Transmission Control Protocol (TCP) in computer networking. Phil Karn and Craig Partridge proposed the technique, commonly known as the Karn-Partridge algorithm, in 1987. Because of the ambiguity produced by retransmitted segments, accu
3 min read