use of java.security.KeyPairGenerator in project robovm by robovm.
the class OldKeyPairGeneratorTestDH method testKeyPairGenerator.
@BrokenTest("Takes ages due to DH computations. Disabling for now.")
public void testKeyPairGenerator() throws NoSuchAlgorithmException {
KeyPairGenerator generator = KeyPairGenerator.getInstance("DH");
generator.initialize(1024);
KeyPair keyPair = generator.generateKeyPair();
assertNotNull("no keypair generated", keyPair);
assertNotNull("no public key generated", keyPair.getPublic());
assertNotNull("no private key generated", keyPair.getPrivate());
new KeyAgreementHelper("DH").test(keyPair);
}
use of java.security.KeyPairGenerator 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.KeyPairGenerator in project robovm by robovm.
the class KeyPairGenerator1Test method testKeyPairGenerator09.
/**
* Test for <code>getInstance(String algorithm, Provider provider)</code>
* method
* Assertion: returns KeyPairGenerator object
*/
public void testKeyPairGenerator09() throws NoSuchAlgorithmException, IllegalArgumentException {
if (!DSASupported) {
fail(NotSupportMsg);
return;
}
KeyPairGenerator kpg;
for (int i = 0; i < algs.length; i++) {
kpg = KeyPairGenerator.getInstance(algs[i], validProvider);
assertEquals("Incorrect algorithm", kpg.getAlgorithm().toUpperCase(), algs[i].toUpperCase());
assertEquals("Incorrect provider", kpg.getProvider(), validProvider);
}
}
use of java.security.KeyPairGenerator in project robovm by robovm.
the class KeyPairGenerator1Test method testKeyPairGenerator13.
/**
* Test for methods: <code>initialize(int keysize)</code>
* <code>initialize(int keysize, SecureRandom random)</code>
* <code>initialize(AlgorithmParameterSpec param)</code>
* <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
* <code>generateKeyPair()</code>
* <code>genKeyPair()</code>
* Assertion: initialize(int ...) throws InvalidParameterException when
* keysize in incorrect Assertion: initialize(AlgorithmParameterSpec
* ...)throws UnsupportedOperationException Assertion: generateKeyPair() and
* genKeyPair() return not null KeyPair Additional class MyKeyPairGenerator2
* is used
*/
public void testKeyPairGenerator13() {
int[] keys = { -1, -250, 1, 63, -512, -1024 };
SecureRandom random = new SecureRandom();
KeyPairGenerator mKPG = new MyKeyPairGenerator2(null);
assertEquals("Algorithm must be null", mKPG.getAlgorithm(), MyKeyPairGenerator2.getResAlgorithm());
assertNull("genKeyPair() must return null", mKPG.genKeyPair());
assertNull("generateKeyPair() mut return null", mKPG.generateKeyPair());
for (int i = 0; i < keys.length; i++) {
try {
mKPG.initialize(keys[i]);
fail("InvalidParameterException must be thrown (key: " + Integer.toString(keys[i]) + ")");
} catch (InvalidParameterException e) {
}
try {
mKPG.initialize(keys[i], random);
fail("InvalidParameterException must be thrown (key: " + Integer.toString(keys[i]) + ")");
} catch (InvalidParameterException e) {
}
}
try {
mKPG.initialize(64);
} catch (InvalidParameterException e) {
fail("Unexpected InvalidParameterException was thrown");
}
try {
mKPG.initialize(64, null);
} catch (InvalidParameterException e) {
fail("Unexpected InvalidParameterException was thrown");
}
try {
mKPG.initialize(null, random);
} catch (UnsupportedOperationException e) {
// on j2se1.4 this exception is not thrown
} catch (InvalidAlgorithmParameterException e) {
fail("Unexpected InvalidAlgorithmParameterException was thrown");
}
}
use of java.security.KeyPairGenerator in project robovm by robovm.
the class KeyPairGenerator1Test method testKeyPairGenerator11.
/**
* Test for methods:
* <code>initialize(int keysize)</code>
* <code>initialize(int keysize, SecureRandom random)</code>
* <code>initialize(AlgorithmParameterSpec param)</code>
* <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
* Assertion: throws InvalidParameterException or
* InvalidAlgorithmParameterException when parameters keysize or param are
* incorrect
*/
public void testKeyPairGenerator11() throws NoSuchAlgorithmException, NoSuchProviderException {
if (!DSASupported) {
fail(NotSupportMsg);
return;
}
int[] keys = { -10000, -1024, -1, 0, 10000 };
KeyPairGenerator[] kpg = createKPGen();
assertNotNull("KeyPairGenerator objects were not created", kpg);
SecureRandom random = new SecureRandom();
AlgorithmParameterSpec aps = null;
for (int i = 0; i < kpg.length; i++) {
for (int j = 0; j < keys.length; j++) {
try {
kpg[i].initialize(keys[j]);
kpg[i].initialize(keys[j], random);
} catch (InvalidParameterException e) {
}
}
try {
kpg[i].initialize(aps);
kpg[i].initialize(aps, random);
} catch (InvalidAlgorithmParameterException e) {
}
}
}
Aggregations