/** * Finds the appropriate service implementation and returns an * {@code SpiAndProvider} instance containing a reference to SPI * and its {@code Provider} */ public SpiAndProvider getInstance(String algorithm, Object param) throws NoSuchAlgorithmException { if (algorithm == null) { throw new NoSuchAlgorithmException("Null algorithm name"); } int newCacheVersion = Services.getCacheVersion(); Provider.Service service; ServiceCacheEntry cacheEntry = this.serviceCache; if (cacheEntry != null && cacheEntry.algorithm.equalsIgnoreCase(algorithm) && newCacheVersion == cacheEntry.cacheVersion) { service = cacheEntry.service; } else { if (Services.isEmpty()) { throw notFound(serviceName, algorithm); } String name = this.serviceName + "." + algorithm.toUpperCase(Locale.US); service = Services.getService(name); if (service == null) { throw notFound(serviceName, algorithm); } this.serviceCache = new ServiceCacheEntry(algorithm, newCacheVersion, service); } return new SpiAndProvider(service.newInstance(param), service.getProvider()); }
/** * Returns the {@code Provider} with the specified name. Returns {@code * null} if name is {@code null} or no provider with the specified name is * installed. * * @param name * the name of the requested provider. * @return the provider with the specified name, maybe {@code null}. */ public static synchronized Provider getProvider(String name) { return Services.getProvider(name); }
/** * Returns an array containing all installed providers. The providers are * ordered according their preference order. * * @return an array containing all installed providers. */ public static synchronized Provider[] getProviders() { return Services.getProviders(); }
/** * Returns the default SecureRandom service description. */ public static synchronized Provider.Service getSecureRandomService() { getCacheVersion(); // used for side effect of updating cache if needed return cachedSecureRandomService; }
/** * Returns the current cache version. This has the possible side * effect of updating the cache if needed. */ public static synchronized int getCacheVersion() { if (needRefresh) { cacheVersion++; synchronized (services) { services.clear(); } cachedSecureRandomService = null; for (Provider p : providers) { initServiceInfo(p); } needRefresh = false; } return cacheVersion; } }
/** * Constructs a new {@code SecureRandom} that uses the default algorithm. */ public SecureRandom() { super(0); Provider.Service service = Services.getSecureRandomService(); if (service == null) { this.provider = null; this.secureRandomSpi = new SHA1PRNG_SecureRandomImpl(); this.algorithm = "SHA1PRNG"; } else { try { this.provider = service.getProvider(); this.secureRandomSpi = (SecureRandomSpi)service.newInstance(null); this.algorithm = service.getAlgorithm(); } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Insert the given {@code Provider} at the specified {@code position}. The * positions define the preference order in which providers are searched for * requested algorithms. * * @param provider * the provider to insert. * @param position * the position (starting from 1). * @return the actual position or {@code -1} if the given {@code provider} * was already in the list. The actual position may be different * from the desired position. */ public static synchronized int insertProviderAt(Provider provider, int position) { // check that provider is not already // installed, else return -1; if (position <1) or (position > max // position) position = max position + 1; insert provider, shift up // one position for next providers; Note: The position is 1-based if (getProvider(provider.getName()) != null) { return -1; } int result = Services.insertProviderAt(provider, position); renumProviders(); return result; }
/** * Removes the {@code Provider} with the specified name form the collection * of providers. If the the {@code Provider} with the specified name is * removed, all provider at a greater position are shifted down one * position. * * <p>Returns silently if {@code name} is {@code null} or no provider with the * specified name is installed. * * @param name * the name of the provider to remove. */ public static synchronized void removeProvider(String name) { // It is not clear from spec.: // 1. if name is null, should we checkSecurityAccess or not? // throw SecurityException or not? // 2. as 1 but provider is not installed // 3. behavior if name is empty string? Provider p; if ((name == null) || (name.length() == 0)) { return; } p = getProvider(name); if (p == null) { return; } Services.removeProvider(p.getProviderNumber()); renumProviders(); p.setProviderNumber(-1); }
return null; java.util.List<Provider> result = Services.getProvidersList(); Set<Entry<String, String>> keys = filter.entrySet(); Map.Entry<String, String> entry;
private static synchronized void checkCacheVersion() { int newCacheVersion = Services.getCacheVersion(); if (newCacheVersion != cacheVersion) { // // Now search providers for mappings like // Alg.Alias.<service>.<OID-INTS-DOT-SEPARATED>=<alg-name> // or // Alg.Alias.<service>.OID.<OID-INTS-DOT-SEPARATED>=<alg-name> // Provider[] pl = Security.getProviders(); for (Provider element : pl) { selectEntries(element); } cacheVersion = newCacheVersion; } }
/** * Returns the current cache version. This has the possible side * effect of updating the cache if needed. */ public static synchronized int getCacheVersion() { if (needRefresh) { cacheVersion++; synchronized (services) { services.clear(); } cachedSecureRandomService = null; for (Provider p : providers) { initServiceInfo(p); } needRefresh = false; } return cacheVersion; } }
/** * Constructs a new {@code SecureRandom} that uses the default algorithm. */ public SecureRandom() { super(0); Provider.Service service = Services.getSecureRandomService(); if (service == null) { this.provider = null; this.secureRandomSpi = new SHA1PRNG_SecureRandomImpl(); this.algorithm = "SHA1PRNG"; } else { try { this.provider = service.getProvider(); this.secureRandomSpi = (SecureRandomSpi)service.newInstance(null); this.algorithm = service.getAlgorithm(); } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Insert the given {@code Provider} at the specified {@code position}. The * positions define the preference order in which providers are searched for * requested algorithms. * * @param provider * the provider to insert. * @param position * the position (starting from 1). * @return the actual position or {@code -1} if the given {@code provider} * was already in the list. The actual position may be different * from the desired position. */ public static synchronized int insertProviderAt(Provider provider, int position) { // check that provider is not already // installed, else return -1; if (position <1) or (position > max // position) position = max position + 1; insert provider, shift up // one position for next providers; Note: The position is 1-based if (getProvider(provider.getName()) != null) { return -1; } int result = Services.insertProviderAt(provider, position); renumProviders(); return result; }
/** * Removes the {@code Provider} with the specified name form the collection * of providers. If the the {@code Provider} with the specified name is * removed, all provider at a greater position are shifted down one * position. * * <p>Returns silently if {@code name} is {@code null} or no provider with the * specified name is installed. * * @param name * the name of the provider to remove. */ public static synchronized void removeProvider(String name) { // It is not clear from spec.: // 1. if name is null, should we checkSecurityAccess or not? // throw SecurityException or not? // 2. as 1 but provider is not installed // 3. behavior if name is empty string? Provider p; if ((name == null) || (name.length() == 0)) { return; } p = getProvider(name); if (p == null) { return; } Services.removeProvider(p.getProviderNumber()); renumProviders(); p.setProviderNumber(-1); }
return null; java.util.List<Provider> result = Services.getProvidersList(); Set<Entry<String, String>> keys = filter.entrySet(); Map.Entry<String, String> entry;
/** * Finds the appropriate service implementation and returns an * {@code SpiAndProvider} instance containing a reference to SPI * and its {@code Provider} */ public SpiAndProvider getInstance(String algorithm, Object param) throws NoSuchAlgorithmException { if (algorithm == null) { throw new NoSuchAlgorithmException("Null algorithm name"); } int newCacheVersion = Services.getCacheVersion(); Provider.Service service; ServiceCacheEntry cacheEntry = this.serviceCache; if (cacheEntry != null && cacheEntry.algorithm.equalsIgnoreCase(algorithm) && newCacheVersion == cacheEntry.cacheVersion) { service = cacheEntry.service; } else { if (Services.isEmpty()) { throw notFound(serviceName, algorithm); } String name = this.serviceName + "." + algorithm.toUpperCase(Locale.US); service = Services.getService(name); if (service == null) { throw notFound(serviceName, algorithm); } this.serviceCache = new ServiceCacheEntry(algorithm, newCacheVersion, service); } return new SpiAndProvider(service.newInstance(param), service.getProvider()); }
/** * * Update sequence numbers of all providers. * */ private static void renumProviders() { Provider[] p = Services.getProviders(); for (int i = 0; i < p.length; i++) { p[i].setProviderNumber(i + 1); } }
/** * Returns the default SecureRandom service description. */ public static synchronized Provider.Service getSecureRandomService() { getCacheVersion(); // used for side effect of updating cache if needed return cachedSecureRandomService; }
/** * Returns the {@code Provider} with the specified name. Returns {@code * null} if name is {@code null} or no provider with the specified name is * installed. * * @param name * the name of the requested provider. * @return the provider with the specified name, maybe {@code null}. */ public static synchronized Provider getProvider(String name) { return Services.getProvider(name); }
/** * Returns the current cache version. This has the possible side * effect of updating the cache if needed. */ public static synchronized int getCacheVersion() { if (needRefresh) { cacheVersion++; synchronized (services) { services.clear(); } cachedSecureRandomService = null; for (Provider p : providers) { initServiceInfo(p); } needRefresh = false; } return cacheVersion; } }