Listing All Available Cryptographic Services

The providers of cryptographic services such as key generation algorithms, register their services with the Security class. A service is represented by a name of the form:
service-type.service-implementation
An example of a service entry is SecureRandom.SHA1PRNG.
// This method returns all available services types
public static String[] getServiceTypes() {
    Set result = new HashSet();

    // All all providers
    Provider[] providers = Security.getProviders();
    for (int i=0; i<providers.length; i++) {
        // Get services provided by each provider
        Set keys = providers[i].keySet();
        for (Iterator it=keys.iterator(); it.hasNext(); ) {
            String key = (String)it.next();
            key = key.split(" ")[0];

            if (key.startsWith("Alg.Alias.")) {
                // Strip the alias
                key = key.substring(10);
            }
            int ix = key.indexOf('.');
            result.add(key.substring(0, ix));
        }
    }
    return (String[])result.toArray(new String[result.size()]);
}

// This method returns the available implementations for a service type
public static String[] getCryptoImpls(String serviceType) {
    Set result = new HashSet();

    // All all providers
    Provider[] providers = Security.getProviders();
    for (int i=0; i<providers.length; i++) {
        // Get services provided by each provider
        Set keys = providers[i].keySet();
        for (Iterator it=keys.iterator(); it.hasNext(); ) {
            String key = (String)it.next();
            key = key.split(" ")[0];

            if (key.startsWith(serviceType+".")) {
                result.add(key.substring(serviceType.length()+1));
            } else if (key.startsWith("Alg.Alias."+serviceType+".")) {
                // This is an alias
                result.add(key.substring(serviceType.length()+11));
            }
        }
    }
    return (String[])result.toArray(new String[result.size()]);
}
Here's a sample list of service types:
AlgorithmParameterGenerator
AlgorithmParameters
CertPathBuilder
CertPathValidator
CertStore
CertificateFactory
Cipher
GssApiMechanism
KeyAgreement
KeyFactory
KeyGenerator
KeyManagerFactory
KeyPairGenerator
KeyStore
Mac
MessageDigest
SSLContext
SecretKeyFactory
SecureRandom
Signature
TrustManagerFactory

Comments

18 Jan 2010 - 7:16am by Anonymous (not verified)

super

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.