Skip to content

Instantly share code, notes, and snippets.

@hongster
Created August 7, 2023 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongster/60bf17512c35ad58831beb382a5ae57d to your computer and use it in GitHub Desktop.
Save hongster/60bf17512c35ad58831beb382a5ae57d to your computer and use it in GitHub Desktop.
Extract SSL SHA256 Fingerprint
#!/usr/bin/env php
<?php
/**
* Get SHA5 digest for remote SSL cert
*
* @param string $host Domain or IP address
* @param int $port Port number, default 443.
* @return string Hexadecimal in lowercase
*/
function getSHA256Digest($host, $port = 443) {
// Since it is connecting to VPN by IP, verifying domain name will failed.
$contextCreate = stream_context_create(["ssl" => [
"capture_peer_cert" => true,
// Disable verifications
"verify_peer" => false,
"verify_peer_name" => false
]]);
$res = stream_socket_client("ssl://{$host}:{$port}", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $contextCreate);
$context = stream_context_get_params($res);
return openssl_x509_fingerprint($context["options"]["ssl"]["peer_certificate"], "sha256");
}
function main() {
global $argc, $argv;
$hashString = null;
if ($argc == 1) {
echo "Usage: {$argv[0]} <host> [<port>]\n";
echo "Usage example: {$argv[0]} 1.2.3.4\n";
echo "Usage example: {$argv[0]} 1.2.3.4 443\n";
return;
}
else if ($argc == 2) { // Port number not specified
$hashString = getSHA256Digest($argv[1]);
}
else {
$hashString = getSHA256Digest($argv[1], $argv[2]);
}
echo "{$hashString}\n";
}
if (php_sapi_name() == "cli") {
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment