PBKDF2 with SHA256 on android

穿精又带淫゛_ 提交于 2019-12-28 04:04:24

问题


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

How can i succeed to generate a hash using PBKDF2WithHmacSHA256?


回答1:


If you use version 1.47 or higher of SpongyCastle, you can invoke PBKDF2WithHmacSHA256 directly:

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

In versions of BC < 1.47, you could not specify SHA256 digest and it defaulted to SHA1.




回答2:


Bouncy Castle doesn't support PBKDF2WithHmacSHA256 so this won't work. You can try implementing it yourself. Look at the source of PKCS5S2ParametersGenerator.java and replace SHA1Digest with SHA256Digest.



来源:https://stackoverflow.com/questions/11628256/pbkdf2-with-sha256-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!