Java SHA-384 Hash using Apache Commons Codec

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

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

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

String sha384Value = DigestUtils.sha384Hex(inputData);

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

Sha384Example1.java

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

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

        String sha384Value = DigestUtils.sha384Hex(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA-384:" + sha384Value);
    }
}
The output as below.
Input String:Simple Solution
SHA-384:7a6c3c8b15aff732b1b45d93ab22e3c09e4eb22a3a93e2f9dc34a30a57fbcc5e1f65b74307a8c462478736905cca16ff

Generate File SHA-384 Checksum Hash String in Java

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

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

Sha384Example2.java

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

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

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

        InputStream inputStream = new FileInputStream(filePath);
        String sha384Value = DigestUtils.sha384Hex(inputStream);

        System.out.println("SHA-384:" + sha384Value);
    }
}
The output as below.
SHA-384:0a92c2d7c581670ee07f79dffdfbaa660d51cab0ae84199db8cf4bbfd92602561270b78c99fed7bb1072829da543c7bd

Generate SHA-384 Hash as Bytes Array

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

byte[] sha384Value = DigestUtils.sha384(inputData);

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

Sha384Example3.java

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

import java.util.Arrays;

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

        byte[] sha384Value = DigestUtils.sha384(inputData);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA-384 as bytes:" + Arrays.toString(sha384Value));
    }
}
The output as below.
Input String:Simple Solution
SHA-384 as bytes:[122, 108, 60, -117, 21, -81, -9, 50, -79, -76, 93, -109, -85, 34, -29, -64, -98, 78, -78, 42, 58, -109, -30, -7, -36, 52, -93, 10, 87, -5, -52, 94, 31, 101, -73, 67, 7, -88, -60, 98, 71, -121, 54, -112, 92, -54, 22, -1]

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

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

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

        MessageDigest sha384MessageDigest = DigestUtils.getSha384Digest();
        byte[] hashedBytes = sha384MessageDigest.digest(inputData.getBytes(StandardCharsets.UTF_8));
        String sha384Value = Hex.encodeHexString(hashedBytes);

        System.out.println("Input String:" + inputData);
        System.out.println("SHA-384:" + sha384Value);
    }
}
The output as below.
Input String:Simple Solution
SHA-384:7a6c3c8b15aff732b1b45d93ab22e3c09e4eb22a3a93e2f9dc34a30a57fbcc5e1f65b74307a8c462478736905cca16ff

Happy Coding 😊

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