SSL(Secure Sockets Layer)证书是用于在Web服务器和客户端之间建立安全连接的数字证书。它包含了公钥、证书持有者的信息以及由证书颁发机构(CA)签名的信息。SSL证书的哈希值通常用于验证证书的完整性和真实性。
SSL证书的哈希值通常使用SHA-256或SHA-384等哈希算法生成。
以下是一个使用PHP检查SSL证书哈希的示例代码:
<?php
function get_ssl_certificate_hash($domain) {
$context = stream_context_create([
'ssl' => [
'capture_session_meta' => true,
],
]);
$socket = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if (!$socket) {
throw new Exception("Failed to connect to $domain: $errstr ($errno)");
}
$meta = stream_get_meta_data($socket);
$cert = $meta['ssl']['peer_certificate'];
if (!$cert) {
throw new Exception("Failed to retrieve SSL certificate for $domain");
}
openssl_x509_free($cert);
return hash('sha256', $cert);
}
try {
$domain = 'example.com';
$hash = get_ssl_certificate_hash($domain);
echo "SSL Certificate Hash for $domain: $hash\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
通过以上步骤和代码示例,您可以检查SSL证书的哈希值,并确保其完整性和真实性。
领取专属 10元无门槛券
手把手带您无忧上云