Utility.java
public static String pbkdf2(String password, String salt, int iterations, int keySize) {
        // convert the username and password to char and byte arrays
        char[] pwd = password.toCharArray();
        byte[] slt = salt.getBytes();

        try {
            // initialise the crypto classes with the desired configuration
            SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
            PBEKeySpec spec = new PBEKeySpec(pwd, slt, iterations, keySize);

            // hash the password using the configuration
            SecretKey key = skf.generateSecret(spec);
            byte[] res = key.getEncoded();

            // return the hashed password as a hexadecimal string
            return toHex(res);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw new RuntimeException(e);
        }
    }
/**
     * Derives a key from a password.
     */
Derives a key from a password.
Derives a key from a password .
public static String pbkdf2(String password, String salt, int iterations, int keySize) {
        char[] pwd = password.toCharArray();
        byte[] slt = salt.getBytes();
        try {
            SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
            PBEKeySpec spec = new PBEKeySpec(pwd, slt, iterations, keySize);
            SecretKey key = skf.generateSecret(spec);
            byte[] res = key.getEncoded();
            return toHex(res);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw new RuntimeException(e);
        }
    }
public static String pbkdf2 ( String password , String salt , int iterations , int keySize ) { char [ ] pwd = password . toCharArray ( ) ; byte [ ] slt = salt . getBytes ( ) ; try { SecretKeyFactory skf = SecretKeyFactory . getInstance ( "PBKDF2WithHmacSHA512" ) ; PBEKeySpec spec = new PBEKeySpec ( pwd , slt , iterations , keySize ) ; SecretKey key = skf . generateSecret ( spec ) ; byte [ ] res = key . getEncoded ( ) ; return toHex ( res ) ; } catch ( NoSuchAlgorithmException | InvalidKeySpecException e ) { throw new RuntimeException ( e ) ; } }
Derives a key from a password.
Derives a key from a password .
// convert the username and password to char and byte arrays // initialise the crypto classes with the desired configuration // hash the password using the configuration // return the hashed password as a hexadecimal string