Sample code for 30+ languages & platforms
PureBasic

Get the SFTP Server Host Key Fingerprint

See more SFTP Examples

Demonstrates the Chilkat SFtp.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.

Background: The host-key fingerprint identifies the server. Comparing it against a known-good value is how a client implements host-key pinning — detecting a man-in-the-middle attempt or a changed server key before trusting the connection with credentials or files. SHA256 is the modern default; MD5 fingerprints still appear in older tooling but are weaker.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSFtp.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the SFtp.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.

    sftp.i = CkSFtp::ckCreate()
    If sftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Step 1: Connect the SSH transport.  Port 22 is the usual port.
    port.i = 22
    success = CkSFtp::ckConnect(sftp,"sftp.example.com",port)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

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

    Debug "Host key fingerprint: " + fingerprint

    CkSFtp::ckDisconnect(sftp)


    CkSFtp::ckDispose(sftp)


    ProcedureReturn
EndProcedure