Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA

前端 未结 3 1524
猫巷女王i
猫巷女王i 2020-12-04 12:52

UPDATED 2019: Bouncycastle now support PBKDF2-HMAC-SHA256 since bouncycastle 1.60


Is there any reliable implementation of PBKDF2-HMAC-SHA256 f

3条回答
  •  天命终不由人
    2020-12-04 13:06

    It is available in Java 8:

    public static byte[] getEncryptedPassword(
                                             String password,
                                             byte[] salt,
                                             int iterations,
                                             int derivedKeyLength
                                             ) throws NoSuchAlgorithmException, InvalidKeySpecException {
        KeySpec spec = new PBEKeySpec(
                                     password.toCharArray(),
                                     salt,
                                     iterations,
                                     derivedKeyLength * 8
                                     );
    
        SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
    
        return f.generateSecret(spec).getEncoded();
    }
    

提交回复
热议问题