Skip to content

Instantly share code, notes, and snippets.

@didyhu
Created January 4, 2016 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save didyhu/aacc402f71003abf4b56 to your computer and use it in GitHub Desktop.
Save didyhu/aacc402f71003abf4b56 to your computer and use it in GitHub Desktop.
Password Salt Utils
package net.csdra.meishanshuhuayuan;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
/**
* Created by Didy on 2015-12-22.
*/
public class PasswordUtils {
public static byte[] generateSalt() {
SecureRandom random = new SecureRandom();
byte salt[] = new byte[20];
random.nextBytes(salt);
return salt;
}
public static byte[] encode(byte[] salt, String input) {
try {
PBEKeySpec pbeKeySpec = new PBEKeySpec(input.toCharArray(), salt, 1024, 128);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
return skf.generateSecret(pbeKeySpec).getEncoded();
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment