Java SHA-256

Secure Hash Algorithm-256:
Most of the cryptographic hash functions runs on the digital data.
Using SHA-256 algorithm we can determine the data integrity.

 For example, computing the hash of a downloaded file and comparing the result to a previously published hash result can show whether the download has been modified or tampered with some other content.

In addition, cryptographic hash functions are extremely collision-resistant.

Cryptographic hash functions:
   It takes input of any length and gives us an output in fixed length.

Collision-resistant ?
In other words, it should be extremely difficult to produce the same hash output from two different input values using a cryptographic hash function.

This is the example to print the SHA-256 value for given string:

import java.io.*;
import java.util.*;
import java.security.*;
import java.math.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        try{
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        System.out.printf("%064x\n", new BigInteger(1, md.digest(s.getBytes())));
        }catch(NoSuchAlgorithmException e){
            //Handle exception
        }
    }
}

Also, It consists of six identical hashing algorithms(i.e., SHA-256, SHA-512, SHA-224, SHA-384, SHA-512/224, SHA-512/256) with a variable digest size.

SHA-256 is a 256-bit ( 32 byte) hashing algorithm which can calculate a hash code for an input of up to 264-1 bits. It undergoes 64 rounds of hashing and calculates a hash code that is a 64-digit hexadecimal number.

Source: https://www.hackerrank.com

No comments:

Post a Comment