<commit_before>package com.miguelgaeta.bootstrap.mg_ssl;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * Created by mrkcsc on 7/18/14.
 *
 * Reference: https://developer.android.com/training/articles/security-ssl.html
 */
public class MGSSLFactory {

    /**
     * Create an SSL socket factory.  If desired can be
     * set to insecure which allows all certificates.
     */
    public static SSLSocketFactory createSSLSocketFactory(boolean insecure) {

        SSLContext sslContext = getSSLContext();

        try {

            if (insecure) {

                // Trust all certificates.
                sslContext.init(null, getInsecureTrustManager(), new java.security.SecureRandom());

            } else {

                // Use default ones.
                sslContext.init(null, null, null);
            }

        } catch (KeyManagementException e) {

            throw new RuntimeException("Unable to initialize SSL context.", e);
        }

        return sslContext.getSocketFactory();
    }

    /**
     * Create an SSL socket factory.
     */
    public static SSLSocketFactory createSSLSocketFactory() {
<commit_msg>Additional code to provide a custom CA certificate.<commit_after>package com.miguelgaeta.bootstrap.mg_ssl;

import android.content.res.AssetManager;
import android.support.annotation.Nullable;

import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import lombok.NonNull;

/**
 * Created by mrkcsc on 7/18/14.
 *
 * Reference: https://developer.android.com/training/articles/security-ssl.html
 */
public class MGSSLFactory {

    @SuppressWarnings("unused")
    public static TrustManagerFactory getTrustManager(@NonNull AssetManager assetManager, @NonNull String certificatePath) {

        try {

            final InputStream is = assetManager.open(certificatePath);

            final CertificateFactory cf = CertificateFactory.getInstance("X.509");
            final X509Certificate caCert = (X509Certificate)cf.generateCertificate(is);

            is.close();

            final TrustManagerFactory tmf = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());

            final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
            ks.load(null); // You don't need the KeyStore instance to come from a file.
            ks.setCertificateEntry("caCert", caCert);

            tmf.init(ks);

            return tmf;

        } catch (Exception e) {

            throw new RuntimeException("Unable to obtain an SSL context.", e);
        }
    }

    @SuppressWarnings("unused")
    public static SSLSocketFactory createSSLSocketFactory(boolean insecure, @Nullable TrustManagerFactory trustManagerFactory) {

        SSLContext sslContext = getSSLContext();

        try {

            if (insecure) {

                // Trust all certificates.
                sslContext.init(null, getInsecureTrustManager(), new java.security.SecureRandom());

            } else {

                // Trust device certificates, plus provided ones.
                sslContext.init(null, trustManagerFactory != null ? trustManagerFactory.getTrustManagers() : null, null);
            }

        } catch (KeyManagementException e) {

            throw new RuntimeException("Unable to initialize SSL context.", e);
        }

        return sslContext.getSocketFactory();
    }

    public static SSLSocketFactory createSSLSocketFactory(boolean insecure) {

        return createSSLSocketFactory(insecure, null);
    }

    @SuppressWarnings("unused")
    public static SSLSocketFactory createSSLSocketFactory() {

{'fraction_non_alphanumeric': 0.09044048734770384, 'fraction_numerical': 0.006560449859418931, 'mean_word_length': 2.9237132352941178, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7062888', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1076, 'n_words': 261}