// Path: lib_common/src/main/java/com/guiying/module/common/utils/CloseUtils.java
// public class CloseUtils {
// 
//     private CloseUtils() {
//         throw new UnsupportedOperationException("u can't instantiate me...");
//     }
// 
//     /**
//      * 关闭IO
//      *
//      * @param closeables closeable
//      */
//     public static void closeIO(Closeable... closeables) {
//         if (closeables == null) return;
//         for (Closeable closeable : closeables) {
//             if (closeable != null) {
//                 try {
//                     closeable.close();
//                 } catch (IOException e) {
//                     e.printStackTrace();
//                 }
//             }
//         }
//     }
// 
//     /**
//      * 安静关闭IO
//      *
//      * @param closeables closeable
//      */
//     public static void closeIOQuietly(Closeable... closeables) {
//         if (closeables == null) return;
//         for (Closeable closeable : closeables) {
//             if (closeable != null) {
//                 try {
//                     closeable.close();
//                 } catch (IOException ignored) {
//                 }
//             }
//         }
//     }
// }


import android.content.Context;
import android.support.annotation.RawRes;
import android.text.TextUtils;
import com.guiying.module.common.utils.CloseUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.security.InvalidKeyException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
package com.guiying.module.common.http;





/**
 * <p>Https证书校验工具类</p>
 *
 * @author 张华洋 2017/5/11 16:14
 * @version V1.2.0
 * @name HttpsUtils
 */
public class HttpsUtils {


    public static class SSLParams {
        public SSLSocketFactory sSLSocketFactory;
        public X509TrustManager trustManager;
    }

    /**
     * @param context   上下文
     * @param bksFileId "XXX.bks"文件(文件位置res/raw/XXX.bks)
     * @param password  The certificate's password.
     * @return SSLParams
     */
    public static SSLParams getSslSocketFactory(Context context, @RawRes int bksFileId, String password, String alias) {
        if (context == null) {
            throw new NullPointerException("context == null");
        }
        if (TextUtils.isEmpty(password) || TextUtils.isEmpty(alias)) {
            throw new NullPointerException("password == null or alias == null!");
        }
        SSLParams sslParams = new SSLParams();
        try {
            // 创建一个BKS类型的KeyStore，存储我们信任的证书
            KeyStore clientKeyStore = KeyStore.getInstance("BKS");
            clientKeyStore.load(context.getResources().openRawResource(bksFileId), password.toCharArray());
            //通过alias直接从密钥库中读取证书
            Certificate rootCA = clientKeyStore.getCertificate(alias);
            // Turn it to X509 format.
            InputStream certInput = new ByteArrayInputStream(rootCA.getEncoded());
            X509Certificate serverCert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(certInput);
            //关闭流
// Path: lib_common/src/main/java/com/guiying/module/common/utils/CloseUtils.java
// public class CloseUtils {
// 
//     private CloseUtils() {
//         throw new UnsupportedOperationException("u can't instantiate me...");
//     }
// 
//     /**
//      * 关闭IO
//      *
//      * @param closeables closeable
//      */
//     public static void closeIO(Closeable... closeables) {
//         if (closeables == null) return;
//         for (Closeable closeable : closeables) {
//             if (closeable != null) {
//                 try {
//                     closeable.close();
//                 } catch (IOException e) {
//                     e.printStackTrace();
//                 }
//             }
//         }
//     }
// 
//     /**
//      * 安静关闭IO
//      *
//      * @param closeables closeable
//      */
//     public static void closeIOQuietly(Closeable... closeables) {
//         if (closeables == null) return;
//         for (Closeable closeable : closeables) {
//             if (closeable != null) {
//                 try {
//                     closeable.close();
//                 } catch (IOException ignored) {
//                 }
//             }
//         }
//     }
// }


// Path: lib_common/src/main/java/com/guiying/module/common/http/HttpsUtils.java
import android.content.Context;
import android.support.annotation.RawRes;
import android.text.TextUtils;
import com.guiying.module.common.utils.CloseUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.security.InvalidKeyException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

package com.guiying.module.common.http;





/**
 * <p>Https证书校验工具类</p>
 *
 * @author 张华洋 2017/5/11 16:14
 * @version V1.2.0
 * @name HttpsUtils
 */
public class HttpsUtils {


    public static class SSLParams {
        public SSLSocketFactory sSLSocketFactory;
        public X509TrustManager trustManager;
    }

    /**
     * @param context   上下文
     * @param bksFileId "XXX.bks"文件(文件位置res/raw/XXX.bks)
     * @param password  The certificate's password.
     * @return SSLParams
     */
    public static SSLParams getSslSocketFactory(Context context, @RawRes int bksFileId, String password, String alias) {
        if (context == null) {
            throw new NullPointerException("context == null");
        }
        if (TextUtils.isEmpty(password) || TextUtils.isEmpty(alias)) {
            throw new NullPointerException("password == null or alias == null!");
        }
        SSLParams sslParams = new SSLParams();
        try {
            // 创建一个BKS类型的KeyStore，存储我们信任的证书
            KeyStore clientKeyStore = KeyStore.getInstance("BKS");
            clientKeyStore.load(context.getResources().openRawResource(bksFileId), password.toCharArray());
            //通过alias直接从密钥库中读取证书
            Certificate rootCA = clientKeyStore.getCertificate(alias);
            // Turn it to X509 format.
            InputStream certInput = new ByteArrayInputStream(rootCA.getEncoded());
            X509Certificate serverCert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(certInput);
            //关闭流
            CloseUtils.closeIO(certInput);