Skip to content

Instantly share code, notes, and snippets.

@Jaymo
Last active November 25, 2021 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jaymo/c2a4b6f29b8c064d4b57 to your computer and use it in GitHub Desktop.
Save Jaymo/c2a4b6f29b8c064d4b57 to your computer and use it in GitHub Desktop.
public static final int PBE_ITERATION_COUNT = 10000; //Am not sure on this size and low end devices. I should test more to prevent lag
private static final String PBE_ALGORITHM = "PBKDF2WithHmacSHA1";
public static final String PROVIDER = "BC";
private static final String SECRET_KEY_ALGORITHM = "AES/GCM/NoPadding;
public static SecretKey getSecretKey(String password, String salt) throws CryptoException {
try {
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), Util.HexEncoder.toByte(salt), PBE_ITERATION_COUNT, 256);
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM, PROVIDER);
SecretKey tmp = factory.generateSecret(pbeKeySpec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), SECRET_KEY_ALGORITHM);
return secret;
} catch (Exception e) {
throw new CryptoException("Unable to get secret key", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment