Search in sources :

Example 46 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class ExemptionMechanismTest method test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec.

public void test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec() throws Exception {
    Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass);
    ExemptionMechanism em = new ExemptionMechanism(new MyExemptionMechanismSpi(), mProv, defaultAlg) {
    };
    Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]);
    em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
    try {
        em.init(key, (AlgorithmParameterSpec) null);
        fail("InvalidAlgorithmParameterException expected");
    } catch (InvalidAlgorithmParameterException e) {
    //expected
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    key = kg.generateKey();
    try {
        em.init(null, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
    try {
        em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
        fail("ExemptionMechanismException expected");
    } catch (ExemptionMechanismException e) {
    //expected
    }
}
Also used : SpiEngUtils(org.apache.harmony.security.tests.support.SpiEngUtils) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) MyExemptionMechanismSpi(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi) ExemptionMechanism(javax.crypto.ExemptionMechanism) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Provider(java.security.Provider) BigInteger(java.math.BigInteger) KeyGenerator(javax.crypto.KeyGenerator) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Key(java.security.Key)

Example 47 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class myKeyGenerator method testGetInstanceString02.

/*
     * Test for <code> getInstance(String algorithm) </code> method
     * Assertions: returns KeyGenerator object
     */
public void testGetInstanceString02() throws NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGenerator keyG;
    for (int i = 0; i < validValues.length; i++) {
        keyG = KeyGenerator.getInstance(validValues[i]);
        assertEquals("Incorrect algorithm", keyG.getAlgorithm(), validValues[i]);
    }
}
Also used : KeyGenerator(javax.crypto.KeyGenerator)

Example 48 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class myKeyGenerator method testGetInstanceStringProvider03.

/*
     * Test for <code> getInstance(String algorithm, Provider provider)</code> method
     * Assertions: returns KeyGenerator object
     */
public void testGetInstanceStringProvider03() throws IllegalArgumentException, NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGenerator keyA;
    for (int i = 0; i < validValues.length; i++) {
        keyA = KeyGenerator.getInstance(validValues[i], defaultProvider);
        assertEquals("Incorrect algorithm", keyA.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", keyA.getProvider(), defaultProvider);
    }
}
Also used : KeyGenerator(javax.crypto.KeyGenerator)

Example 49 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class myKeyGenerator method testKeyGenerator.

/**
     * Test for <code>KeyGenerator</code> constructor Assertion: returns
     * KeyGenerator object
     */
public void testKeyGenerator() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGeneratorSpi spi = new MyKeyGeneratorSpi();
    KeyGenerator keyG = new myKeyGenerator(spi, defaultProvider, defaultAlgorithm);
    assertEquals("Incorrect algorithm", keyG.getAlgorithm(), defaultAlgorithm);
    assertEquals("Incorrect provider", keyG.getProvider(), defaultProvider);
    AlgorithmParameterSpec params = null;
    int keysize = 0;
    try {
        keyG.init(params, null);
        fail("InvalidAlgorithmParameterException must be thrown");
    } catch (InvalidAlgorithmParameterException e) {
    }
    try {
        keyG.init(keysize, null);
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    keyG = new myKeyGenerator(null, null, null);
    assertNull("Algorithm must be null", keyG.getAlgorithm());
    assertNull("Provider must be null", keyG.getProvider());
    try {
        keyG.init(params, null);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    try {
        keyG.init(keysize, null);
        fail("NullPointerException or InvalidParameterException must be thrown");
    } catch (InvalidParameterException e) {
    } catch (NullPointerException e) {
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) MyKeyGeneratorSpi(org.apache.harmony.crypto.tests.support.MyKeyGeneratorSpi) KeyGeneratorSpi(javax.crypto.KeyGeneratorSpi) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) MyKeyGeneratorSpi(org.apache.harmony.crypto.tests.support.MyKeyGeneratorSpi) KeyGenerator(javax.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 50 with KeyGenerator

use of javax.crypto.KeyGenerator in project robovm by robovm.

the class myKeyGenerator method testGetInstanceStringString03.

/*
     * Test for <code> getInstance(String algorithm, String provider)</code> method
     * Assertions: returns KeyGenerator object
     */
public void testGetInstanceStringString03() throws IllegalArgumentException, NoSuchAlgorithmException, NoSuchProviderException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGenerator keyG;
    for (int i = 0; i < validValues.length; i++) {
        keyG = KeyGenerator.getInstance(validValues[i], defaultProviderName);
        assertEquals("Incorrect algorithm", keyG.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", keyG.getProvider().getName(), defaultProviderName);
    }
}
Also used : KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

KeyGenerator (javax.crypto.KeyGenerator)464 SecretKey (javax.crypto.SecretKey)343 Test (org.junit.Test)106 ArrayList (java.util.ArrayList)104 SecureRandom (java.security.SecureRandom)99 Document (org.w3c.dom.Document)98 InputStream (java.io.InputStream)95 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)93 ByteArrayOutputStream (java.io.ByteArrayOutputStream)87 NodeList (org.w3c.dom.NodeList)82 Cipher (javax.crypto.Cipher)79 ByteArrayInputStream (java.io.ByteArrayInputStream)75 XMLStreamReader (javax.xml.stream.XMLStreamReader)68 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)68 DocumentBuilder (javax.xml.parsers.DocumentBuilder)62 Key (java.security.Key)58 QName (javax.xml.namespace.QName)47 IOException (java.io.IOException)45 SecurePart (org.apache.xml.security.stax.ext.SecurePart)40 SecretKeySpec (javax.crypto.spec.SecretKeySpec)39