Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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.i = CkSsh::ckCreate()
    If ssh.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Connect to the SSH server.  Port 22 is the usual SSH port.
    sshPort.i = 22
    success = CkSsh::ckConnect(ssh,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  Get the SHA256 fingerprint, including the key type and hash name in the result.
    includeKeyType.i = 1
    includeHashName.i = 1
    fingerprint.s = CkSsh::ckGetHostKeyFP(ssh,"SHA256",includeKeyType,includeHashName)
    If CkSsh::ckLastMethodSuccess(ssh) = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    Debug "Host key fingerprint: " + fingerprint

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)


    ProcedureReturn
EndProcedure