generate SHA-512 Salt - Java Security

Java examples for Security:SHA

Description

generate SHA-512 Salt

Demo Code


//package com.java2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(generateSalt());
    }/*from  ww  w. j  ava  2 s. co  m*/

    public static String generateSalt() throws NoSuchAlgorithmException {
        String salt = "";
        byte[] buffer = new byte[512];
        new SecureRandom().nextBytes(buffer);
        MessageDigest md = MessageDigest.getInstance("SHA-512");
        md.update(buffer);
        byte[] digest = md.digest();
        for (int i = 0; i < digest.length; i++) {
            salt += Integer.toString((digest[i] & 0xff) + 0x100, 16)
                    .substring(1).toUpperCase();
        }
        return salt;
    }
}

Related Tutorials