Java SHA-256 hackerrank Solution

Java SHA-256 hackerrank Solution
Java SHA-256 hackerrank Solution

Java SHA-256 hackerrank Solution

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-256SHA-512SHA-224SHA-384SHA-512/224SHA-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, , print its SHA-256 hash value.

Input Format

A single alphanumeric string denoting s.


  • 6<=|s|<=20
  • String  consists of English alphabetic letters (i.e., [a-zA-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


Sample Output 0


Code Solution:

//Java SHA-256 hackerrank Solution

import java.util.*;

public class Solution {

        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        public static void main(String[] args) {
        Scanner scanner = new Scanner(;
        String key =;
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] digest = md.digest();
        StringBuffer stringbuffer = new StringBuffer();
        for (byte b: digest)
        {  // needed to print it in hexadecimal format
            stringbuffer.append(String.format("%02x", b));
        catch (NoSuchAlgorithmException exception)
//Java SHA-256 hackerrank Solution

Disclaimer: This problem is originally created and published by HackerRank, we only provide solutions to this problem. Hence, doesn’t guarantee the truthfulness of the problem. This is only for information purposes.

Leave a Comment