Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Demonstrates generating a fingerprint for an SSH key.

    CkSshKey key = new CkSshKey();

    //  Set the password before importing an encrypted private key.  This should come from a secure
    //  source rather than being hard-coded.
    key.put_Password("myKeyPassword");

    //  LoadText is a convenience method that reads any text file into a string.  It does not itself
    //  load the key.
    String keyStr = key.loadText("qa_data/privkey_openssh_encrypted.pem");
    if (key.get_LastMethodSuccess() == false) {
        Log.i(TAG, key.lastErrorText());
        return;
        }

    success = key.FromOpenSshPrivateKey(keyStr);
    if (success == false) {
        Log.i(TAG, key.lastErrorText());
        return;
        }

    String fingerprint = key.genFingerprint();
    if (key.get_LastMethodSuccess() == false) {
        Log.i(TAG, key.lastErrorText());
        return;
        }

    Log.i(TAG, fingerprint);
    //  For example:  ssh-rsa 2048 d0:5f:f7:d6:49:60:7b:50:19:f4:41:59:d4:1f:61:7a

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}