Java MD2 Hash using Apache Commons Codec

Tags: Apache Commons Codec Java MD2 Java Hash DigestUtils

In this Java tutorial we learn how to calculate MD2 digest in Java application using the DigestUtils class of Apache Commons Codec library.

Table of contents

  1. Add Apache Commons Codec Dependency to Java project
  2. Convert String to MD2 Hash in Java
  3. Generate File MD2 Checksum in Java
  4. Convert String to MD2 Hash Bytes Array
  5. Get Instance of java.security.MessageDigest for MD2 message digest algorithm

Add Apache Commons Codec Dependency to Java project

To use Apache Commons Codec library in the Gradle build project, add the following dependency into the build.gradle file.

implementation group: 'commons-codec', name: 'commons-codec', version: '1.15'

To use the Apache Commons Codec library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>

To have more information about the Apache Commons Codec Java library you can visit the project home page at commons.apache.org/proper/commons-codec/

Convert String to MD2 Hash in Java

To calculate MD2 hash and return a hex string we can use the DigestUtils.md2Hex() method as below.

String md2Value = DigestUtils.md2Hex(inputData);

In the following Java example program, we show how to calculate MD2 hash with a given String value and return MD2 hash as a hex string.

Md2Example1.java

import org.apache.commons.codec.digest.DigestUtils;

public class Md2Example1 {
    public static void main(String... args) {
        String inputData = "Simple Solution";

        String md2Value = DigestUtils.md2Hex(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("MD2:" + md2Value);
    }
}
The output as below.
Input String:Simple Solution
MD2:f80c207fc8420e4204bcacf5376d2841

Generate File MD2 Checksum in Java

We also can use the DigestUtils.md2Hex() to generate MD2 hash for an InputStream as below.

String md2Value = DigestUtils.md2Hex(inputStream);

For example, we have a document file at D:\SimpleSolution\Document.doc

The following Java program to show you how to use the DigestUtils.md2Hex() to generate MD2 checksum hash for a given file and return MD2 hash value as a hex string.

Md2Example2.java

import org.apache.commons.codec.digest.DigestUtils;

import java.io.FileInputStream;
import java.io.IOException;

public class Md2Example2 {
    public static void main(String... args) throws IOException {
        String filePath = "D:\\SimpleSolution\\Document.docx";

        String md2Value = DigestUtils.md2Hex(new FileInputStream(filePath));

        System.out.println("MD2:" + md2Value);
    }
}
The output as below.
MD2:6df4def2714f05a13d377bb5e345e652

Convert String to MD2 Hash Bytes Array

With Apache Commons Codec we can use DigestUtils.md2() method to calculate MD2 digest and return value as a 16 element byte[] array.

byte[] md2Value = DigestUtils.md2(inputBytes);

The following Java program to show you how to use the DigestUtils.md2() to hash a String to a MD2 hash value as byte[] array.

Md2Example3.java

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Arrays;

public class Md2Example3 {
    public static void main(String... args) {
        String inputData = "Simple Solution";

        byte[] md2Value = DigestUtils.md2(inputData.getBytes());

        System.out.println("Input String:" + inputData);
        System.out.println("MD2 as bytes:" + Arrays.toString(md2Value));
    }
}
The output as below.
Input String:Simple Solution
MD2 as bytes:[-8, 12, 32, 127, -56, 66, 14, 66, 4, -68, -84, -11, 55, 109, 40, 65]

Get Instance of java.security.MessageDigest for MD2 message digest algorithm

Using Apache Commons Codec we can use the DigestUtils.getMd2Digest() method to return instance of MD2 java.security.MessageDigest and use it to calculate MD2 digest as following Java program.

Md2Example4.java

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.security.MessageDigest;

public class Md2Example4 {
    public static void main(String... args) {
        String inputData = "Simple Solution";

        MessageDigest messageDigest = DigestUtils.getMd2Digest();
        byte[] hashedBytes = messageDigest.digest(inputData.getBytes());
        String md2Value = Hex.encodeHexString(hashedBytes);

        System.out.println("Input String:" + inputData);
        System.out.println("MD2:" + md2Value);
    }
}
The output as below.
Input String:Simple Solution
MD2:f80c207fc8420e4204bcacf5376d2841

Happy Coding 😊

Java MD2 Hash using Apache Commons Codec

Java Generate SHA-1 using DigestUtils in Apache Commons Codec

Java SHA-256 Hash using Apache Commons Codec

Java SHA-384 Hash using Apache Commons Codec

Java SHA-512 Hash using Apache Commons Codec

Java SHA3-224 Hash using Apache Commons Codec

Java SHA3-256 Hash using Apache Commons Codec

Java SHA3-384 Hash using Apache Commons Codec

Java SHA3-512 Hash using Apache Commons Codec

Java SHA-512 / 224 Hash using Apache Commons Codec

Java SHA-512 / 256 Hash using Apache Commons Codec

Java Apache Commons Codec Tutorial