Java SHA-256 | hackerRank Solution

Hello coders, today we are going to solve Java SHA-256 HackerRank Solution.

Java SHA-256

Problem

Cryptographic hash functions are mathematical operations run on digital data; by comparing the computed hash (i.e., the output produced by executing a hashing algorithm) to a known and expected hash value, a person can determine the data’s 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. In addition, cryptographic hash functions are extremely 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.

Secure Hash Algorithm 2 (SHA-2) is a set of cryptographic hash functions designed by the National Security Agency (NSA). 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. Given a string, s, print its SHA-256 hash value.

Input Format

A single alphanumeric string denoting s.

Constraints

  • 6 <= |s| <= 20
  • String s consists of English alphabetic letters (i.e.,[a-z A-Z] and/or decimal digits (i.e.,0 through 9) only.

Output Format

Print the SHA-256 encryption value of s on a new line.

Sample Input 0

 HelloWorld

Sample Output 0

 68e109f0f40ca72a15e05cc22786f8e6

Sample Input 1

 Javarmi123

Sample Output 1

 2da2d1e0ce7b4951a858ed2d547ef485

Solution – Java SHA-256

import java.util.*;
import java.security.*;

public class Solution {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        Scanner input = new Scanner(System.in);
        MessageDigest m = MessageDigest.getInstance("SHA-256");
        m.reset();
        m.update(input.nextLine().getBytes());
        for (byte i : m.digest()) {
            System.out.print(String.format("%02x", i));
        }
        System.out.println();
    }
}

Disclaimer: The above Problem ( Java SHA-256 ) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *