HackerRank Java- SHA-256




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.

Given a string, s, print its SHA-256 hash value.



  1. import java.util.Scanner;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. public class Solution {
  5. public static void main(String[] args) throws NoSuchAlgorithmException {
  6. Scanner scan = new Scanner(System.in);
  7. String str = scan.next();
  8. scan.close();
  9. /* Encode the String using SHA-256 */
  10. MessageDigest md = MessageDigest.getInstance("SHA-256");
  11. md.update(str.getBytes());
  12. byte[] digest = md.digest();
  13. /* Print the encoded value in hexadecimal */
  14. for (byte b : digest) {
  15. System.out.format("%02x", b);
  16. }
  17. }
  18. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube