Java SHA-256

Sort by

recency

|

98 Discussions

|

  • + 1 comment
    import java.io.*;
    import java.util.*;
    import java.security.*;
    import java.nio.charset.*;
    
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String originalString = sc.nextLine();
            
            try {
                MessageDigest digest = MessageDigest.getInstance("SHA-256");
                byte[] encodedhash = digest.digest(
                    originalString.getBytes(StandardCharsets.UTF_8));
                System.out.println(bytesToHex(encodedhash));
            }
            catch (NoSuchAlgorithmException e) {
                System.out.println(e);
            }
        }
        
        private static String bytesToHex(byte[] hash) {
            StringBuilder hexString = new StringBuilder(2 * hash.length);
            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if(hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        }
    }
    
  • + 0 comments

    import java.nio.charset.StandardCharsets; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;

    public class Solution { public static void main(String[] args) { // Create a Scanner to read input from the user Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter the input string
    
        String inputString = scanner.nextLine();
    
        // Compute the SHA-256 hash of the input string
        String hashValue = computeSHA_256Hash(inputString);
    
        // Print the hash value
        System.out.println( hashValue);
    }
    
    // Function to compute the SHA-256 hash of a given string
    public static String computeSHA_256Hash(String input) {
        try {
            // Create a MessageDigest instance for SHA-256
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
    
            // Update the digest with the input bytes
            digest.update(input.getBytes());
    
            // Compute the hash
            byte[] hashBytes = digest.digest();
    
            // Convert the hash bytes to a hexadecimal string
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                hexString.append(String.format("%02x", b));
            }
    
            // Return the hexadecimal string representation of the hash
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            // If the SHA-256 algorithm is not available, print an error message
            System.err.println("SHA-256 algorithm not available");
            e.printStackTrace();
            return null;
        }
    }
    

    }

  • + 0 comments

    The final HackerRank Challenge turned out to be the one where I only had to copy paste, there was no other way, Thank You @RodneyShag

  • + 0 comments

    Complete solution in this website for this question. Java Java SHA-256 - Duplicate Words-hacker-rank-solution

    (https://digitwood.com/java-sha-256-hacker-rank-solution-digit-wood/)

  • + 0 comments

    Complete solution in this website for this question. Java Java SHA-256 - Duplicate Words-hacker-rank-solution

    (https://digitwood.com/java-sha-256-hacker-rank-solution-digit-wood/)