Sample code for 30+ languages & platforms
SQL Server

SSH Key Fingerprint

See more SSH Examples

Demonstrates generating a fingerprint for an SSH key. The private key is imported into an SshKey object — setting Password first if the key file is encrypted — and GenFingerprint returns the fingerprint.

Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.

Background: A key fingerprint is a short hash that stands in for the full key, making it practical for humans to compare and for inventories to record. The everyday use is confirming that the private key you hold corresponds to a particular public key installed on a server — matching fingerprints is far easier than comparing key material. Note this fingerprints your own key; use Ssh.GetHostKeyFP to fingerprint a server's host key.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates generating a fingerprint for an SSH key.

    DECLARE @key int
    EXEC @hr = sp_OACreate 'Chilkat.SshKey', @key OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Set the password before importing an encrypted private key.  This should come from a secure
    --  source rather than being hard-coded.
    EXEC sp_OASetProperty @key, 'Password', 'myKeyPassword'

    --  LoadText is a convenience method that reads any text file into a string.  It does not itself
    --  load the key.
    DECLARE @keyStr nvarchar(4000)
    EXEC sp_OAMethod @key, 'LoadText', @keyStr OUT, 'qa_data/privkey_openssh_encrypted.pem'
    EXEC sp_OAGetProperty @key, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @key
        RETURN
      END

    EXEC sp_OAMethod @key, 'FromOpenSshPrivateKey', @success OUT, @keyStr
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @key
        RETURN
      END

    DECLARE @fingerprint nvarchar(4000)
    EXEC sp_OAMethod @key, 'GenFingerprint', @fingerprint OUT
    EXEC sp_OAGetProperty @key, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @key
        RETURN
      END

    PRINT @fingerprint
    --  For example:  ssh-rsa 2048 d0:5f:f7:d6:49:60:7b:50:19:f4:41:59:d4:1f:61:7a

    EXEC @hr = sp_OADestroy @key


END
GO