Skip to content

Instantly share code, notes, and snippets.

@d0nutptr
Last active August 1, 2017 00:48
Show Gist options
  • Save d0nutptr/2ced03fb9f64e1dff2381e12f34218af to your computer and use it in GitHub Desktop.
Save d0nutptr/2ced03fb9f64e1dff2381e12f34218af to your computer and use it in GitHub Desktop.
public class KeyGenerationExample {
public generateSecretKey(String password, byte[] salt) {
//get the PBKDF2 HMAC SHA1 secret key factory
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
//similar to WPA2 key generation
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 4096, 32);
//generate the key
return secretKeyFactory.generateSecret(keySpec);
}
public byte[] generateSalt() {
byte[] salt = new byte[32];
(new SecureRandom()).nextBytes(salt);
return salt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment