openssl_x509_check_private_key
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
openssl_x509_check_private_key — 检查私钥是否对应于证书
说明
openssl_x509_check_private_key(OpenSSLCertificate|string
$certificate
, #[\SensitiveParameter] OpenSSLAsymmetricKey|OpenSSLCertificate|array|string $private_key
): bool
检查指定的私钥 private_key
是否和证书 certificate
对应。
警告
这个函数不会检查密钥 private_key
是否真的是私钥。
它只是比较了和密钥匹配的公共材料 (比如,RSA 密钥的指数和模量) 和/或密钥参数(比如,EC 密钥的参数)。
这也意味着,比如,提供给 private_key
赋一个公钥值,该函数可能返回 true
。
参数
certificate
-
证书。
private_key
-
私钥。
更新日志
版本 | 说明 |
---|---|
8.0.0 |
certificate 现在接受 OpenSSLCertificate
实例;之前接受类型 OpenSSL X.509 的 resource。
|
8.0.0 |
private_key 现在接受 OpenSSLAsymmetricKey
或 OpenSSLCertificate 实例;之前接受类型 OpenSSL key 或
OpenSSL X.509 的 resource。
|
+添加备注
用户贡献的备注 2 notes
tomsie at toms dot ie ¶
7 years ago
This function DOES return TRUE if the key has a passphrase, you just need to set up the data in such a way that the function can understand it. It is not documented here.
This error message led me to the solution:
PHP Warning: openssl_x509_check_private_key(): key array must be of the form array(0 => key, 1 => phrase)
So this works:
$certFile = file_get_contents('cert.crt');
$keyFile = file_get_contents('cert.key');
$keyPassphrase = "password1234";
$keyCheckData = array(0=>$keyFile,1=>$keyPassphrase);
$result = openssl_x509_check_private_key($certFile,$keyCheckData);
jared at enhancesoft dot com ¶
9 years ago
This function will return FALSE if the private key requires a pass phrase.
备份地址:http://www.lvesu.com/blog/php/function.openssl-x509-check-private-key.php