You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用纯Ballerina解析证书文件(无需Java互操作)

纯Ballerina实现读取X509证书信息

完全可以通过纯Ballerina实现读取X509证书(.cert/.crt格式)的功能,无需依赖Java互操作。Ballerina的标准ballerina/crypto模块提供了原生的证书处理能力,以下是对应实现代码:

import ballerina/crypto;
import ballerina/io;

public function main() returns error? {
    // 指定证书文件路径
    string certPath = "/path/to/file/cert.crt";
    // 从文件加载X509证书
    crypto:X509Certificate cert = check crypto:loadCertificateFromFile(certPath);
    
    // 输出证书有效期起始时间
    io:println("From: " + cert.notBefore.toString());
    // 输出证书有效期结束时间
    io:println("Until: " + cert.notAfter.toString());
}

代码说明:

  • crypto:loadCertificateFromFile:Ballerina标准库原生函数,直接从指定路径加载X509证书,返回crypto:X509Certificate类型实例
  • cert.notBefore/cert.notAfter:证书实例的内置属性,分别对应证书的生效起始时间与过期时间,调用toString()可直接格式化输出

内容的提问来源于stack exchange,提问作者Dilan Sachintha Nayanajith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 06:41:30