Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

SFTP Authentication using an SSH Certificate

See more SFTP Examples

Demonstrates how to SFTP authenticate using an SSH certificate.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final sbSshCert = CkStringBuilder();
  try {
    sbSshCert.loadFile('qa_data/sshCert/user_ecdsa_key-cert.pub', 'utf-8');
  } on ChilkatException {
    print('Failed to load user_ecdsa_key-cert.pub');
    return;
  }

  final sbPrivKey = CkStringBuilder();
  try {
    sbPrivKey.loadFile('qa_data/sshKeys/user_ecdsa_key', 'utf-8');
  } on ChilkatException {
    print('Failed to load user_ecdsa_key');
    return;
  }

  final key = CkSshKey();
  // Provide the password if the user_ecdsa_key is stored in an encrypted format.
  key.password = 'secret';
  try {
    key.fromOpenSshPrivateKey(sbPrivKey.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Indicate that the SSH certificate is to be used for authentication.
  // The UseSshCertificate method was added in Chilkat v11.0.0
  key.useSshCertificate(sbSshCert.getAsString());

  final sftp = CkSFtp();

  final hostname = 'sftp.example.com';
  final port = 22;
  try {
    sftp.connect(hostname, port);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    sftp.authenticatePk('myLogin', key);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Public-Key Authentication using an SSH Certificate was Successful!');
}