Wednesday, 17 April 2019

Java SHA-256 HackerRank Solution

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-256SHA-512SHA-224SHA-384SHA-512/224SHA-512/256) with a variable digest size. SHA-256 is a -bit ( byte) hashing algorithm which can calculate a hash code for an input of up to  bits. It undergoes  rounds of hashing and calculates a hash code that is a -digit hexadecimal number.
Given a string, , print its SHA-256 hash value.
Input Format
A single alphanumeric string denoting .
Constraints
  • String  consists of English alphabetic letters (i.e.,  and/or decimal digits (i.e.,  through ) only.
Output Format
Print the SHA-256 encryption value of  on a new line.
Sample Input 0
HelloWorld
Sample Output 0
872e4e50ce9990d8b041330c47c9ddd11bec6b503ae9386a99da8584e9bb12c4
Sample Input 1
Javarmi123
Sample Output 1
f1d5f8d75bb55c777207c251d07d9091dc10fe7d6682db869106aacb4b7df678

Solution:-


import java.io.*;
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();
}
}
Conversation opened. 1 read message. Skip to content Using Gmail with screen readers 1 of 3,270 PopAds.net - Website Approved Inbox x PopAds Notification Attachments 1:16 AM (8 hours ago) to me Dear Mayuresh, Your website 'GoodPractices' has been approved. In order to start generating revenue, you have to install our advertising code on your website. For your convenience, we have attached the adcode in this email. You can also use the Code Generator tool available after logging in for more control over the adcode settings. -- Best regards, PopAds Team Attachments area code.txt Displaying code.txt.

No comments:

Post a Comment

Error While embed the video in Your website page

Error:- Refused to display '<URL>' in a frame because it set 'X-Frame-Options' to 'sameorigin Solution:- if ...