java.security.Security.getProvider()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(579)

本文整理了Java中java.security.Security.getProvider()方法的一些代码示例,展示了Security.getProvider()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Security.getProvider()方法的具体详情如下:
包路径:java.security.Security
类名称:Security
方法名:getProvider

Security.getProvider介绍

[英]Returns the Provider with the specified name. Returns null if name is null or no provider with the specified name is installed.
[中]返回具有指定名称的提供程序。如果名称为null或未安装具有指定名称的提供程序,则返回null。

代码示例

代码示例来源:origin: aws/aws-sdk-java

public static synchronized boolean isBouncyCastleAvailable() {
  return Security.getProvider(BOUNCY_CASTLE_PROVIDER) != null;
}

代码示例来源:origin: prestodb/presto

/**
 * Checks to see if Google Play Services Dynamic Security Provider is present which provides ALPN
 * support. If it isn't checks to see if device is Android 5.0+ since 4.x device have broken
 * ALPN support.
 */
private static boolean supportsAlpn() {
 if (Security.getProvider("GMSCore_OpenSSL") != null) {
  return true;
 } else {
  try {
   Class.forName("android.net.Network"); // Arbitrary class added in Android 5.0.
   return true;
  } catch (ClassNotFoundException ignored) { }
 }
 return false;
}

代码示例来源:origin: floragunncom/search-guard

@Override
  public Object run() {
    if(Security.getProvider("BC") == null) {
      Security.addProvider(new BouncyCastleProvider());
    }
    return null;
  }
});

代码示例来源:origin: plutext/docx4j

@SuppressWarnings("unchecked")
public static void registerBouncyCastle() {
  if (Security.getProvider("BC") != null) return;
  try {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    String bcProviderName = "org.bouncycastle.jce.provider.BouncyCastleProvider";
    Class<Provider> clazz = (Class<Provider>)cl.loadClass(bcProviderName);
    Security.addProvider(clazz.newInstance());
  } catch (Exception e) {
    throw new EncryptedDocumentException("Only the BouncyCastle provider supports your encryption settings - please add it to the classpath.");
  }
}

代码示例来源:origin: org.apache.poi/poi

@SuppressWarnings("unchecked")
public static void registerBouncyCastle() {
  if (Security.getProvider("BC") != null) {
    return;
  }
  
  try {
    ClassLoader cl = CryptoFunctions.class.getClassLoader();
    String bcProviderName = "org.bouncycastle.jce.provider.BouncyCastleProvider";
    Class<Provider> clazz = (Class<Provider>)cl.loadClass(bcProviderName);
    Security.addProvider(clazz.newInstance());
  } catch (Exception e) {
    throw new EncryptedDocumentException("Only the BouncyCastle provider supports your encryption settings - please add it to the classpath.", e);
  }
}

代码示例来源:origin: apache/nifi

/**
 * Initializes the encryptor with a {@link KeyProvider}.
 *
 * @param keyProvider the key provider which will be responsible for accessing keys
 * @throws KeyManagementException if there is an issue configuring the key provider
 */
@Override
public void initialize(KeyProvider keyProvider) throws KeyManagementException {
  this.keyProvider = keyProvider;
  if (this.aesKeyedCipherProvider == null) {
    this.aesKeyedCipherProvider = new AESKeyedCipherProvider();
  }
  if (Security.getProvider("BC") == null) {
    Security.addProvider(new BouncyCastleProvider());
  }
}

代码示例来源:origin: apache/storm

@Override
protected TTransportFactory getServerTransportFactory(boolean impersonationAllowed) throws IOException {
  //create an authentication callback handler
  CallbackHandler serverCallbackHandler = new SimpleSaslServerCallbackHandler(impersonationAllowed,
                                        (userName) -> Optional.of("password".toCharArray()));
  if (Security.getProvider(SaslPlainServer.SecurityProvider.SASL_PLAIN_SERVER) == null) {
    Security.addProvider(new SaslPlainServer.SecurityProvider());
  }
  //create a transport factory that will invoke our auth callback for digest
  TSaslServerTransport.Factory factory = new TSaslServerTransport.Factory();
  factory.addServerDefinition(PLAIN, ClientAuthUtils.SERVICE, "localhost", null, serverCallbackHandler);
  LOG.error("SASL PLAIN transport factory will be used.  This is totally insecure.  Please do not use this.");
  return factory;
}

