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
  • 相关阅读:
    DevExpress-xaml常用控件
    循环中Random()产生相同随机数问题的对策
    插入U盘提示程序
    钛度TSG309鼠,血手幽灵P93 彩漫鼠,微星GM41Lightweight鼠标参数对比
    MarkdownPad
    C# 通过路径取文件方法
    初识张首晟教授有感
    【机器学习123】模型评估与选择 (上)
    【机器学习123】绪论
    【深度学习123】开篇
  • 原文地址:https://www.cnblogs.com/xpengp/p/12762603.html
Copyright © 2011-2022 走看看