Java SHA-512 Hash using Apache Commons Codec

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

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

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

String sha512Value = DigestUtils.sha512Hex(inputData);

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

Sha512Example1.java

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

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

        String sha512Value = DigestUtils.sha512Hex(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA-512:" + sha512Value);
    }
}
The output as below.
Input String:Simple Solution
SHA-512:5d745b3f47a5ebed95135a28346680b5ed3a4a49696e8206c3a90fd51c0e8847b47cf2dee5c03bf2c7bc4b7fd5076503f2aaad334be46c79b18b0033f9e68fb4

Generate File SHA-512 Checksum Hash String in Java

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

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

Sha512Example2.java

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

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

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

        InputStream inputStream = new FileInputStream(filePath);
        String sha512Value = DigestUtils.sha512Hex(inputStream);

        System.out.println("SHA-512:" + sha512Value);
    }
}
The output as below.
SHA-512:3566ac0c8dcd885f050cf738fecaa0232cce2b26305b24156e2ff13feb5633fbd6b93545b90ae6329bd425c7c06a2b5a35f9394172f787c6e3e110cc6d636226

Generate SHA-512 Hash as Bytes Array

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

byte[] sha512Value = DigestUtils.sha512(inputData);

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

Sha512Example3.java

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

import java.util.Arrays;

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

        byte[] sha512Value = DigestUtils.sha512(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA-512 as bytes:" + Arrays.toString(sha512Value));
    }
}
The output as below.
Input String:Simple Solution
SHA-512 as bytes:[93, 116, 91, 63, 71, -91, -21, -19, -107, 19, 90, 40, 52, 102, -128, -75, -19, 58, 74, 73, 105, 110, -126, 6, -61, -87, 15, -43, 28, 14, -120, 71, -76, 124, -14, -34, -27, -64, 59, -14, -57, -68, 75, 127, -43, 7, 101, 3, -14, -86, -83, 51, 75, -28, 108, 121, -79, -117, 0, 51, -7, -26, -113, -76]

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

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

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

        MessageDigest sha512MessageDigest = DigestUtils.getSha512Digest();
        byte[] hashedBytes = sha512MessageDigest.digest(inputData.getBytes(StandardCharsets.UTF_8));
        String sha512Value = Hex.encodeHexString(hashedBytes);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA-512:" + sha512Value);
    }
}
The output as below.
Input String:Simple Solution
SHA-512:5d745b3f47a5ebed95135a28346680b5ed3a4a49696e8206c3a90fd51c0e8847b47cf2dee5c03bf2c7bc4b7fd5076503f2aaad334be46c79b18b0033f9e68fb4

Happy Coding 😊

Java SHA-256 Hash using Apache Commons Codec

Java SHA-384 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 Generate SHA-1 using DigestUtils in Apache Commons Codec

Java MD2 Hash using Apache Commons Codec

Java MD5 Hash using Apache Commons Codec

Java Apache Commons Codec Tutorial