Listing All Available Certificate Formats - Java Security

Java examples for Security:Certificate

Description

Listing All Available Certificate Formats

Demo Code

import java.security.Provider;
import java.security.Security;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Main {
  public static void main(String[] args) throws Exception {
    String[] names = getCryptoImpls("CertificateFactory");
    System.out.println(Arrays.toString(names));
  }//from w  ww .  j  a  v  a2  s.c om
  public static String[] getCryptoImpls(String serviceType) {
    Set result = new HashSet();
    Provider[] providers = Security.getProviders();
    for (int i = 0; i < providers.length; i++) {
      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()]);
  }

}

Result


Related Tutorials