Search in sources :

Example 6 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project robovm by robovm.

the class AlgorithmParameterGenerator2Test method testGetInstance03.

/**
     * Test for <code>getInstance(String algorithm, Provider provider)</code>
     * method
     * Assertions:
     * throws NullPointerException must be thrown is null
     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
     * throws IllegalArgumentException when provider is null;
     * returns AlgorithmParameterGenerator object
     */
public void testGetInstance03() throws NoSuchAlgorithmException, IllegalArgumentException, InvalidAlgorithmParameterException {
    try {
        AlgorithmParameterGenerator.getInstance(null, mProv);
        fail("NullPointerException or NoSuchAlgorithmException should be thrown");
    } catch (NullPointerException e) {
    } catch (NoSuchAlgorithmException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            AlgorithmParameterGenerator.getInstance(invalidValues[i], mProv);
            fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (NoSuchAlgorithmException e) {
        }
    }
    Provider prov = null;
    for (int i = 0; i < validValues.length; i++) {
        try {
            AlgorithmParameterGenerator.getInstance(validValues[i], prov);
            fail("IllegalArgumentException must be thrown when provider is null (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (IllegalArgumentException e) {
        }
    }
    AlgorithmParameterGenerator apG;
    for (int i = 0; i < validValues.length; i++) {
        apG = AlgorithmParameterGenerator.getInstance(validValues[i], mProv);
        assertEquals("Incorrect algorithm", apG.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", apG.getProvider(), mProv);
        checkResult(apG);
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider)

Example 7 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project robovm by robovm.

the class AlgorithmParameterGenerator2Test method testGetInstance02.

/**
     * Test for <code>getInstance(String algorithm, String provider)</code>
     * method
     * Assertions:
     * throws NullPointerException must be thrown is null
     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
     * throws IllegalArgumentException when provider is null;
     * throws NoSuchProviderException when provider is available;
     * returns AlgorithmParameterGenerator object
     */
public void testGetInstance02() throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException, InvalidAlgorithmParameterException {
    try {
        AlgorithmParameterGenerator.getInstance(null, mProv.getName());
        fail("NullPointerException or NoSuchAlgorithmException should be thrown");
    } catch (NullPointerException e) {
    } catch (NoSuchAlgorithmException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            AlgorithmParameterGenerator.getInstance(invalidValues[i], mProv.getName());
            fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (NoSuchAlgorithmException e) {
        }
    }
    String prov = null;
    for (int i = 0; i < validValues.length; i++) {
        try {
            AlgorithmParameterGenerator.getInstance(validValues[i], prov);
            fail("IllegalArgumentException must be thrown when provider is null (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (IllegalArgumentException e) {
        }
    }
    for (int i = 0; i < validValues.length; i++) {
        for (int j = 1; j < invalidValues.length; j++) {
            try {
                AlgorithmParameterGenerator.getInstance(validValues[i], invalidValues[j]);
                fail("NoSuchProviderException must be thrown (algorithm: ".concat(invalidValues[i]).concat(" provider: ").concat(invalidValues[j]).concat(")"));
            } catch (NoSuchProviderException e) {
            }
        }
    }
    AlgorithmParameterGenerator apG;
    for (int i = 0; i < validValues.length; i++) {
        apG = AlgorithmParameterGenerator.getInstance(validValues[i], mProv.getName());
        assertEquals("Incorrect algorithm", apG.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", apG.getProvider().getName(), mProv.getName());
        checkResult(apG);
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 8 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project robovm by robovm.

the class AlgorithmParameterGeneratorTest method testAlgorithmParameterGenerator.

public void testAlgorithmParameterGenerator() {
    AlgorithmParameterGenerator generator = null;
    try {
        generator = AlgorithmParameterGenerator.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        fail(e.getMessage());
    }
    generator.init(1024);
    AlgorithmParameters parameters = generator.generateParameters();
    assertNotNull("generated parameters are null", parameters);
    helper.test(parameters);
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 9 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project robovm by robovm.

the class OldAlgorithmParameterGeneratorTest method test_initILjava_security_SecureRandom.

public void test_initILjava_security_SecureRandom() throws Exception {
    // Test for method void
    // java.security.AlgorithmParameterGenerator.init(int,
    // java.security.SecureRandom)
    // checks that no exception is thrown
    int[] valid = { 512, 576, 640, 960, 1024 };
    AlgorithmParameterGenerator gen = AlgorithmParameterGenerator.getInstance("DSA");
    for (int i = 0; i < valid.length; i++) {
        try {
            gen.init(valid[i], new SecureRandom());
            gen.init(valid[i], null);
        } catch (Exception e) {
            fail("Exception should not be thrown for valid parameter" + valid[i]);
        }
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) SecureRandom(java.security.SecureRandom)

Example 10 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project robovm by robovm.

the class OldDHTest method testDHGen.

@BrokenTest("Suffers from DH slowness, disabling for now")
public void testDHGen() throws Exception {
    KeyPairGenerator gen = null;
    try {
        gen = KeyPairGenerator.getInstance("DH");
    } catch (NoSuchAlgorithmException e) {
        fail(e.getMessage());
    }
    AlgorithmParameterGenerator algorithmparametergenerator = AlgorithmParameterGenerator.getInstance("DH");
    algorithmparametergenerator.init(1024, new SecureRandom());
    AlgorithmParameters algorithmparameters = algorithmparametergenerator.generateParameters();
    DHParameterSpec dhparameterspec = algorithmparameters.getParameterSpec(DHParameterSpec.class);
    //gen.initialize(1024);
    gen.initialize(dhparameterspec);
    KeyPair key = gen.generateKeyPair();
}
Also used : KeyPair(java.security.KeyPair) AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmParameters(java.security.AlgorithmParameters) BrokenTest(dalvik.annotation.BrokenTest)

Aggregations

AlgorithmParameterGenerator (java.security.AlgorithmParameterGenerator)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 AlgorithmParameters (java.security.AlgorithmParameters)5 SecureRandom (java.security.SecureRandom)4 DHParameterSpec (javax.crypto.spec.DHParameterSpec)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)2 BrokenTest (dalvik.annotation.BrokenTest)1 AlgorithmParameterGeneratorSpi (java.security.AlgorithmParameterGeneratorSpi)1 InvalidKeyException (java.security.InvalidKeyException)1 InvalidParameterException (java.security.InvalidParameterException)1 Key (java.security.Key)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 Provider (java.security.Provider)1 DSAGenParameterSpec (java.security.spec.DSAGenParameterSpec)1 SecretKey (javax.crypto.SecretKey)1 DHGenParameterSpec (javax.crypto.spec.DHGenParameterSpec)1 MyAlgorithmParameterGeneratorSpi (org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi)1