Java SHA3-512 Hash using Apache Commons Codec

Tags: Apache Commons Codec Java SHA3-512 Java Hash DigestUtils

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

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

String hashedValue = DigestUtils.sha3_512Hex(inputData);

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

Sha3512Example1.java

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

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

        String hashedValue = DigestUtils.sha3_512Hex(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA3-512:" + hashedValue);
    }
}
The output as below.
Input String:Simple Solution
SHA3-512:69dc824429a15f16ef1934684093f03a5a54b06f623557e823e11b500ab5e4a87fd004fc47fb7bd884c7a9a89f8ef96bdd3e3234aada8a0767a4e701f3beb890

Generate File SHA3-512 Checksum Hash String in Java

Apache Commons Codec library provides the DigestUtils.sha3_512Hex() method to generate SHA3-512 hash for an InputStream as below.

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

Sha3512Example2.java

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

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

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

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

        System.out.println("SHA3-512:" + hashedValue);
    }
}
The output as below.
SHA3-512:8fb0ab50c149ae46e9accccce0cd268c370fcc086a32f4f119c4aef8199c33b11348524a2755b33ffc00c4b619dd1cfda7357b6fd8abb76388136415a8da02bb

Generate SHA3-512 Hash as Bytes Array

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

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

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

Sha3512Example3.java

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

import java.util.Arrays;

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

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

        System.out.println("Input String:" + inputData);
        System.out.println("SHA3-512 as bytes:" + Arrays.toString(hashedValue));
    }
}
The output as below.
Input String:Simple Solution
SHA3-512 as bytes:[105, -36, -126, 68, 41, -95, 95, 22, -17, 25, 52, 104, 64, -109, -16, 58, 90, 84, -80, 111, 98, 53, 87, -24, 35, -31, 27, 80, 10, -75, -28, -88, 127, -48, 4, -4, 71, -5, 123, -40, -124, -57, -87, -88, -97, -114, -7, 107, -35, 62, 50, 52, -86, -38, -118, 7, 103, -92, -25, 1, -13, -66, -72, -112]

Get Instance of java.security.MessageDigest for SHA3-512 Hash Algorithm

Using Apache Commons Codec we can use the DigestUtils.getSha3_512Digest() method to return instance of SHA3-512 java.security.MessageDigest and use it to calculate the SHA3-512 hash as following Java program.

Sha3512Example4.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 Sha3512Example4 {
    public static void main(String... args) {
        String inputData = "Simple Solution";

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

        System.out.println("Input String:" + inputData);
        System.out.println("SHA3-512:" + hashedValue);
    }
}
The output as below.
Input String:Simple Solution
SHA3-512:69dc824429a15f16ef1934684093f03a5a54b06f623557e823e11b500ab5e4a87fd004fc47fb7bd884c7a9a89f8ef96bdd3e3234aada8a0767a4e701f3beb890

Happy Coding 😊

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

Java SHA-512 / 256 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