use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.
the class PBKDF2TranslateTest method testMyOwnSecretKey.
/**
* The test case scenario implemented in the method: - derive Key1 for the
* given PBKDF2 algorithm - create my own secret Key2 as an instance of a
* class implements PBEKey - translate Key2 - check if the key value of the
* translated key and Key1 are the same.
*/
private void testMyOwnSecretKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt);
SecretKey key2 = getMyOwnSecretKey(salt);
// Is it actually the same?
if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
throw new RuntimeException("We shouldn't be here. The key1 and key2 values in its" + " primary encoding format have to be the same!");
}
// translate key
SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest);
SecretKey key3 = skf.translateKey(key2);
// Check if it still the same after translation
if (!Arrays.equals(key1.getEncoded(), key3.getEncoded())) {
System.out.println("Key1=" + new String(key1.getEncoded()) + " key3=" + new String(key3.getEncoded()) + " salt=" + new String(salt));
throw new RuntimeException("testMyOwnSecretKey test case failed: the key1 and key3" + " values in its primary encoding format are not" + " the same for " + algoForTest + " algorithm.");
}
}
use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.
the class PBKDF2TranslateTest method getSecretKeyForPBKDF2.
/**
* Generate a PBKDF2 secret key using given algorithm.
*/
private SecretKey getSecretKeyForPBKDF2(String algoDeriveKey, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory skf = SecretKeyFactory.getInstance(algoDeriveKey);
PBEKeySpec spec = new PBEKeySpec(PASS_PHRASE.toCharArray(), salt, ITERATION_COUNT, KEY_SIZE);
return skf.generateSecret(spec);
}
use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.
the class PBKDF2TranslateTest method generateAndTranslateKey.
/**
* The test case scenario implemented in the method: - derive PBKDF2 key
* using the given algorithm; - translate the key - check if the translated
* and original keys have the same key value.
*
*/
public void generateAndTranslateKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
// derive PBKDF2 key
SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt);
// translate key
SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest);
SecretKey key2 = skf.translateKey(key1);
// Check if it still the same after translation
if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
System.out.println("Key1=" + new String(key1.getEncoded()) + " key2=" + new String(key2.getEncoded()) + " salt=" + new String(salt));
throw new RuntimeException("generateAndTranslateKey test case failed: the key1 and" + " key2 values in its primary encoding format are" + " not the same for " + algoForTest + " algorithm.");
}
}
use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.
the class MyPBKDF2SecretKey method generateAndTranslateKey.
/**
* The test case scenario implemented in the method: - derive PBKDF2 key
* using the given algorithm; - translate the key - check if the translated
* and original keys have the same key value.
*
* @return true if the test case passed; false - otherwise.
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws InvalidKeyException
*/
public boolean generateAndTranslateKey() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
// derive PBKDF2 key
SecretKey key1 = getSecretKeyForPBKDF2(algoToTest);
// translate key
SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest);
SecretKey key2 = skf.translateKey(key1);
// check if it still the same after translation
if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
System.err.println("generateAndTranslateKey test case failed: the " + "key1 and key2 values in its primary encoding format are " + "not the same for " + algoToTest + "algorithm.");
return false;
}
return true;
}
use of javax.crypto.SecretKeyFactory in project jdk8u_jdk by JetBrains.
the class MyPBKDF2SecretKey method getSecretKeyForPBKDF2.
/**
* Generate a PBKDF2 secret key using given algorithm.
*
* @param algoToDeriveKey PBKDF2 algorithm
* @return PBKDF2 secret key
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
private SecretKey getSecretKeyForPBKDF2(String algoToDeriveKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToDeriveKey);
PBEKeySpec spec = new PBEKeySpec(PASS_PHRASE.toCharArray(), this.salt, ITERATION_COUNT, KEY_SIZE);
return skf.generateSecret(spec);
}
Aggregations