· 9 years ago · Jan 18, 2017, 12:34 PM
1public static SecretKey generateKey(char[] passphraseOrPin, byte[] salt)
2 throws NoSuchAlgorithmException, InvalidKeySpecException {
3 // Number of PBKDF2 hardening rounds to use. Larger values increase
4 // computation time. You should select a value that causes computation
5 // to take >100ms.
6 final int iterations = 1000;
7
8 // Generate a 256-bit key
9 final int outputKeyLength = 256;
10
11 SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
12 KeySpec keySpec = new PBEKeySpec(passphraseOrPin, salt, iterations, outputKeyLength);
13 SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
14 return secretKey;
15}