PHP SSL 证书指纹

发布于 2024-11-16 14:32:54 字数 126 浏览 8 评论 0原文

我需要在网页中显示 SSL 证书的指纹。 PHP 中可以吗?该函数

openssl_x509_parse

不返回 SHA1 和 MD5 指纹。 如何解决这个问题? 谢谢。

I need display in web page fingerprints of SSL Certificate.
Is it possible in PHP? The function

openssl_x509_parse

doesn't return SHA1 and MD5 fingerprints.
How resolve this problem?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疏忽 2024-11-23 14:32:54

我认为您可以使用以下代码生成 SHA 指纹:

$resource = openssl_x509_read($certificate);

$fingerprint = null;
$output = null;

$result = openssl_x509_export($resource, $output);
if($result !== false) {
    $output = str_replace('-----BEGIN CERTIFICATE-----', '', $output);
    $output = str_replace('-----END CERTIFICATE-----', '', $output);

    $output = base64_decode($output);

    $fingerprint = sha1($output);
}

I think you can generate the SHA fingerprint with the following code:

$resource = openssl_x509_read($certificate);

$fingerprint = null;
$output = null;

$result = openssl_x509_export($resource, $output);
if($result !== false) {
    $output = str_replace('-----BEGIN CERTIFICATE-----', '', $output);
    $output = str_replace('-----END CERTIFICATE-----', '', $output);

    $output = base64_decode($output);

    $fingerprint = sha1($output);
}
゛时过境迁 2024-11-23 14:32:54

这是一个更好的解决方案:

 function sha1_thumbprint($file)
 {
    $file = preg_replace('/\-+BEGIN CERTIFICATE\-+/','',$file);
    $file = preg_replace('/\-+END CERTIFICATE\-+/','',$file);
    $file = trim($file);
    $file = str_replace( array("\n\r","\n","\r"), '', $file);
    $bin = base64_decode($file);
    return sha1($bin);
 }

Here is a better solution:

 function sha1_thumbprint($file)
 {
    $file = preg_replace('/\-+BEGIN CERTIFICATE\-+/','',$file);
    $file = preg_replace('/\-+END CERTIFICATE\-+/','',$file);
    $file = trim($file);
    $file = str_replace( array("\n\r","\n","\r"), '', $file);
    $bin = base64_decode($file);
    return sha1($bin);
 }
∞琼窗梦回ˉ 2024-11-23 14:32:54

从 PHP 5.6 开始,您可以使用 openssl_x509_fingerprint()

$cert = openssl_x509_read($certificate);
$sha1_hash = openssl_x509_fingerprint($cert); // sha1 hash
$md5_hash = openssl_x509_fingerprint($cert, 'md5'); // md5 hash

该函数当前未记录,但这将在发布时修复;这是函数签名:

openssl_x509_fingerprint($cert [, $hash_method = "sha1" [, $raw_output = false ] ] )

From PHP 5.6 onwards, you can use openssl_x509_fingerprint():

$cert = openssl_x509_read($certificate);
$sha1_hash = openssl_x509_fingerprint($cert); // sha1 hash
$md5_hash = openssl_x509_fingerprint($cert, 'md5'); // md5 hash

The function is currently undocumented, but this will be fixed at release time; this is the function signature:

openssl_x509_fingerprint($cert [, $hash_method = "sha1" [, $raw_output = false ] ] )
何时共饮酒 2024-11-23 14:32:54

我猜最简单的方法是通过系统调用 openssl 是的

$fingerprint = str_replace("SHA1 Fingerprint=", '', system('openssl x509 -noout -in /path/to/your/cert.pem -fingerprint'));

,我知道,这并不是一种干净的方法 - 然而,这是我能想到的唯一方法! !!

I'd guess the easiest way is going to be to call openssl through system

$fingerprint = str_replace("SHA1 Fingerprint=", '', system('openssl x509 -noout -in /path/to/your/cert.pem -fingerprint'));

And yes, I know, this is nothing like a clean way of doing this - however, it's the only one I can think of of the top of my head!!!

爱的那么颓废 2024-11-23 14:32:54

根据您的信息,我用 PHP 编写了一个小型证书查看器

使用作为烘焙您自己的查看器的起点。

Based on your information, I wrote a tiny certificate viewer in PHP

Use as your starting point to bake your own viewer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文