Java SHA-512/224 Hash using Apache Commons Codec

Tags: Apache Commons Codec Java SHA-512-224 Java Hash DigestUtils

In this Java tutorial we learn how to calculate the SHA-512 / 224 hash in Java 9 or higher 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 SHA-512 / 224 Hash in Java
  3. Generate File SHA-512 / 224 Checksum Hash String in Java
  4. Generate SHA-512 / 224 Hash as Bytes Array
  5. Get Instance of java.security.MessageDigest for SHA-512 / 224 Hash 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 SHA-512 / 224 Hash in Java

The Apache Commons Codec library provides method DigestUtils.sha512_224Hex() can be used to calculate the SHA-512 / 224 hash and return value as a hex string.

String hashedValue = DigestUtils.sha512_224Hex(inputData);

In the following Java program, we show how to use DigestUtils.sha512_224Hex() method to hash an input String to SHA-512 / 224 hash as a hex string.

Sha512224Example1.java

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

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

        String hashedValue = DigestUtils.sha512_224Hex(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA-512/224:" + hashedValue);
    }
}
The output as below.
Input String:Simple Solution
SHA-512/224:1191fb76df16725b4eee462bbf61bddf43352f1e8ae9b07b7c2fba00

Generate File SHA-512 / 224 Checksum Hash String in Java

Apache Commons Codec library provides the DigestUtils.sha512_224Hex() method to generate SHA-512 / 224 hash for an InputStream as below.

String hashedValue = DigestUtils.sha512_224Hex(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.sha512_224Hex() to generate SHA-512 / 224 checksum hash for a given file and return SHA-512 / 224 hash value as a hex string.

Sha512224Example2.java

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

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

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

        InputStream inputStream = new FileInputStream(filePath);
        String hashedValue = DigestUtils.sha512_224Hex(inputStream);

        System.out.println("SHA-512/224:" + hashedValue);
    }
}
The output as below.
SHA-512/224:ef5bfbb6a7c05faf3f5308c4875f81466460a075ec393809bca6c299

Generate SHA-512 / 224 Hash as Bytes Array

The Apache Commons Codec library also provides DigestUtils.sha512_224() method to calculate SHA-512 / 224 hash and return value as a byte[] array.

byte[] hashedValue = DigestUtils.sha512_224(inputData);

The following Java example program to show you how to use the DigestUtils.sha512_224() method to hash a String to a SHA-512 / 224 hash value as byte[] array.

Sha512224Example3.java

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

import java.util.Arrays;

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

        byte[] hashedValue = DigestUtils.sha512_224(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA-512/224 as bytes:" + Arrays.toString(hashedValue));
    }
}
The output as below.
Input String:Simple Solution
SHA-512/224 as bytes:[17, -111, -5, 118, -33, 22, 114, 91, 78, -18, 70, 43, -65, 97, -67, -33, 67, 53, 47, 30, -118, -23, -80, 123, 124, 47, -70, 0]

Get Instance of java.security.MessageDigest for SHA-512 / 224 Hash Algorithm

Using Apache Commons Codec we can use the DigestUtils.getSha512_224Digest() method to return instance of SHA-512 / 224 java.security.MessageDigest and use it to calculate the SHA-51 2/ 224 hash as following Java program.

Sha512224Example4.java

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

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

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

        MessageDigest messageDigest = DigestUtils.getSha512_224Digest();
        byte[] hashedBytes = messageDigest.digest(inputData.getBytes(StandardCharsets.UTF_8));
        String hashedValue = Hex.encodeHexString(hashedBytes);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA-512/224:" + hashedValue);
    }
}
The output as below.
Input String:Simple Solution
SHA-512/224:1191fb76df16725b4eee462bbf61bddf43352f1e8ae9b07b7c2fba00

Happy Coding 😊

Java SHA-512 / 256 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 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 MD2 Hash using Apache Commons Codec

Java MD5 Hash using Apache Commons Codec

Java Apache Commons Codec Tutorial