@NotNull private static String generatePBKDF2(@NotNull String pwd, @NotNull String salt, @NotNull String algorithm, int iterations, int keyLength) throws NoSuchAlgorithmException { // for example PBKDF2WithHmacSHA1 SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm); byte[] saltBytes = convertHexToBytes(salt); KeySpec keyspec = new PBEKeySpec(pwd.toCharArray(), saltBytes, iterations, keyLength); try { Key key = factory.generateSecret(keyspec); byte[] bytes = key.getEncoded(); return convertBytesToHex(bytes); } catch (InvalidKeySpecException e) { throw new NoSuchAlgorithmException(algorithm, e); } }
@Nonnull private static String generatePBKDF2(@Nonnull String pwd, @Nonnull String salt, @Nonnull String algorithm, int iterations, int keyLength) throws NoSuchAlgorithmException { // for example PBKDF2WithHmacSHA1 SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm); byte[] saltBytes = convertHexToBytes(salt); KeySpec keyspec = new PBEKeySpec(pwd.toCharArray(), saltBytes, iterations, keyLength); try { Key key = factory.generateSecret(keyspec); byte[] bytes = key.getEncoded(); return convertBytesToHex(bytes); } catch (InvalidKeySpecException e) { throw new NoSuchAlgorithmException(algorithm, e); } }