代码示例来源:origin: robovm/robovm

/**
 * Returns a new instance of {@code KeyFactory} that utilizes the specified
 * algorithm from the specified provider.
 *
 * @param algorithm
 *            the name of the algorithm.
 * @param provider
 *            the name of the provider.
 * @return a new instance of {@code KeyFactory} that utilizes the specified
 *         algorithm from the specified provider.
 * @throws NoSuchAlgorithmException
 *             if the provider does not provide the requested algorithm.
 * @throws NoSuchProviderException
 *             if the requested provider is not available.
 * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
 */
public static KeyFactory getInstance(String algorithm, String provider)
    throws NoSuchAlgorithmException, NoSuchProviderException {
  if (provider == null || provider.isEmpty()) {
    throw new IllegalArgumentException();
  }
  Provider p = Security.getProvider(provider);
  if (p == null) {
    throw new NoSuchProviderException(provider);
  }
  return getInstance(algorithm, p);
}

代码示例来源:origin: robovm/robovm

/**
 * Returns a new instance of {@code KeyPairGenerator} that utilizes the
 * specified algorithm from the specified provider.
 *
 * @param algorithm
 *            the name of the algorithm to use
 * @param provider
 *            the name of the provider
 * @return a new instance of {@code KeyPairGenerator} that utilizes the
 *         specified algorithm from the specified provider
 * @throws NoSuchAlgorithmException if the specified algorithm is not available
 * @throws NoSuchProviderException if the specified provider is not available
 * @throws NullPointerException
 *             if {@code algorithm} is {@code null}
 * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
 */
public static KeyPairGenerator getInstance(String algorithm, String provider)
    throws NoSuchAlgorithmException, NoSuchProviderException {
  if (provider == null || provider.isEmpty()) {
    throw new IllegalArgumentException();
  }
  Provider impProvider = Security.getProvider(provider);
  if (impProvider == null) {
    throw new NoSuchProviderException(provider);
  }
  return getInstance(algorithm, impProvider);
}

代码示例来源:origin: robovm/robovm

/**
 * Returns a new instance of {@code MessageDigest} that utilizes the
 * specified algorithm from the specified provider.
 *
 * @param algorithm
 *            the name of the algorithm to use
 * @param provider
 *            the name of the provider
 * @return a new instance of {@code MessageDigest} that utilizes the
 *         specified algorithm from the specified provider
 * @throws NoSuchAlgorithmException
 *             if the specified algorithm is not available
 * @throws NoSuchProviderException
 *             if the specified provider is not available
 * @throws NullPointerException
 *             if {@code algorithm} is {@code null}
 * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
 */
public static MessageDigest getInstance(String algorithm, String provider)
    throws NoSuchAlgorithmException, NoSuchProviderException {
  if (provider == null || provider.isEmpty()) {
    throw new IllegalArgumentException();
  }
  Provider p = Security.getProvider(provider);
  if (p == null) {
    throw new NoSuchProviderException(provider);
  }
  return getInstance(algorithm, p);
}

代码示例来源:origin: wildfly/wildfly

/**
 * Set the JCA provider name which contains all classes needed by built utility class.
 * @param providerName the provider name
 * @return this Builder
 */
