use of java.security.spec.KeySpec in project robovm by robovm.
the class KeyFactory2Test method test_getKeySpecLjava_security_KeyLjava_lang_Class.
public void test_getKeySpecLjava_security_KeyLjava_lang_Class() throws Exception {
// java.lang.Class)
for (int i = 0; i < keyfactAlgs.length; i++) {
KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i], providerName);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyfactAlgs[i]);
// We don't use getInstance
SecureRandom random = new SecureRandom();
keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random);
KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
KeyPair keys = keyGen.generateKeyPair();
if (keepalive != null) {
keepalive.interrupt();
}
KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), StandardNames.getPrivateKeySpecClass(keyfactAlgs[i]));
KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(), StandardNames.getPublicKeySpecClass(keyfactAlgs[i]));
PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
PublicKey publicKey = fact.generatePublic(publicKeySpec);
assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
assertEquals("generatePublic generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPublic().getEncoded()), Arrays.toString(publicKey.getEncoded()));
KeySpec encodedSpec = fact.getKeySpec(keys.getPublic(), X509EncodedKeySpec.class);
assertTrue("improper key spec for encoded public key", encodedSpec.getClass().equals(X509EncodedKeySpec.class));
encodedSpec = fact.getKeySpec(keys.getPrivate(), PKCS8EncodedKeySpec.class);
assertTrue("improper key spec for encoded private key", encodedSpec.getClass().equals(PKCS8EncodedKeySpec.class));
}
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class KeyFactorySpiTest method testKeyFactorySpi.
/**
* Test for <code>KeyFactorySpi</code> constructor
* Assertion: constructs KeyFactorySpi
*/
public void testKeyFactorySpi() {
MyKeyFactorySpi keyFSpi = new MyKeyFactorySpi();
assertTrue(keyFSpi instanceof KeyFactorySpi);
KeySpec ks = new MyKeySpec();
KeySpec kss = new MyKeySpec();
try {
keyFSpi.engineGeneratePrivate(ks);
keyFSpi.engineGeneratePublic(ks);
keyFSpi.engineGetKeySpec(null, java.lang.Class.class);
keyFSpi.engineTranslateKey(null);
} catch (Exception e) {
fail("Unexpected exception");
}
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class KeyFactoryTest method testGeneratePublic.
@SuppressWarnings("unchecked")
public void testGeneratePublic() {
KeyFactory factory = null;
try {
factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
}
assertNotNull(factory);
try {
TestPublicKey key = new TestPublicKey();
TestPublicKeySpec keySpec = new TestPublicKeySpec(key);
PublicKey publicKey = factory.generatePublic(keySpec);
assertNotNull(publicKey);
assertTrue(Arrays.equals(key.encoded, publicKey.getEncoded()));
} catch (InvalidKeySpecException e) {
fail("unexpected exception: " + e);
}
KeySpec[] keySpecs = { new TestPrivateKeySpec(new TestPrivateKey()), null, new DSAPublicKeySpec(null, null, null, null) };
Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class };
for (int i = 0; i < keySpecs.length; i++) {
KeySpec keySpec = keySpecs[i];
String message = "generatePublic(" + (keySpec == null ? "null" : keySpec.toString()) + ")";
try {
PublicKey generatePublic = factory.generatePublic(keySpec);
assertNotNull(generatePublic);
} catch (Exception e) {
checkException(message, e, exceptions[i]);
} finally {
checkException(message, null, exceptions[i]);
}
}
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class KeyFactory2Test method test_generatePrivateLjava_security_spec_KeySpec.
public void test_generatePrivateLjava_security_spec_KeySpec() throws Exception {
// java.security.KeyFactory.generatePrivate(java.security.spec.KeySpec)
for (int i = 0; i < keyfactAlgs.length; i++) {
KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i], providerName);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyfactAlgs[i]);
// We don't use
SecureRandom random = new SecureRandom();
// getInstance
keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random);
KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
KeyPair keys = keyGen.generateKeyPair();
if (keepalive != null) {
keepalive.interrupt();
}
KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), StandardNames.getPrivateKeySpecClass(keyfactAlgs[i]));
PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i], Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
privateKey = fact.generatePrivate(new PKCS8EncodedKeySpec(keys.getPrivate().getEncoded()));
assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i], Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
}
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class RSAPublicKeySpecTest method testRSAPublicKeySpec02.
/**
* Test #2 for <code>RSAPublicKeySpec</code> constructor
* Assertion: Constructs <code>RSAPublicKeySpec</code>
* object using valid parameters
*/
public final void testRSAPublicKeySpec02() {
KeySpec ks = new RSAPublicKeySpec(null, null);
assertTrue(ks instanceof RSAPublicKeySpec);
}
Aggregations