Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get the SSH Server Host Key Fingerprint
See more SSH Examples
Demonstrates the Chilkat Ssh.GetHostKeyFP method, which returns the connected server's host-key fingerprint. The first argument is the hash algorithm (for example SHA256 or MD5), the second controls whether the key type is included, and the third controls whether the hash name is included. The digest is Base64 without trailing padding.
Background: The host-key fingerprint identifies the server. Comparing it against a known-good value is how a client implements host-key pinning — detecting man-in-the-middle attempts or a changed server key.
SHA256 is the modern default; MD5 fingerprints are still seen in older tooling but are weaker.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Ssh;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
ssh: TSsh;
sshPort: Integer;
includeKeyType: Boolean;
includeHashName: Boolean;
fingerprint: string;
begin
success := False;
// Demonstrates the Ssh.GetHostKeyFP method, which returns the server's host-key fingerprint.
// The 1st argument is the hash algorithm, the 2nd controls whether the key type is included,
// and the 3rd controls whether the hash name is included.
ssh := TSsh.Create;
// Connect to the SSH server. Port 22 is the usual SSH port.
sshPort := 22;
success := ssh.Connect('ssh.example.com',sshPort);
if (success = False) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
// Get the SHA256 fingerprint, including the key type and hash name in the result.
includeKeyType := True;
includeHashName := True;
fingerprint := ssh.GetHostKeyFP('SHA256',includeKeyType,includeHashName);
if (ssh.LastMethodSuccess = False) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
WriteLn('Host key fingerprint: ' + fingerprint);
ssh.Disconnect();
ssh.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.