public Builder provider(String providerName) {
  Assert.checkNotNullParam("providerName", providerName);
  provider = Security.getProvider(providerName);
  if (provider == null) {
    throw log.securityProviderDoesnExist(providerName);
  }
  return this;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Get a password factory instance.  The returned password factory object will implement the given algorithm.
 *
 * @param algorithm the name of the algorithm
 * @param providerName the name of the provider to use
 * @return a password factory instance
 * @throws NoSuchAlgorithmException if the given algorithm has no available implementations
 */
public static PasswordFactory getInstance(String algorithm, String providerName) throws NoSuchAlgorithmException, NoSuchProviderException {
  final Provider provider = Security.getProvider(providerName);
  if (provider == null) throw new NoSuchProviderException(providerName);
  return getInstance(algorithm, provider);
}

代码示例来源:origin: wildfly/wildfly

/**
 * Get a {@code CredentialStore} instance.  The returned CredentialStore object will implement the given algorithm.
 *
 * @param algorithm the name of the algorithm
 * @param providerName the name of the provider to use
 * @return a {@code CredentialStore} instance
 * @throws NoSuchAlgorithmException if the given algorithm has no available implementations
 * @throws NoSuchProviderException if given provider name cannot match any registered {@link Provider}
 */
public static CredentialStore getInstance(String algorithm, String providerName) throws NoSuchAlgorithmException, NoSuchProviderException {
  final Provider provider = Security.getProvider(providerName);
  if (provider == null) throw new NoSuchProviderException(providerName);
  return getInstance(algorithm, provider);
}

代码示例来源:origin: Javen205/IJPay

/**
 * 添加签名,验签,加密算法提供者
 */
private static void addProvider(){
  if (Security.getProvider("BC") == null) {
    LogUtil.writeLog("add BC provider");
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  } else {
    Security.removeProvider("BC"); //解决eclipse调试时tomcat自动重新加载时,BC存在不明原因异常的问题。
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    LogUtil.writeLog("re-add BC provider");
  }
  printSysInfo();
}

代码示例来源:origin: robovm/robovm

/**
 * 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;
}

代码示例来源:origin: apache/incubator-gobblin

/**
 * Generate a PGPEncryptedDataList from an inputstream
 * @param inputStream file inputstream that needs to be decrypted
 * @throws IOException
 */
private PGPEncryptedDataList getPGPEncryptedDataList(InputStream inputStream) throws IOException {
 if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
  Security.addProvider(new BouncyCastleProvider());
 }
 inputStream = PGPUtil.getDecoderStream(inputStream);
 JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(inputStream);
 PGPEncryptedDataList enc;
 Object pgpfObject = pgpF.nextObject();
 if (pgpfObject instanceof PGPEncryptedDataList) {
  enc = (PGPEncryptedDataList) pgpfObject;
 } else {
  enc = (PGPEncryptedDataList) pgpF.nextObject();
 }
 return enc;
}

代码示例来源:origin: voldemort/voldemort

private void testJCEProvider() throws IOException {
  Properties properties = new Properties();
  // Default configuration. Bouncy castle provider will not be used.
  server = getVoldemortServer(properties);
  assertNull(Security.getProvider(BouncyCastleProvider.PROVIDER_NAME));
}

代码示例来源:origin: ethereum/ethereumj

@Test
public void testSunECRoundTrip() throws Exception {
  Provider provider = Security.getProvider("SunEC");
  if (provider != null) {
    testProviderRoundTrip(provider);
  } else {
    System.out.println("Skip test as provider doesn't exist. " +
        "Must be OpenJDK which could be shipped without 'SunEC'");
  }
}

代码示例来源:origin: ethereum/ethereumj

@Test(expected = IllegalArgumentException.class)
public void testInvalidPrivateKey() throws Exception {
  new ECKey(
    Security.getProvider("SunEC"),
    KeyPairGenerator.getInstance("RSA").generateKeyPair().getPrivate(),
    ECKey.fromPublicOnly(pubKey).getPubKeyPoint());
  fail("Expecting an IllegalArgumentException for using an non EC private key");
}

代码示例来源:origin: apache/zookeeper

String keyStorePassword,
          Boolean hostnameVerification) throws IOException, GeneralSecurityException, OperatorCreationException {
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
  throw new IllegalStateException("BC Security provider was not found");

相关文章