Java SHA3-256 Hash using Apache Commons Codec
Tags: Apache Commons Codec Java SHA3-256 Java Hash DigestUtils
In this Java tutorial we learn how to calculate the SHA3-256 hash in Java 9 or higher using the DigestUtils class of Apache Commons Codec library.
Table of contents
- Add Apache Commons Codec Dependency to Java project
- Convert String to SHA3-256 Hash in Java
- Generate File SHA3-256 Checksum Hash String in Java
- Generate SHA3-256 Hash as Bytes Array
- Get Instance of java.security.MessageDigest for SHA3-256 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-256 Hash in Java
The Apache Commons Codec library provides method DigestUtils.sha3_256Hex() can be used to calculate the SHA3-256 hash and return value as a hex string.
String hashedValue = DigestUtils.sha3_256Hex(inputData);
In the following Java program, we show how to use DigestUtils.sha3_256Hex() method to hash an input String to SHA3-256 hash as a hex string.
Sha3256Example1.java
import org.apache.commons.codec.digest.DigestUtils;
public class Sha3256Example1 {
public static void main(String... args) {
String inputData = "Simple Solution";
String hashedValue = DigestUtils.sha3_256Hex(inputData);
System.out.println("Input String:" + inputData);
System.out.println("SHA3-256:" + hashedValue);
}
}
Input String:Simple Solution
SHA3-256:bbf85acf5f39adfffc6693e7a14be8a5e1ea290f19d5d6b5b5c0b485c324644b
Generate File SHA3-256 Checksum Hash String in Java
Apache Commons Codec library provides the DigestUtils.sha3_256Hex() method to generate SHA3-256 hash for an InputStream as below.
String hashedValue = DigestUtils.sha3_256Hex(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_256Hex() to generate SHA3-256 checksum hash for a given file and return SHA3-256 hash value as a hex string.
Sha3256Example2.java
import org.apache.commons.codec.digest.DigestUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Sha3256Example2 {
public static void main(String... args) throws IOException {
String filePath = "D:\\SimpleSolution\\Document.docx";
InputStream inputStream = new FileInputStream(filePath);
String hashedValue = DigestUtils.sha3_256Hex(inputStream);
System.out.println("SHA3-256:" + hashedValue);
}
}
SHA3-256:5b73b187ea43246a6be92aec560be39a476ccd51d2c20a55cde7720e59dfd237
Generate SHA3-256 Hash as Bytes Array
The Apache Commons Codec library also provides DigestUtils.sha3_256() method to calculate SHA3-256 hash and return value as a byte[] array.
byte[] hashedValue = DigestUtils.sha3_256(inputData);
The following Java example program to show you how to use the DigestUtils.sha3_256() method to hash a String to a SHA3-256 hash value as byte[] array.
Sha3256Example3.java
import org.apache.commons.codec.digest.DigestUtils;
import java.util.Arrays;
public class Sha3256Example3 {
public static void main(String... args) {
String inputData = "Simple Solution";
byte[] hashedValue = DigestUtils.sha3_256(inputData);
System.out.println("Input String:" + inputData);
System.out.println("SHA3-256 as bytes:" + Arrays.toString(hashedValue));
}
}
Input String:Simple Solution
SHA3-256 as bytes:[-69, -8, 90, -49, 95, 57, -83, -1, -4, 102, -109, -25, -95, 75, -24, -91, -31, -22, 41, 15, 25, -43, -42, -75, -75, -64, -76, -123, -61, 36, 100, 75]
Get Instance of java.security.MessageDigest for SHA3-256 Hash Algorithm
Using Apache Commons Codec we can use the DigestUtils.getSha3_256Digest() method to return instance of SHA3-256 java.security.MessageDigest and use it to calculate the SHA3-256 hash as following Java program.
Sha3256Example4.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 Sha3256Example4 {
public static void main(String... args) {
String inputData = "Simple Solution";
MessageDigest messageDigest = DigestUtils.getSha3_256Digest();
byte[] hashedBytes = messageDigest.digest(inputData.getBytes(StandardCharsets.UTF_8));
String hashedValue = Hex.encodeHexString(hashedBytes);
System.out.println("Input String:" + inputData);
System.out.println("SHA3-256:" + hashedValue);
}
}
Input String:Simple Solution
SHA3-256:bbf85acf5f39adfffc6693e7a14be8a5e1ea290f19d5d6b5b5c0b485c324644b
Happy Coding 😊
Related Articles
Java SHA3-224 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 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