簡體   English   中英

PHP SSL證書指紋

[英]PHP SSL Certificate Fingerprint

我需要在SSL證書的網頁指紋中顯示。 在PHP中有可能嗎? 功能

openssl_x509_parse

不返回SHA1和MD5指紋。 怎么解決這個問題? 謝謝。

我認為您可以使用以下代碼生成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);
}

這是一個更好的解決方案:

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

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

我猜最簡單的方法是通過系統調用openssl

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

是的,我知道,這不是一個干凈的方式 - 但是,它是我能想到的最好的一個!

根據您的信息,我在PHP中編寫了一個小型證書查看器

用作烘焙自己觀眾的起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM