Last active
February 28, 2016 07:55
-
-
Save Quiark/d2e3697020f2ab92eaee to your computer and use it in GitHub Desktop.
Java Crypto Basics: generating key
This file contains hidden or 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
import org.bouncycastle.crypto.PBEParametersGenerator; | |
import org.bouncycastle.crypto.digests.SHA256Digest; | |
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; | |
import org.bouncycastle.crypto.params.KeyParameter; | |
private final int CR_ITERATIONS = 128 * 1000; | |
public byte[] kdfPassword(String pwd, byte[] salt, int outLength) { | |
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest()); | |
gen.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(pwd.toCharArray()), salt, CR_ITERATIONS); | |
byte[] key = ((KeyParameter)gen.generateDerivedParameters(outLength)).getKey(); | |
return key; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment