×

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 Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

Sleeping Barber Problem in Java

The barbershop in this issue has one barber, one barber chair, and N chairs for customers in the waiting area. We may demonstrate the issue by keeping with the original...

6 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Objects and Classes in Java

Objects and Classes in Java Classes and objects are the basic concepts of object-oriented programming. It revolves around the real world entity. In Java, the object is a physical and logical...

5 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

How to Calculate Time Difference Between Two Dates in Java?

Date is being used extensively in Java to calculate date differences. While constructing an application, the date of joining an organisation, admission date, appointment date, and others might be included....

4 minutes read.

How to Convert String to Date in Java

How to Convert String to Date in java You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes....

2 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.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.