PBKDF2 with SHA256 on Android

I want to create a derived password hash using PBKDF2 with SHA256. with this SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1") this work, but it uses SHA1. With SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256") (or SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256","SC") when with spongycastle) I have an error.

How can I generate a hash using PBKDF2WithHmacSHA256?

+4
source share
2 answers

If you are using version 1.47 or higher of SpongyCastle, you can directly call PBKDF2WithHmacSHA256:

 PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(new SHA256Digest()); generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password), salt, iterations); KeyParameter key = (KeyParameter)generator.generateDerivedMacParameters(keySizeInBits); 

In versions BC <1.47, you cannot specify SHA256 digest and by default it is SHA1.

+15
source

Bouncy Castle does not support PBKDF2WithHmacSHA256 , so this will not work. You can try to implement it yourself. Look at the source PKCS5S2ParametersGenerator.java and replace SHA1Digest with SHA256Digest .

+2
source

Source: https://habr.com/ru/post/1241259/


All Articles