Java SHA-256

Java SHA-256

Hello Friends, How are you? Today I am going to solve the HackerRank Java SHA-256 Problem with a very easy explanation. In this article, you will get more than one approach to solve this problem. So let’s start- {tocify} $title={Table of Contents} 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 bytes) hashing algorithm which can calculate a hash code for the 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. A single alphanumeric string denoting s. String s consists of English alphabetic letters (i.e., [a – zA – Z]  and/or decimal digits (i.e., 0 through 9) only. Print the SHA-256 encryption value of s on a new line.

HelloWorld {codeBox}




Approach I: Java SHA-256 HackerRank Java Solution

// ========================
// Information
// ======================== // Name: Java SHA-256 HackerRank Problem
// Direct Link:
// Difficulty: Medium
// Max Score: 30
// Language: Java 8 // ========================
// Solution Start
// ======================== // Java SHA-256 - Hacker Rank Solution Start import java.util.Scanner;
import; public class Solution { public static void main(String[] args) throws NoSuchAlgorithmException { /* Read and save the input String */ Scanner scan = new Scanner(; String str =; scan.close(); /* Encode the String using SHA-256 */ MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(str.getBytes()); byte[] digest = md.digest(); /* Print the encoded value in hexadecimal */ for (byte b : digest) { System.out.format("%02x", b); } }
} // Java SHA-256 Hacker Rank Solution END
// MyEduWaves
No Comments

Sorry, the comment form is closed at this time.