Search in sources :

Example 61 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project j2objc by google.

the class AlgorithmParametersTest method test_getInstanceLjava_lang_StringLjava_lang_String.

/**
     * java.security.AlgorithmParameters#getInstance(String, String)
     */
public void test_getInstanceLjava_lang_StringLjava_lang_String() {
    String[] alg = { "", "qwertyu", "!@#$%^&*()" };
    String[] prv = { "", null };
    String[] prv1 = { "1234567890", "qwertyu", "!@#$%^&*()" };
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", "MyProvider");
        checkUnititialized(ap);
        ap.init(new byte[6]);
        checkAP(ap, p);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
    for (int i = 0; i < alg.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance(alg[i], "MyProvider");
            fail("NoSuchAlgorithmException was not thrown for parameter " + alg[i]);
        } catch (NoSuchAlgorithmException nsae) {
        //expected
        } catch (Exception e) {
            fail("Incorrect exception " + e + " was thrown for " + alg[i]);
        }
    }
    for (int i = 0; i < prv.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", prv[i]);
            fail("IllegalArgumentException was not thrown for parameter " + prv[i]);
        } catch (IllegalArgumentException iae) {
        //expected
        } catch (Exception e) {
            fail("Incorrect exception " + e + " was thrown for " + prv[i]);
        }
    }
    for (int i = 0; i < prv1.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", prv1[i]);
            fail("NoSuchProviderException was not thrown for parameter " + prv1[i]);
        } catch (NoSuchProviderException nspe) {
        //expected
        } catch (Exception e) {
            fail("Incorrect exception " + e + " was thrown for " + prv1[i]);
        }
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 62 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project j2objc by google.

the class KeyFactoryTest method testTranslateKey.

@SuppressWarnings("unchecked")
public void testTranslateKey() {
    KeyFactory factory = null;
    try {
        factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
    assertNotNull(factory);
    {
        Key[] keys = { new TestPrivateKey(), new TestPublicKey() };
        Class[] translated = { TestPublicKey.class, TestPrivateKey.class };
        for (int i = 0; i < keys.length; i++) {
            Key key = keys[i];
            Class translate = translated[i];
            try {
                Key translateKey = factory.translateKey(key);
                assertNotNull(translateKey);
                assertEquals(translate, translateKey.getClass());
            } catch (InvalidKeyException e) {
                fail("unexpected exception: " + e);
            }
        }
    }
    {
        Key[] keys = { new AnotherKey(), null };
        Class[] exceptions = { InvalidKeyException.class, NullPointerException.class };
        for (int i = 0; i < keys.length; i++) {
            Key key = keys[i];
            String message = "translateKey(" + (key == null ? "null" : key.toString()) + ")";
            exceptionThrown = false;
            try {
                factory.translateKey(key);
            } catch (Exception e) {
                checkException(message, e, exceptions[i]);
            } finally {
                checkException(message, null, exceptions[i]);
            }
        }
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) KeyFactory(java.security.KeyFactory) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 63 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project j2objc by google.

the class AlgorithmParametersTest method test_getInstanceLjava_lang_String.

/**
     * java.security.AlgorithmParameters#getInstance(String)
     */
public void test_getInstanceLjava_lang_String() {
    String[] str = { "", "qwertyu", "!@#$%^&*()" };
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC");
        checkUnititialized(ap);
        ap.init(new MyAlgorithmParameterSpec());
        checkAP(ap, p);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
    for (int i = 0; i < str.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance(str[i]);
            fail("NoSuchAlgorithmException was not thrown for parameter " + str[i]);
        } catch (NoSuchAlgorithmException nsae) {
        //expected
        }
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 64 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project j2objc by google.

the class SecureRandom2Test method testGetProvider.

public void testGetProvider() {
    SecureRandom sr1 = new SecureRandom();
    assertNotNull(sr1.getProvider());
    SecureRandom sr2 = new SecureRandom(SEED_BYTES);
    assertNotNull(sr2.getProvider());
    MyProvider p = new MyProvider();
    MySecureRandom sr3 = new MySecureRandom(new MySecureRandomSpi(), p);
    assertEquals(p, sr3.getProvider());
    try {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        assertNotNull(random.getProvider());
    } catch (NoSuchAlgorithmException e) {
        fail("Unexpected NoSuchAlgorithmException");
    }
}
Also used : SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 65 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project keywhiz by square.

the class Hkdf method initMac.

private Mac initMac(SecretKey key) {
    Mac mac;
    try {
        mac = Mac.getInstance(hash.getAlgorithm(), provider);
        mac.init(key);
        return mac;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1557 MessageDigest (java.security.MessageDigest)590 IOException (java.io.IOException)374 InvalidKeyException (java.security.InvalidKeyException)266 KeyStoreException (java.security.KeyStoreException)200 CertificateException (java.security.cert.CertificateException)163 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)162 UnsupportedEncodingException (java.io.UnsupportedEncodingException)141 KeyManagementException (java.security.KeyManagementException)130 KeyFactory (java.security.KeyFactory)105 NoSuchProviderException (java.security.NoSuchProviderException)102 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)96 SSLContext (javax.net.ssl.SSLContext)91 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)90 KeyStore (java.security.KeyStore)89 UnrecoverableKeyException (java.security.UnrecoverableKeyException)88 InputStream (java.io.InputStream)82 SecureRandom (java.security.SecureRandom)82 Cipher (javax.crypto.Cipher)79 BadPaddingException (javax.crypto.BadPaddingException)75