【问题标题】:PHP SSL Certificate FingerprintPHP SSL 证书指纹
【发布时间】:2011-09-19 14:15:36
【问题描述】:

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

openssl_x509_parse

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

【问题讨论】:

    标签: php ssl-certificate md5 sha1 fingerprint


    【解决方案1】:

    我认为您可以使用以下代码生成 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);
    }
    

    【讨论】:

      【解决方案2】:

      这里有一个更好的解决方案:

       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);
       }
      

      【讨论】:

        【解决方案3】:

        从 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 ] ] )
        

        【讨论】:

          【解决方案4】:

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

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

          是的,我知道,这绝不是一种干净的方式 - 但是,这是我唯一能想到的方法!!!

          【讨论】:

          • 恐怕如果使用系统或执行 - 作品将取决于操作系统。为此,我尝试仅对 OpenSSL 使用 PHP 函数
          【解决方案5】:

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

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

          【讨论】:

            猜你喜欢
            • 2016-06-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-03
            • 2010-11-08
            • 2013-08-09
            • 2019-10-29
            相关资源
            最近更新 更多