use of javax.crypto.interfaces.PBEKey in project platformlayer by platformlayer.
the class PasswordHash method checkPasswordHash.
public static boolean checkPasswordHash(byte[] hashed, String password) {
if (hashed.length < SALT_LENGTH) {
return false;
}
byte[] salt = Arrays.copyOfRange(hashed, 0, SALT_LENGTH);
int iterationCount = 1000;
PBEKey key = KeyDerivationFunctions.doPbkdf2(iterationCount, salt, password, KEY_LENGTH);
byte[] data = key.getEncoded();
if ((data.length + SALT_LENGTH) != hashed.length) {
return false;
}
// Try to prevent timing attacks
int score = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] != hashed[SALT_LENGTH + i]) {
score--;
} else {
score++;
}
}
return score == data.length;
}
use of javax.crypto.interfaces.PBEKey in project platformlayer by platformlayer.
the class SharedSecretTokenService method deriveHmacSha1Key.
static SecretKeySpec deriveHmacSha1Key(String keyData) {
// We want a consistent salt; it can't be empty
byte[] salt = Utf8.getBytes(keyData);
// ??
int keySize = 128;
int iterationCount = 1000;
PBEKey pbeKey = KeyDerivationFunctions.doPbkdf2(iterationCount, salt, keyData, keySize);
return CryptoUtils.buildHmacSha1Key(pbeKey.getEncoded());
}
use of javax.crypto.interfaces.PBEKey in project jdk8u_jdk by JetBrains.
the class MyPBEKey method runTest.
private static void runTest(String alg, byte[] plaintext, char[] password, Provider p) throws Exception {
Cipher cipher = Cipher.getInstance(alg, p);
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
AlgorithmParameters pbeParams = null;
SecretKey key = keyFac.generateSecret(pbeKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
byte[] enc1 = cipher.doFinal(plaintext);
byte[] enc2 = cipher.doFinal(plaintext);
if (Arrays.equals(enc1, enc2) == false) {
throw new Exception("Re-encryption test failed");
}
pbeParams = cipher.getParameters();
cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
byte[] dec = cipher.doFinal(enc1);
if (Arrays.equals(plaintext, dec) == false) {
throw new Exception("decryption test for " + alg + " failed");
}
PBEParameterSpec spec = (PBEParameterSpec) pbeParams.getParameterSpec(PBEParameterSpec.class);
PBEKey key2 = new MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
byte[] dec2 = cipher.doFinal(enc1);
if (Arrays.equals(dec2, dec) == false) {
throw new Exception("Re-decryption test#1 failed");
}
cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
byte[] dec3 = cipher.doFinal(enc1);
if (Arrays.equals(dec3, dec) == false) {
throw new Exception("Re-decryption test#2 failed");
}
System.out.println("passed: " + alg);
}
use of javax.crypto.interfaces.PBEKey in project platformlayer by platformlayer.
the class PasswordHash method doPasswordHash.
public static byte[] doPasswordHash(String password) {
byte[] salt = CryptoUtils.generateSecureRandom(SALT_LENGTH);
int iterationCount = 1000;
PBEKey key = KeyDerivationFunctions.doPbkdf2(iterationCount, salt, password, KEY_LENGTH);
return CryptoUtils.concat(salt, key.getEncoded());
}
use of javax.crypto.interfaces.PBEKey in project platformlayer by platformlayer.
the class AesCbcUtils method deriveKey.
public static SecretKey deriveKey(byte[] salt, String password) {
int iterationCount = 1000;
PBEKey pbeKey = KeyDerivationFunctions.doPbkdf2(iterationCount, salt, password, DEFAULT_KEYSIZE);
SecretKey secretKey = new SecretKeySpec(pbeKey.getEncoded(), ALGORITHM);
return secretKey;
}
Aggregations