Java SHA-256 Hash using Apache Commons Codec
Tags: Apache Commons Codec Java SHA-256 Java Hash DigestUtils
In this Java tutorial we learn how to calculate the SHA-256 digest in Java application using the DigestUtils class of Apache Commons Codec library.
Table of contents
- Add Apache Commons Codec Dependency to Java project
- Convert String to SHA-256 Hash in Java
- Generate File SHA-256 Checksum Hash String in Java
- Generate SHA-256 Hash as Bytes Array
- Get Instance of java.security.MessageDigest for SHA-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 SHA-256 Hash in Java
The Apache Commons Codec library provides method DigestUtils.sha256Hex() can be used to calculate the SHA-256 digest and return value as a hex string.
String sha256Value = DigestUtils.sha256Hex(inputData);
In the following Java program, we show how to use DigestUtils.sha256Hex() method to hash an input String to SHA-256 hash as a hex string.
Sha256Example1.java
import org.apache.commons.codec.digest.DigestUtils;
public class Sha256Example1 {
public static void main(String... args) {
String inputData = "Simple Solution";
String sha256Value = DigestUtils.sha256Hex(inputData);
System.out.println("Input String:" + inputData);
System.out.println("SHA-256:" + sha256Value);
}
}
Input String:Simple Solution
SHA-256:a93913858f405f9edc58f24a5202d687a5961122f1de2ac39c57034ab5c00f9e
Generate File SHA-256 Checksum Hash String in Java
Apache Commons Codec library provides the DigestUtils.sha256Hex() method to generate SHA-256 hash for an InputStream as below.
String sha256Value = DigestUtils.sha256Hex(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.sha256Hex() to generate SHA-256 checksum hash for a given file and return SHA-256 hash value as a hex string.
Sha256Example2.java
import org.apache.commons.codec.digest.DigestUtils;
import java.io.FileInputStream;
import java.io.IOException;
public class Sha256Example2 {
public static void main(String... args) throws IOException {
String filePath = "D:\\SimpleSolution\\Document.docx";
String sha256Value = DigestUtils.sha256Hex(new FileInputStream(filePath));
System.out.println("SHA-256:" + sha256Value);
}
}
SHA-256:ec79b54711691ec3f5d493a2df5fa998eca92a83a457fb21eb5da508e7dcdfc6
Generate SHA-256 Hash as Bytes Array
The Apache Commons Codec library also provides DigestUtils.sha256() method to calculate SHA-256 digest and return value as a byte[] array.
byte[] sha256Value = DigestUtils.sha256(inputData);
The following Java example program to show you how to use the DigestUtils.sha256() method to hash a String to a SHA-256 hash value as byte[] array.
Sha256Example3.java
import org.apache.commons.codec.digest.DigestUtils;
import java.util.Arrays;
public class Sha256Example3 {
public static void main(String... args) {
String inputData = "Simple Solution";
byte[] sha256Value = DigestUtils.sha256(inputData);
System.out.println("Input String:" + inputData);
System.out.println("SHA-256 as bytes:" + Arrays.toString(sha256Value));
}
}
Input String:Simple Solution
SHA-256 as bytes:[-87, 57, 19, -123, -113, 64, 95, -98, -36, 88, -14, 74, 82, 2, -42, -121, -91, -106, 17, 34, -15, -34, 42, -61, -100, 87, 3, 74, -75, -64, 15, -98]
Get Instance of java.security.MessageDigest for SHA-256 Hash Algorithm
Using Apache Commons Codec we can use the DigestUtils.getSha256Digest() method to return instance of SHA-256 java.security.MessageDigest and use it to calculate the SHA-256 hash as following Java program.
Sha256Example4.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 Sha256Example4 {
public static void main(String... args) {
String inputData = "Simple Solution";
MessageDigest sha256MessageDigest = DigestUtils.getSha256Digest();
byte[] hashedBytes = sha256MessageDigest.digest(inputData.getBytes(StandardCharsets.UTF_8));
String sha256Value = Hex.encodeHexString(hashedBytes);
System.out.println("Input String:" + inputData);
System.out.println("SHA-256:" + sha256Value);
}
}
Input String:Simple Solution
SHA-256:a93913858f405f9edc58f24a5202d687a5961122f1de2ac39c57034ab5c00f9e
Happy Coding 😊
Related Articles
Java SHA-384 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