Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSftp
    Integer iPort
    Boolean iIncludeKeyType
    Boolean iIncludeHashName
    String sFingerprint
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatSFtp)) To hoSftp
    If (Not(IsComObjectCreated(hoSftp))) Begin
        Send CreateComObject of hoSftp
    End

    //  Step 1: Connect the SSH transport.  Port 22 is the usual port.
    Move 22 To iPort
    Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Get the SHA256 fingerprint, including the key type and hash name.
    Move True To iIncludeKeyType
    Move True To iIncludeHashName
    Get ComGetHostKeyFP Of hoSftp "SHA256" iIncludeKeyType iIncludeHashName To sFingerprint
    Get ComLastMethodSuccess Of hoSftp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Host key fingerprint: " sFingerprint

    Send ComDisconnect To hoSftp


End_Procedure