Hello Guys,
In Cryptography, SHA is cryptographic hash function which takes input as 20 Bytes and rendered the hash value in hexadecimal number, 40 digits long approx.
Message Digest Class :
- To calculate cryptographic hashing value in Java, MessageDigest Class is used, under the package java.security.
- MessageDigest Class provides following cryptographic hash function to find hash value of a text, they are:
MD5
SHA-1
SHA-256
This Algorithms are initialized in static method called getInstance(). After selecting the algorithm it calculate the digest value and return the results in byte array.
BigInteger Class :
- BigInteger is used, which converts the resultant byte array into its sign-magnitude representation. This representation is converted into hex format to get the MessageDigest.
SHA-256 PROGRAM
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class SHA256
{
public static String getSHA256(String s)
{
try{
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte [] messageDigest = md.digest(s.getBytes());
BigInteger no = new BigInteger(1,messageDigest);
String hash = no.toString(16);
while(hash.length() < 32)
{
hash = "0" + hash;
}
return hash;
}
catch(NoSuchAlgorithmException e)
{
throw new RuntimeException(e);
}
}
public static void main(String [] args)
{
System.out.print("Enter the word :");
Scanner scan = new Scanner(System.in);
String s = scan.next();
System.out.println(s+ " : "+getSHA256(s));
}
}
Output :
Enter the word :Hello
Hello : 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
Enter the word :infopsh
infopsh : f3d96df7c3b21c554b5be9198b6ad27a2f49c25ad6c5709d30faef27c54d8044
If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support. 🙏
1 Comments
Good work bhai 🙂
ReplyDelete