package com.skr.mdm.util;


import java.io.InputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: mdm_new
 * @Package: com.skr.mdm.systemSettings.controller
 * @ClassName: ClientUpgradeController
 * @Author: Tty
 * @Description: 客户端升级
 * @Date: 2020/9/27 9:11
 * @Version: 1.0
 */
public class ReadFileUtil {

    public synchronized static Map<String,Object> readCerFile(InputStream inputStream) throws Exception {
        Map<String, Object> result = new HashMap<>() ;
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate)cf.generateCertificate(inputStream);
        // 公钥
        result.put("publicKey", cert.getPublicKey()) ;
        // 版本
        result.put("version", cert.getVersion()) ;
        // 序列号
        result.put("serialNumber", cert.getSerialNumber().toString(16)) ;
        // 主体
        result.put("subjectDN", cert.getSubjectDN().getName()) ;
        // 颁发者
        result.put("issuerDN", cert.getIssuerDN().getName()) ;
        // 开始日期
        result.put("notBefore", cert.getNotBefore()) ;
        // 结束日期
        result.put("notAfter", cert.getNotAfter()) ;
        // 签名算法
        result.put("sigAlgName", cert.getSigAlgName()) ;
        return result ;
    }

}
