[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/biblelamp/JavaExercises/master/Experiments/SHA512.java [Back]  [Original]

// Java program to calculate SHA-512 hash value 

import java.math.BigInteger; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 

public class SHA512 { 
    public static String encryptThisString(String input) { 
        try { 
            // getInstance() method is called with algorithm SHA-512 
            MessageDigest md = MessageDigest.getInstance("SHA-512"); 

            // digest() method is called 
            // to calculate message digest of the input string 
            // returned as array of byte 
            byte[] messageDigest = md.digest(input.getBytes()); 

            // Convert byte array into signum representation 
            BigInteger no = new BigInteger(1, messageDigest); 

            // Convert message digest into hex value 
            String hashtext = no.toString(16); 

            // Add preceding 0s to make it 32 bit 
            while (hashtext.length() < 32) { 
                hashtext = "0" + hashtext; 
            } 

            // return the HashText 
            return hashtext; 
        } 

        // For specifying wrong message digest algorithms 
        catch (NoSuchAlgorithmException e) { 
            throw new RuntimeException(e); 
        } 
    } 

    public static void main(String args[]) throws NoSuchAlgorithmException { 

        System.out.println("HashCode Generated by SHA-512 for:"); 

        String s1 = "GeeksForGeeks"; 
        System.out.println("\n" + s1 + ":\n" + encryptThisString(s1)); 

        String s2 = "hello world"; 
        System.out.println("\n" + s2 + ":\n" + encryptThisString(s2)); 
    } 
} 

Web Proxy Viewer  |  New URL  |  Original Page