openssl 证书与私钥匹配校验
确认证书与私钥是否配对
语法
openssl x509 -noout -modulus -in 证书 | openssl sha256
openssl rsa -noout -modulus -in 私钥 | openssl sha256说明
适用于证书更新后服务无法启动、日志提示 key values mismatch 的场景。原理是比较证书公钥与私钥各自输出的模数(RSA)或公钥点(EC),两者一致才算配对。这一步不能靠比对文件名或生成时间判断。
参数
-modulus必填输出 RSA 公钥模数
-pubout输出公钥,适合非 RSA 算法比对
示例
比对 RSA 证书与私钥
哈希一致表示配对成功。
openssl x509 -noout -modulus -in cert.pem | openssl sha256 && openssl rsa -noout -modulus -in private.key | openssl sha256注意事项:两行输出的哈希值必须完全相同。EC 密钥没有 modulus,需要改用 -pubout 后比对公钥内容。
比对 EC 证书与私钥
非对称算法下比较公钥部分。
openssl x509 -in cert.pem -noout -pubkey | openssl sha256 && openssl pkey -in ec-private.key -pubout | openssl sha256注意事项:同样要求两行哈希一致。若不一致,说明部署时替换了其中一份文件。