×

Java SHA256

Definition: In cryptography, SHA is a hash function that takes 20 bytes of input and produces an approximate 40-digit hexadecimal integer as the hash result.

Class for Message Digest:

Java's MessageDigest Class, which is part of the java. security package, is used to determine cryptographic hashing values.

To determine the hash value of a text, the MessagDigest Class includes the following cryptographic hash functions:

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

The static function getInstance() initialises this algorithm. It calculates the digest value after choosing the algorithm and returns the results in a byte array.

The generated byte array is transformed itself into the sign-magnitude representation using the BigInteger class. To obtain the MessageDigest, this representation is translated to hex format.

Examples:

Input : JavaTpoint

Output : f9142e5ca706378c1c7f9daf6782dcff8197ef1ecfd4075b63dae2f40186afa6

Input : Deekshitha

Output : b9a7b716a303aaf0d9aca444ed2952cab3a0b0b43d280569d125b2f2f9c65a2f

Input : heanchek

Output: dab257e7e013412044748a4279ba69a7df04205c81a33247de9681fb2becac62

Java Program

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
// SHA hash value calculation in Java
 
class JavaSha256expl {
    public static byte[] getSHA(String input) throws NoSuchAlgorithmException
    {
        // With SHA hashing, the static getInstance function is called.
        MessageDigest m = MessageDigest.getInstance("SHA-256");
 
        // Message digest of such an 
// input is calculated using the 
// digest() method, which returns 
// an array of bytes.
        return m.digest(input.getBytes(StandardCharsets.UTF_8));
    }
     
    public static String toHexString(byte[] hash)
    {
        // Convert byte array into signum representation
        BigInteger n = new BigInteger(1, hash);
 
        // message digest -> hex value conversion
        StringBuilder hexString = new StringBuilder(n.toString(16));
 
        // Add leading zeros to the pad
        while (hexString.length() < 64)
        {
            hexString.insert(0, '0');
        }
 
        return hexString.toString();
    }
 
    // Driver code
    public static void main(String args[])
    {
        try
     { System.out.println(" SHA-256 generated a hash code for: ");
String s0 = "JavaTpoint";
            System.out.println("\n" + s0 + " : " + toHexString(getSHA(s0)));
 
            String s1 = "Deekshitha";
            System.out.println("\n" + s1 + " : " + toHexString(getSHA(s1)));
             
            String s2 = "heanchek"; 
            System.out.println("\n" + s2 + " : " + toHexString(getSHA(s2)));
        }  // due to incorrectly spec'ing message digest algorithms
        catch (NoSuchAlgorithmException e) {
            System.out.println(" An error in the algorithm resulted in an exception: " + e);
        }
    }
}

Output:

Java SHA256

Application:

Data Integrity and Cryptography are the areas where JavaSha256 is majorly used.


Related Topics

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

Check whether a Number is a Power of 4 or Not in Java

There are many ways to figure out if an integer is a power of 4. This section will go over a variety of techniques for figuring out whether or not...

11 minutes read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Finding Odd Occurrence of a Number in Java

In this tutorial, we will learn how to find the odd occurrence of a number through a java program. Let us consider an array of non-negative integers. Here, except for one...

6 minutes read.