zoukankan      html  css  js  c++  java
  • 使用ssl-validator识别证书信息

    前言

    使用 npm包 ssl-validator 来获取证书的域名、有效期等信息。

    代码

    const sslValidator = require('ssl-validator');
    const SSLData = require('./data'); //引入你的证书信息 csr key
    
    (async () => {
        try {
            // 扫描所有有效信息是否配对
            const res = await sslValidator.validateSSL(SSLData.CSR, {
                key: SSLData.KEY
            });
            // 罗列几个主要的信息
            const certInfo = {
                domain: res.certInfo.commonName,
                validity: {
                    start: res.certInfo.validity.start,
                    startString: new Date(res.certInfo.validity.start).toLocaleString(),
                    end: res.certInfo.validity.end,
                    endString: new Date(res.certInfo.validity.end).toLocaleString()
                }
            };
            // console.log('res', res);
            console.log('certInfo', certInfo);
        } catch (e) {
            // console.error(e.message);
            // 不匹配的几种情况
            if(e.message.indexOf('unable to load certificate') !== -1 || e.message.indexOf('Certificate must start and end with proper formatting.') !== -1){
                console.error('无效的证书');
            }else if(e.message.indexOf('unable to load Private Key') !== -1 || e.message.indexOf('Key must start and end with proper formatting.') !== -1){
                console.error('无效的秘钥');
            }else if(e.message.indexOf('The certificate does not match the domain.') !== -1){
                console.error('证书与域名不匹配');
            }else if(e.message.indexOf('The provided certificate and key do not match.') !== -1){
                console.error('证书与秘钥不匹配');
            }else {
                console.error('证书、秘钥、域名格式错误或不匹配');
            }
        };
    })();
    
    

    总结

    1. 如果在windowns下执行,需要先安装OpenSSL
  • 相关阅读:
    【PC Basic】CPU、核、多线程的那些事儿
    为什么要使用 do while(0)?
    DPDK CAS(compare and set)操作
    编程中Foo,Bar 到底是什么意思
    如何用Python进行ARP攻击?
    有哪些有趣的化学方程式?
    1636. 按照频率将数组升序排序
    1046. 最后一块石头的重量
    1122. 数组的相对排序
    459. 重复的子字符串
  • 原文地址:https://www.cnblogs.com/xpengp/p/12762603.html
Copyright © 2011-2022 走看看