SHA Decrypt in Java
In this section, we will be acknowledged about the decryption of SHA in Java.
Java SHA
The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal number after accepting 20 bytes as input.
Secure Hash Algorithm is referred to as SHA. One of the most popular cryptographic hash functions in Java is SHA. Using a cryptographic hash, a text sign or data file can be produced. However, SHA is nothing more than a cryptographic algorithm which accepts input up to 20 bytes long and produces a hash result in hexadecimal notation that is roughly 40 digits long. The SHA-256 technique may generate 256-bit (32-byte) hash values that are almost entirely unique and non-static. But it's important to keep in mind that this function is only one-way, meaning the result it generates cannot be reverse-encrypted to reveal its original value.
After SHA-2, SHA-3 is the most recent safe hashing standard. Compared to SHA-2, SHA-3 offers a novel method for creating a distinctive one-way hash and can be considerably quicker on some embedded applications. Similar to the SHA-256 method, the SHA3-256 approach has a constant length of 256 bits.
Java offers the MessageDigest class to implement the SHA-256 algorithm.
Message Digest Class
Java uses the MessageDigest class to determine the cryptographic hashing value. The java.security package contains it.
The MessageDigest Class's cryptographic hash methods for generating a text's hash value are listed below. They are:
- SHA-1
- MD5
- SHA-256
The static procedure getInstance is where these algorithms are introduced ( ). After choosing an algorithm, the digest value is calculated, and the output is returned in the form of a byte array.
With the aid of the "BigInteger" class, the output byte array is transformed into its sign-magnitude representation. The representation is then transformed to hexadecimal representation to obtain the "MessageDigest" after that.
Examples
Example1
Input: hello word
Output: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
Example2
Input: K1t4fo0V
Output: 0a979e43f4874eb24b740c0157994e34636eed0425688161cc58e8b26b1dcf4e
Example3
Input: Just an Example
Output: 284cdc2bb78a70e5661bb5214534593fad996635e7f0554d885c1c27dafac9a2
Now let us write the program code that decrypts the hash code or hash value for certain string.
File name: SHA.java
// Java programme that determines the SHA hash value
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class SHA {
public static byte[] getSHA(String input) throws NoSuchAlgorithmException
{
// With SHA hashing, the static getInstance function is called.
MessageDigest md = MessageDigest.getInstance("SHA-256");
// Message digest of such an entry is calculated using the digest() method, which returns an //array of bytes.
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash)
{
// byte array to signum representation conversion
BigInteger number = new BigInteger(1, hash);
// message digest -> hex value conversion
StringBuilder hexString = new StringBuilder(number.toString(16));
// Add leading zeros to the pad
while (hexString.length() < 64)
{
hexString.insert(0, '0');
}
return hexString.toString();
}
public static void main(String args[])
{
try
{
System.out.println("HashCode Generated by SHA-256 for:");
String s1 = "Just an Example ";
System.out.println("\n" + s1 + " : " + toHexString(getSHA(s1)));
String s2 = "hello world";
System.out.println("\n" + s2 + " : " + toHexString(getSHA(s2)));
String s3 = "K1t4fo0V";
System.out.println("\n" + s3 + " : " + toHexString(getSHA(s3)));
}
// due to the inappropriate message digest algorithms being specified
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown for incorrect algorithm: " + e);
}
}
}
Output:
SHA-256 generated a hash values for:
Just an Example:
284cdc2bb78a70e5661bb5214534593fad996635e7f0554d885c1c27dafac9a2
hello world: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
K1t4fo0V:
a979e43f4874eb24b740c0157994e34636eed0425688161cc58e8b26b1dcf4e
Advantages
Technology pioneers mostly employ SHA-256 because, unlike some other well-liked hashing algorithms, it has not been "broken" and has no known security flaws.
We use SHA-256 over other popular hashing algorithms because its 256-bit key is significantly more secure. The main advantages of SHA-256, without delving into far to much specific information, are as follows:
- It's an established, reliable industrial standard:
Leading public-sector organisations rely on the industry standard SHA-256, which is also extensively adopted by top technological experts.
- Collisions are very unlikely to occur:
When employing SHA-256, there are 2256 potential hash values, making it nearly unlikely for two distinct documents to accidentally have the same hash value.
- The impact of an avalanche:
Contrary to certain older hashing algorithms, the avalanche effect causes even very small modifications to the original data to drastically alter the hash result.