Example usage for java.security Security getProviders

List of usage examples for java.security Security getProviders

Introduction

In this page you can find the example usage for java.security Security getProviders.

Prototype

public static Provider[] getProviders() 

Source Link

Document

Returns an array containing all the installed providers.

Usage

From source file:com.zacwolf.commons.crypto._CRYPTOfactory.java

public static void dumpCiphers() {
    for (Provider provider : Security.getProviders()) {
        System.out.println(provider.getName());
        for (String key : provider.stringPropertyNames())
            System.out.println("\t" + key + "\t" + provider.getProperty(key));
    }/*ww  w .j a v a  2s  .  co  m*/
}

From source file:eu.europa.esig.dss.DSSUtils.java

/**
 * This method lists all defined security providers.
 *///from  ww w.  java2s. c o m
public static void printSecurityProvides() {

    final Provider[] providers = Security.getProviders();
    for (final Provider provider : providers) {

        System.out.println("PROVIDER: " + provider.getName());
        final Set<Provider.Service> services = provider.getServices();
        for (final Provider.Service service : services) {

            System.out.println("\tALGORITHM: " + service.getAlgorithm() + " / " + service.getType() + " / "
                    + service.getClassName());
        }
    }
}

From source file:org.hyperledger.fabric.sdk.security.CryptoPrimitives.java

/**
 * Return X509Certificate  from pem bytes.
 * So you may ask why this ?  Well some providers (BC) seems to have problems with creating the
 * X509 cert from bytes so here we go through all available providers till one can convert. :)
 *
 * @param pemCertificate//from  w w  w .  j a  va2  s .c o m
 * @return
 */

private X509Certificate getX509Certificate(byte[] pemCertificate) throws CryptoException {
    X509Certificate ret = null;
    CryptoException rete = null;

    List<Provider> providerList = new LinkedList<>(Arrays.asList(Security.getProviders()));
    if (SECURITY_PROVIDER != null) { //Add if overridden
        providerList.add(SECURITY_PROVIDER);
    }
    try {
        providerList.add(BouncyCastleProvider.class.newInstance()); // bouncy castle is there always.
    } catch (Exception e) {
        logger.warn(e);

    }
    for (Provider provider : providerList) {
        try {
            if (null == provider) {
                continue;
            }
            CertificateFactory certFactory = CertificateFactory.getInstance(CERTIFICATE_FORMAT, provider);
            if (null != certFactory) {
                try (ByteArrayInputStream bis = new ByteArrayInputStream(pemCertificate)) {
                    Certificate certificate = certFactory.generateCertificate(bis);

                    if (certificate instanceof X509Certificate) {
                        ret = (X509Certificate) certificate;
                        rete = null;
                        break;
                    }
                }

            }
        } catch (Exception e) {

            rete = new CryptoException(e.getMessage(), e);

        }

    }

    if (null != rete) {

        throw rete;

    }

    if (ret == null) {

        logger.error("Could not convert pem bytes");

    }

    return ret;

}

From source file:org.jenkinsci.plugins.relution_publisher.configuration.jobs.ArtifactPublisher.java

private void logProviderInformation(final Log log) {
    log.write(this, "Available security providers:");

    final Provider[] providers = Security.getProviders();
    for (final Provider provider : providers) {
        log.write(this, "%s %s", provider.getName(), String.valueOf(provider.getVersion()));
    }/*from   w w w  . j a va 2s  .com*/
    log.write();
}

From source file:org.jets3t.service.security.EncryptionUtil.java

public static Provider[] listAvailableProviders() {
    return Security.getProviders();
}

From source file:org.josso.selfservices.password.generator.RandomFactory.java

public Set<String> getAlgorithms() {
    Set<String> result = new HashSet<String>();

    // All providers
    Provider[] providers = Security.getProviders();
    for (int i = 0; i < providers.length; i++) {
        // Get services provided by each provider
        Set keys = providers[i].keySet();
        for (Iterator<String> it = keys.iterator(); it.hasNext();) {
            String key = (String) it.next();
            String value = (String) providers[i].get(key);
            result.add(value);//  ww  w .j a  v a  2s. com
        }
    }
    return result;
}

From source file:org.josso.selfservices.password.generator.RandomFactory.java

/**
 * Returns a cleaned up version of the service providers.
 * //from   www .  jav  a2 s . c om
 * @return a set of service providers that can be used for SecureRandom
 *         feed.
 */
public Set<String> getServiceProviders() {
    Set<String> result = new HashSet<String>();

    // All providers
    Provider[] providers = Security.getProviders();
    for (int i = 0; i < providers.length; i++) {
        // Get services provided by each provider
        Set keys = providers[i].keySet();
        for (Iterator<String> it = keys.iterator(); it.hasNext();) {
            String key = (String) it.next();
            key = key.split(" ")[0]; //$NON-NLS-1$

            if (key.startsWith(ALG_PARSE_STRING)) {
                // Strip the alias
                key = key.substring(10);
            }
            int ix = key.indexOf('.');
            result.add(key.substring(0, ix));
        }
    }
    return result;
}

From source file:org.josso.selfservices.password.generator.RandomFactory.java

