Skip to content

Instantly share code, notes, and snippets.

@kimjj81
Created April 30, 2018 09:04
Show Gist options
  • Save kimjj81/ef14290dbfa07ef3a7a50a30bb94241b to your computer and use it in GitHub Desktop.
Save kimjj81/ef14290dbfa07ef3a7a50a30bb94241b to your computer and use it in GitHub Desktop.
PBKDF2withHmacSHA1
public static String makePasswordHash(String text) {
byte[] salt = "yoursalt".getBytes(Charsets.UTF_8);
try {
char[] chars = text.toCharArray();
final int iterations = 10;
// Generate a 256-bit key
final int outputKeyLength = 256;
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA1");
KeySpec keySpec = new PBEKeySpec(chars, salt, iterations, outputKeyLength);
SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
//edit to your needs.
return text + " => " + Base64.encodeToString(salt,Base64.DEFAULT) + " | " + Base64.encodeToString(secretKey.getEncoded(),Base64.DEFAULT);
} catch (Exception e) {
Log.d("PBKDF2","Exception: Error in generating password"
+ Log.getStackTraceString(e));
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment