Created
January 4, 2016 02:17
-
-
Save didyhu/aacc402f71003abf4b56 to your computer and use it in GitHub Desktop.
Password Salt Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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