/** @throws InvalidKeyException if the specified key cannot be used to initialize any provider. */ private static Engine.SpiAndProvider tryAlgorithm(Key key, Provider provider, String algorithm) throws InvalidKeyException { if (provider != null) { Provider.Service service = provider.getService(SERVICE, algorithm); if (service == null) { return null; } return tryAlgorithmWithProvider(null, service); } ArrayList<Provider.Service> services = ENGINE.getServices(algorithm); if (services == null || services.isEmpty()) { return null; } boolean keySupported = false; for (Provider.Service service : services) { if (key == null || service.supportsParameter(key)) { keySupported = true; Engine.SpiAndProvider sap = tryAlgorithmWithProvider(key, service); if (sap != null) { return sap; } } } if (!keySupported) { throw new InvalidKeyException("No provider supports the provided key"); } return null; }
/** * Returns a {@code Set} of all registered algorithms for the specified cryptographic service. * {@code "Signature"}, {@code "Cipher"} and {@code "KeyStore"} are examples for such kind of * services. * * @param serviceName the case-insensitive name of the service. * @return a {@code Set} of all registered algorithms for the specified cryptographic service, or * an empty {@code Set} if {@code serviceName} is {@code null} or if no registered provider * provides the requested service. */ public static Set<String> getAlgorithms(String serviceName) { Set<String> result = new HashSet<String>(); // compatibility with RI if (serviceName == null) { return result; } for (Provider provider : getProviders()) { for (Provider.Service service : provider.getServices()) { if (service.getType().equalsIgnoreCase(serviceName)) { result.add(service.getAlgorithm()); } } } return result; }
private static Engine.SpiAndProvider tryAlgorithmWithProvider(Key key, Provider.Service service) { try { if (key != null && !service.supportsParameter(key)) { return null; } Engine.SpiAndProvider sap = ENGINE.getInstance(service, null); if (sap.spi == null || sap.provider == null) { return null; } if (!(sap.spi instanceof SignatureSpi)) { return null; } return sap; } catch (NoSuchAlgorithmException ignored) { } return null; }
// Access to Security.getAliases() public List<String> getAliases(Provider.Service s) { return s.getAliases(); }