I want to generate key using PBKDF2WithHmacSHA1, but it takes too long to calculate on android. I am using same number of iteration on iOS with common crypto and it takes approximately 6 seconds where as on android it takes 100 seconds.
Here is the code:
public static String generateStorngPasswordHash(String password)
{
try
{
char[] chars = password.toCharArray();
byte[] salt = getSalt();
PBEKeySpec spec = new PBEKeySpec(chars, salt, 1010101, 32 * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = skf.generateSecret(spec).getEncoded();
return toHex(salt) + ":" + toHex(hash);
} catch (Exception e)
{
Logger.e("Exception: Error in generating password" + e.toString());
}
return "";
}
private static byte[] getSalt() throws NoSuchAlgorithmException
{
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[32];
sr.nextBytes(salt);
return salt;
}
Please let me know if there is any problem with this code?
EDIT
One more thing, I am also using sqlCipher in my application. They use openssl for calculating PKDF2 and I have read somewhere that openssl implementation is very faster than java implementation to find PKDF2. So my question is:
- Is this true that openssl can give me better performace on android?
- Can I use openssl implementation used in sqlCipherg?
- If yes how can I use caclulate PKDF2 using openssl?
- If No then how can I use openssl to find PBKDF2WithHmacSHA1 in android. I have
searched over the net but didn't found any example.