Esempio n. 1
0
 /** @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;
 }
 public static final CertificateFactory getInstance(String type) throws CertificateException {
   if (type == null) {
     throw new NullPointerException("type == null");
   }
   try {
     SpiAndProvider sap = ENGINE.getInstance(type, null);
     return new CertificateFactory((CertificateFactorySpi) sap.spi, sap.provider, type);
   } catch (Throwable e) {
     throw new CertificateException(e);
   }
 }
 public static final CertificateFactory getInstance(String type, Provider provider)
     throws CertificateException {
   if (provider == null) {
     throw new IllegalArgumentException("provider == null");
   } else if (type == null) {
     throw new NullPointerException("type == null");
   } else {
     try {
       return new CertificateFactory(
           (CertificateFactorySpi) ENGINE.getInstance(type, provider, null), provider, type);
     } catch (Throwable e) {
       throw new CertificateException(e);
     }
   }
 }
Esempio n. 4
0
 /**
  * Returns a new instance of {@code MessageDigest} that utilizes the specified algorithm.
  *
  * @param algorithm the name of the algorithm to use
  * @return a new instance of {@code MessageDigest} that utilizes the specified algorithm
  * @throws NoSuchAlgorithmException if the specified algorithm is not available
  * @throws NullPointerException if {@code algorithm} is {@code null}
  */
 public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException {
   if (algorithm == null) {
     throw new NullPointerException();
   }
   MessageDigest result;
   synchronized (engine) {
     engine.getInstance(algorithm, null);
     if (engine.spi instanceof MessageDigest) {
       result = (MessageDigest) engine.spi;
       result.algorithm = algorithm;
       result.provider = engine.provider;
       return result;
     }
     return new MessageDigestImpl((MessageDigestSpi) engine.spi, engine.provider, algorithm);
   }
 }
Esempio n. 5
0
  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;
  }