Search in sources :

Example 6 with AlgorithmParameters

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

the class AlgorithmParametersTest method test_toString.

/**
     * java.security.AlgorithmParameters#toString()
     */
public void test_toString() throws Exception {
    final String str = "AlgorithmParameters";
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected String engineToString() {
            return str;
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    assertNull("unititialized", params.toString());
    params.init(new byte[0]);
    assertSame(str, params.toString());
}
Also used : AlgorithmParameters(java.security.AlgorithmParameters)

Example 7 with AlgorithmParameters

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

the class AlgorithmParametersTest method test_getProvider.

/**
     * java.security.AlgorithmParameters#getProvider()
     */
public void test_getProvider() throws Exception {
    // test: null value
    AlgorithmParameters ap = new DummyAlgorithmParameters(null, null, "AAA");
    assertNull(ap.getProvider());
    // test: not null value
    ap = new DummyAlgorithmParameters(null, p, "AAA");
    assertSame(p, ap.getProvider());
}
Also used : AlgorithmParameters(java.security.AlgorithmParameters)

Example 8 with AlgorithmParameters

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

the class AlgorithmParametersTest method testDSAProvider.

/**
     * Tests DSA AlgorithmParameters provider
     */
public void testDSAProvider() throws Exception {
    AlgorithmParameters params = AlgorithmParameters.getInstance("DSA");
    assertEquals("Algorithm", "DSA", params.getAlgorithm());
    // init(AlgorithmParameterSpec)
    BigInteger p = BigInteger.ONE;
    BigInteger q = BigInteger.TEN;
    BigInteger g = BigInteger.ZERO;
    params.init(new DSAParameterSpec(p, q, g));
    // getEncoded() and getEncoded(String) (TODO verify returned encoding)
    byte[] enc = params.getEncoded();
    assertNotNull(enc);
    assertNotNull(params.getEncoded("ASN.1"));
    // TODO assertNotNull(params.getEncoded(null)); // HARMONY-2680
    // getParameterSpec(Class)
    DSAParameterSpec spec = params.getParameterSpec(DSAParameterSpec.class);
    assertEquals("p is wrong ", p, spec.getP());
    assertEquals("q is wrong ", q, spec.getQ());
    assertEquals("g is wrong ", g, spec.getG());
    // init(byte[])
    params = AlgorithmParameters.getInstance("DSA");
    params.init(enc);
    assertTrue("param encoded is different", Arrays.equals(enc, params.getEncoded()));
    // init(byte[], String)
    params = AlgorithmParameters.getInstance("DSA");
    params.init(enc, "ASN.1");
    assertTrue("param encoded is different", Arrays.equals(enc, params.getEncoded()));
    params = AlgorithmParameters.getInstance("DSA");
    try {
        params.init(enc, "DOUGLASMAWSON");
        fail("unsupported format should have raised IOException");
    } catch (IOException e) {
    // expected
    }
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) BigInteger(java.math.BigInteger) IOException(java.io.IOException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 9 with AlgorithmParameters

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

the class AlgorithmParametersTest method test_initLjava_security_spec_AlgorithmParameterSpec.

/**
     * java.security.AlgorithmParameters#init(java.security.spec.AlgorithmParameterSpec)
     */
public void test_initLjava_security_spec_AlgorithmParameterSpec() throws Exception {
    //
    // test: corresponding spi method is invoked
    //
    final MyAlgorithmParameterSpec spec = new MyAlgorithmParameterSpec();
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
            assertSame(spec, paramSpec);
            runEngineInit_AlgParamSpec = true;
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(spec);
    assertTrue(paramSpi.runEngineInit_AlgParamSpec);
    //
    try {
        params.init(spec);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new byte[0]);
    try {
        params.init(spec);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new byte[0], "format");
    try {
        params.init(spec);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    //
    // test: if paramSpec is null
    //
    paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
            // null is passed to spi-provider
            assertNull(paramSpec);
            runEngineInit_AlgParamSpec = true;
        }
    };
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init((AlgorithmParameterSpec) null);
    assertTrue(paramSpi.runEngineInit_AlgParamSpec);
}
Also used : InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 10 with AlgorithmParameters

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

the class AlgorithmParametersTest method test_init$B.

/**
     * java.security.AlgorithmParameters#init(byte[])
     */
public void test_init$B() throws Exception {
    //
    // test: corresponding spi method is invoked
    //
    final byte[] enc = new byte[] { 0x02, 0x01, 0x03 };
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(byte[] params) throws IOException {
            runEngineInitB$ = true;
            assertSame(enc, params);
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(enc);
    assertTrue(paramSpi.runEngineInitB$);
    //
    try {
        params.init(enc);
        fail("No expected IOException");
    } catch (IOException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new MyAlgorithmParameterSpec());
    try {
        params.init(enc);
        fail("No expected IOException");
    } catch (IOException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(enc, "format");
    try {
        params.init(enc);
        fail("No expected IOException");
    } catch (IOException e) {
    // expected
    }
    //
    // test: if params is null
    //
    paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(byte[] params) throws IOException {
            runEngineInitB$ = true;
            // null is passed to spi-provider
            assertNull(params);
        }
    };
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init((byte[]) null);
    assertTrue(paramSpi.runEngineInitB$);
}
Also used : IOException(java.io.IOException) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

AlgorithmParameters (java.security.AlgorithmParameters)107 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 IOException (java.io.IOException)31 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)22 Cipher (javax.crypto.Cipher)22 SecretKey (javax.crypto.SecretKey)18 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 NoSuchProviderException (java.security.NoSuchProviderException)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 Key (java.security.Key)11 SecureRandom (java.security.SecureRandom)10 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)10 InvalidKeyException (java.security.InvalidKeyException)8 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)8 UnrecoverableKeyException (java.security.UnrecoverableKeyException)7 KeyPair (java.security.KeyPair)6 KeyPairGenerator (java.security.KeyPairGenerator)6 AlgorithmId (sun.security.x509.AlgorithmId)6 Nullable (android.annotation.Nullable)5 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5