use of java.security.spec.PKCS8EncodedKeySpec in project robovm by robovm.
the class KeyFactory method engineGeneratePrivate.
protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
if (keySpec instanceof PKCS8EncodedKeySpec) {
try {
PrivateKeyInfo info = PrivateKeyInfo.getInstance(((PKCS8EncodedKeySpec) keySpec).getEncoded());
PrivateKey key = BouncyCastleProvider.getPrivateKey(info);
if (key != null) {
return key;
}
throw new InvalidKeySpecException("no factory found for OID: " + info.getPrivateKeyAlgorithm().getAlgorithm());
} catch (Exception e) {
throw new InvalidKeySpecException(e.toString());
}
}
throw new InvalidKeySpecException("Unknown KeySpec type: " + keySpec.getClass().getName());
}
use of java.security.spec.PKCS8EncodedKeySpec in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKeyProvider01.
/**
* Encrypted data contains valid PKCS8 key info encoding
*/
public final void test_ROUNDTRIP_GetKeySpecKeyProvider01() {
boolean performed = false;
for (int i = 0; i < algName.length; i++) {
for (int l = 0; l < provider.length; l++) {
if (provider[l] == null) {
continue;
}
TestDataGenerator g;
try {
// generate test data
g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfo, provider[l]);
} catch (TestDataGenerator.AllowedFailure allowedFailure) {
continue;
}
try {
// create test object
EncryptedPrivateKeyInfo epki;
if (g.ap() == null) {
epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
} else {
epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
}
try {
PKCS8EncodedKeySpec eks = epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(), provider[l]);
if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
fail(algName[i][0] + " != " + algName[i][1]);
}
} catch (InvalidKeyException e) {
fail(algName[i][0] + ", " + algName[i][1] + ": " + e);
}
performed = true;
} catch (NoSuchAlgorithmException allowedFailure) {
}
}
}
assertTrue("Test not performed", performed);
}
use of java.security.spec.PKCS8EncodedKeySpec in project robovm by robovm.
the class PKCS8EncodedKeySpecTest method testGetEncoded.
/**
* Test for <code>getEncoded()</code> method<br>
* Assertion: returns encoded key
*/
public final void testGetEncoded() {
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKey);
byte[] ek = meks.getEncoded();
assertTrue(Arrays.equals(encodedKey, ek));
}
use of java.security.spec.PKCS8EncodedKeySpec in project robovm by robovm.
the class PKCS8EncodedKeySpecTest method testPKCS8EncodedKeySpec.
//
// Tests
//
/**
* Test for <code>PKCS8EncodedKeySpec</code> constructor<br>
* Assertion: constructs new <code>PKCS8EncodedKeySpec</code>
* object using valid parameter
*/
public final void testPKCS8EncodedKeySpec() {
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
EncodedKeySpec eks = new PKCS8EncodedKeySpec(encodedKey);
assertTrue(eks instanceof PKCS8EncodedKeySpec);
try {
eks = new PKCS8EncodedKeySpec(null);
fail("expected NullPointerException");
} catch (NullPointerException e) {
// ok
}
}
use of java.security.spec.PKCS8EncodedKeySpec in project robovm by robovm.
the class PKCS8EncodedKeySpecTest method testIsStatePreserved1.
/**
* Tests that internal state of the object
* can not be changed by modifying initial
* array value
*/
public final void testIsStatePreserved1() {
// Reference array
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
// Reference array's copy will be used for test
byte[] encodedKeyCopy = encodedKey.clone();
PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKeyCopy);
// Modify initial array's value
encodedKeyCopy[3] = (byte) 5;
// Get encoded key
byte[] ek = meks.getEncoded();
// Check using reference array that
// byte value has not been changed
assertTrue(Arrays.equals(encodedKey, ek));
}
Aggregations