Home » Java SHA

Java SHA

SHA is an abbreviation for Secure Hash Algorithm. In Java SHA and is one of the most prevalent cryptographic hash functions. A cryptographic hash can be used to create a text signature or data file. Now, SHA is nothing but a cryptographic hash function that takes input as much as 20 bytes long and returns a hexadecimal hash value which is approximately 40 digits in length. The SHA-256 algorithm is capable of producing nearly a unique, non-static 256-bit (i.e., 32-byte) hash value. But the point to remember is that this function is only a one-way function, i.e., the result it produces cannot be decrypted back to its original value.

Now, the latest safe hashing standard after SHA-2 is the SHA-3 which in comparison to SHA-2 provides a unique way of making a unique one-way hash, and can be much faster on some hardware implementations. The SHA3-256 algorithm has a fixed length algorithm of 256-bit in SHA-3 just like it was in SHA-256.

In order to implement SHA-256 algorithm Java provides the MessageDigest class.

Message Digest Class

MessageDigest class is used in Java to calculate the value of cryptographic hashing value.
It is provided in the java.security package.

Following are the cryptographic hash functions by MessageDigest Class for obtaining hash value of a text, namely:

  1. SHA-1
  2. MD5
  3. SHA-256

These algorithms are begun in static method called getInstance( ). After selecting an algorithm, digest value is calculated and the results in byte array is returned.

The resulting byte array is converted into its sign-magnitude representation with the help of ‘BigInteger’ class. After that, the representation is converted into hexadecimal format to get the ‘MessageDigest’.

Examples

Input: EncryptingThis

Output: 8a69cbe118a9333180b16ebbcf2430ef69cee5b72c54b10a5bc11d8445cc657a

Input: Just An Example

Output: 284cdc2bb78a70e5661bb5214534593fad996635e7f0554d885c1c27dafac9a2

Let’s implement the SHA-256 algorithm in Java program.

JavaSha.java

Output:

HashCode Generated by SHA - 256 for : TutorAspire : 2a6b5683a73648f11785ace4e8a25ac45ef87d3ef33781b26a03fb9bfaac708a  HashFunc : 5aca749b645d076d05c74d92728155bc3c306f5790bfec019694e45e4fa0bff3  

You may also like