public Set<String> getServiceProviderFor(String type) {
    Set<String> result = new HashSet<String>();

    Provider[] providers = Security.getProviders();
    for (int i = 0; i < providers.length; i++) {
        // Get services provided by each provider
        Set keys = providers[i].keySet();
        for (Iterator it = keys.iterator(); it.hasNext();) {
            String key = (String) it.next();
            key = key.split(" ")[0]; //$NON-NLS-1$

            if (key.startsWith(type + ".")) //$NON-NLS-1$
            {/*from  w w  w  .  j  a  v  a 2 s . com*/
                result.add(key.substring(type.length() + 1));
            } else if (key.startsWith(ALG_PARSE_STRING + type + ".")) //$NON-NLS-1$
            {
                // This is an alias
                result.add(key.substring(type.length() + 11));
            }
        }
    }
    return result;
}

From source file:org.mule.providers.ldap.LdapSASLConnector.java

protected void doInitialise() throws InitialisationException {

    // if (isForceJDK14())
    // {//from   w  ww  .  ja va  2  s  .c  o m
    // logger.debug("forcing JDK 1.4 SASL mode");
    Security.addProvider(new com.novell.sasl.client.SaslProvider());
    // }
    /*
     * else { Provider sunSASL = Security.getProvider("SunSASL");
     * 
     * if (sunSASL != null) { logger .debug("SunSASL implementation (JDK >=
     * 1.5) detected. Use it."); try { Sasl.setSaslClientFactory(new
     * SaslBridgeClientFactory()); } catch (RuntimeException e) {
     * logger.warn(e.toString()); } } else { logger .debug("No SunSASL
     * implementation (JDK >= 1.5 detected. Fall back to JDK 1.4 mode");
     * Security.addProvider(new com.novell.sasl.client.SaslProvider()); } }
     */

    if (logger.isDebugEnabled()) {

        Provider[] ps = Security.getProviders();
        for (int i = 0; i < ps.length; i++) {
            Provider provider = ps[i];
            logger.debug(provider.getClass() + "/" + provider.getName() + "/" + provider.getVersion() + "/"
                    + provider.getInfo());

        }
    }

    if (MECHANISM_DIGEST_EXTERNAL.equals(mechanism)) {

        try {
            if (trustAll) {
                SSLContext context = SSLContext.getInstance("TLS");
                context.init(null, trustAll ? TrustAllCertsManager.getTrustAllCertsManager() : null, null);

                // certificate_unknown
                ssf = new LDAPJSSESecureSocketFactory(context.getSocketFactory());
            } else {
                if (StringUtils.isEmpty(trustStore)) {
                    throw new InitialisationException(new IllegalArgumentException(
                            "Either trustAll value must be true or the trustStore parameter must be set"),
                            this);
                }

                File trustStoreFile = new File(trustStore);

                if (!trustStoreFile.exists() || !trustStoreFile.canRead()) {
                    throw new InitialisationException(new IllegalArgumentException("truststore file "
                            + trustStoreFile.getAbsolutePath() + " do not exist or is not readable"), this);
                }

                System.setProperty("javax.net.ssl.trustStore", trustStore);

                logger.debug("truststore set to " + trustStoreFile.getAbsolutePath());

                ssf = new LDAPJSSESecureSocketFactory();
            }
            // pix path
            // ssf = new LDAPJSSESecureSocketFactory((SSLSocketFactory)
            // SSLSocketFactory.getDefault());

            // TODO SSL<->TLS (TLS maybe require startTLS() call on lc
            // ssf = new LDAPJSSEStartTLSFactory();
        } catch (KeyManagementException e) {
            throw new InitialisationException(e, this);
        } catch (NoSuchAlgorithmException e) {
            throw new InitialisationException(e, this);
        }

    }

    super.doInitialise();
}

From source file:org.nuxeo.common.codec.Crypto.java

/**
 * The method returns either the decrypted {@code strToDecrypt}, either the {@code strToDecrypt} itself if it is not
 * recognized as a crypted string or if the decryption fails. The return value is a byte array for security purpose,
 * it is your responsibility to convert it then to a String or not (use of {@code char[]} is recommended).
 *
 * @param strToDecrypt//  w  ww.  j  a  v a2  s  . co m
 * @return the decrypted {@code strToDecrypt} as an array of bytes, never {@code null}
 * @see #getChars(byte[])
 */
public byte[] decrypt(String strToDecrypt) {
    Matcher matcher = CRYPTO_PATTERN.matcher(strToDecrypt);
    if (!matcher.matches()) {
        return strToDecrypt.getBytes();
    }
    Cipher decipher;
    try {
        String algorithm = new String(Base64.decodeBase64(matcher.group("algo")));
        if (StringUtils.isBlank(algorithm)) {
            algorithm = DEFAULT_ALGO;
        }
        decipher = Cipher.getInstance(algorithm);
        decipher.init(Cipher.DECRYPT_MODE, getSecretKey(algorithm, secretKey));
        final byte[] decryptedString = decipher.doFinal(Base64.decodeBase64(matcher.group("value")));
        return decryptedString;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        log.trace("Available algorithms: " + Security.getAlgorithms("Cipher"));
        log.trace("Available security providers: " + Arrays.asList(Security.getProviders()));
        log.debug(e, e);
    } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        log.debug(e, e);
    }
    return strToDecrypt.getBytes();
}