
The SHA-256 stands for "Secure Hash Algorithm 256-bit" is a cryptographic "hash function" that takes input data and produces a fixed-size, 256-bit (32-byte) hash value.
SHA-256 is widely used for data integrity, digital signatures, and security applications.
Let's take a look at a few examples of how to generate a SHA-256 in Java.
Example:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class JavaSHA256HashExample {
public static void main(String[] args) throws NoSuchAlgorithmException {
String algorithm = "SHA-256";
String inputMessage = "Code2care";
MessageDigest sha256MessageDigest = MessageDigest.getInstance(algorithm);
byte[] hashBytes = sha256MessageDigest.digest(inputMessage.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
hexString.append(String.format("%02x", b));
}
System.out.println("Message:" + inputMessage);
System.out.println("SHA-256 Hash: " + hexString.toString());
}
}
Output:
Message:Code2care
SHA-256 Hash: 96111e521eec4620d6da2006d39891a7c8099dba6861baca73a11037a9bf63bb
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!