2011-06-21 9 views

Odpowiedz

1

Przypuszczam, najprostszym sposobem będzie zadzwonić OpenSSL poprzez system

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

I tak, wiem, to nic innego jak czysty sposób to zrobić - jednak jest to tylko jeden Mogę myśleć o mojej głowie !!!

+0

Obawiam się, że jeśli system operacyjny lub exec-works będzie zależny od systemu operacyjnego. Do tego staram się używać tylko funkcji PHP dla OpenSSL –

13

myślę, że można generować odcisk palca SHA z następującego kodu:

$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); 
} 
+0

Pracowałem świetnie dla mnie, dzięki. –

4

Oto lepsze rozwiązanie:

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

z PHP 5.6 roku, można użyć 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 

funkcja jest obecnie nieudokumentowana, ale ta zostanie ustalona w czasie zwolnienia; to jest podpis funkcji:

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