MD5 (Message Digest Algorithm 5)



The MD5 method was among of the first hashing algorithms to gain global recognition as a replacement to the MD4 algorithm. Despite of potential security risks, MD5 is an essential part of data infrastructure in a wide range of situations.

Before diving into MD5 we need to understand what exactly hashing is!

What is Hashing?

Hashing is the process of changing a standard string of information into a complex piece of data. This is done in order to scramble the information so that it entirely changes the original value, which produces a hashed value that is completely different from the original.

Hashing Technique

Hashing transforms regular data into an unreadable form by using a hash function. The original data is transformed into its hashed values by these hash functions, which are a series of mathematical operations that are referred to as the hash digest or digest in general. No matter the size of the input, the digest size for a certain hash function, such as MD5 or SHA1, is always the same.

There are two main use cases for hashing −

Password Verification

To keep passwords secure from attackers, user credentials for websites are frequently stored in a hashed format. Comparing password hashes is far more private because hash functions always return the same result for the same input.

Password Verification

Here is how the full process works −

  • User creates a new password while logging onto the website.
  • It hashes the password and saves the result on the server as a digest.
  • A user must reenter their password each time they attempt to log in.
  • To create a digest, it runs the password entered via the hash algorithm again.
  • The login is validated if the newly created digest matches with the one on the server.

Integrity Verification

Hashing functions can be used to look into some files for data corruption. Hash functions, despite of the iteration parameters, will always produce the same result for similar input, just like in the example above.

Integrity Verification

The steps in the process are as follows −

  • An individual posts a file to the internet.
  • Along with the file, the hash digest is also uploaded.
  • The hash digest is updated as a user gets the file.
  • File integrity is preserved if the digest matches the initial hash value.
  • Having established a basic foundation in hashing, you are able to examine the MD5 algorithm, which is the primary focus of this chapter.

What is MD5 algorithm?

A cryptographic hash algorithm called MD5 (Message Digest Method 5) can be used to create a 128-bit digest from a string of arbitrary length. Hexadecimal numbers of 32 digits are used to represent the digests.

The method was created in 1991 by Ronald Rivest to enable the verification of digital signatures. It was included into several different frameworks in order to improve security measures.

MD5 Algorithm

Because of hashing function suggestions, the digest size is always 128 bits, and even minor changes to the input string result in considerably different digests. This is needed to reduce the chance of hash collisions, or the formation of similar hashes.

Now, you will learn the steps that make up the MD5 algorithm's working.

MD5 Algorithm Steps

The algorithm consists of four main sections −

Padding Bits

Verify that the input string's size is 64 bits less than a multiple of 512 when you receive it. In order to round off the extra characters, you must add zeroes after adding one (1) to the bits of padding.

Padding Bits

Padding Length

The final string needs to include a few more characters in order to be a multiple of 512. To achieve this, take the original input's length and represent it as 64 bits. Once the two are combined, the last string is prepared for hashing.

Padding Length

Initialize MD Buffer

The entire string is divided into several blocks, each having 512 bits. In addition, four buffers (A, B, C, and D) need to be initialised. Each of these 32-bit buffers is initialised as follows −

Initialize MD Buffer

Process Each Block

A 512-bit block can be further divided into 16 sub-blocks, each containing 32 bits. Each of the four operation rounds makes use of all of the buffers, constant array values, and sub-blocks.

You can refer to this constant array as T[1] ⇒ T[64].

The sub-blocks are identified by the notation M[0] ⇒ M[15].

Implementation of MD5

Now we will implement Md5 algorithm with the help of different programming langugages so you will be able to code in any of these langugages.

Using Python

To implement the MD5 algorithm we are going to use hashlib module of Python which provides the support for calculating the hash value of the given message. So the code in Python is as follows −

import hashlib

def generate_md5(input_string):
   md5_hash = hashlib.md5(input_string.encode()).hexdigest()
   return md5_hash

# function execution
input_string = "tutorialspoint"
md5_hash = generate_md5(input_string)
print("MD5 hash for", input_string, ":", md5_hash)

Output

MD5 hash for tutorialspoint : 6c60b3cfe5124f982eb629e00a98f01f

Using Java

Now we will implement the MD5 with the help of Java and its built-in libraries. So we are going to use two classes MessageDigest and NoSuchAlgorithmException. The MessageDigest class lets us to create instances of various message digest algorithms, like MD5, and the NoSuchAlgorithmException is raised if the required cryptographic algorithm is not accessible in the environment. The code for MD5 using Java is as follows −

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Algo {
   public static String generateMD5(String input) {
      try {
         MessageDigest md = MessageDigest.getInstance("MD5");
         byte[] inputBytes = input.getBytes();
         byte[] hashBytes = md.digest(inputBytes);

         // Convert the byte array to a hexadecimal string
         StringBuilder sb = new StringBuilder();
         for (byte b : hashBytes) {
            sb.append(String.format("%02x", b));
         }
         return sb.toString();
      } catch (NoSuchAlgorithmException e) {
         e.printStackTrace();
         return null;
      }
   }

   public static void main(String[] args) {
      String input = "tutorialspoint";
      String md5Hash = generateMD5(input);
      System.out.println("MD5 hash for " + input + " : " + md5Hash);
   }
}

Output

MD5 hash for tutorialspoint : 6c60b3cfe5124f982eb629e00a98f01f

Application Of MD5

Below are some common application where we can use MD5 algorithm −

  • Message digests are used to authenticate and check the integrity of files.
  • Data encryption and security were handled using MD5.
  • It is used for both password verification and message digestion, whatever the message size.
  • It is also used for graphics and game boards.

Advantages of MD5

Here are some advantages of MD5 −

  • Easy to Compare − When it comes to digest verification, a 32-digit digest is comparatively simpler to compare than the most recent hash algorithm families.
  • Keeping Passwords Secure − Hackers and other attackers can access passwords if they are not kept in unencrypted format. The database benefits from digests as well because all hash values have the same size.
  • Low Resource − To combine several services into a single framework without introducing CPU cost, a comparatively small memory footprint is required.
  • Integrity Check − By analysing hash values before and after their transit, you can keep a look for any file corruption. File integrity checks are valid and prevent data damage once the hashes match.

Disadvantages of MD5

  • Sometimes, for different inputs, MD5 creates the similar hash function.
  • SHA1 offers better security than MD5.
  • It has been said that the MD5 algorithm is unsafe. SHA256 is currently being used in place of MD5.
  • Neither symmetric nor asymmetric algorithms apply to MD5.

Is MD5 algorithm secure?

Any message-digest function's goal is to generate digests that look random. Two conditions need to be fulfilled for the hash function to be considered cryptographically secure −

  • An attacker cannot produce a message that matches a given hash value.
  • An attacker cannot possibly produce two messages with the same hash value.

The IETF states that MD5 hashes should not be used for cryptographic authentication since they are no longer regarded as cryptographically secure techniques.

Advertisements