Dart
Dart
SFTP Authentication using X.509 Certificates
See more SFTP Examples
Demonstrates how to authenticate with an SSH/SFTP server using an certificate's private key.Note: See X.509v3 Certificates for SSH Authentication for more information.
Chilkat Dart Downloads
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 sftp = CkSFtp();
final hostname = 'sftp.example.com';
final port = 22;
try {
sftp.connect(hostname, port);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Load the cert + private key from a .pfx.
// Note: Chilkat provides methods for loading certs and private keys from many sources, including smart cards and USB tokens (HSM's)
final cert = CkCert();
try {
cert.loadPfxFile('qa_data/pfx/example.pfx', 'pfx_password');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Get the cert's private key (as PEM) to be used for SSH authentication.
// (The public key is installed on the server.)
final String privKeyPem;
try {
privKeyPem = cert.getPrivateKeyPem();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final key = CkSshKey();
// Load a private key from a PEM string:
try {
key.fromOpenSshPrivateKey(privKeyPem);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Authenticate with the SSH server.
try {
sftp.authenticatePk('myLogin', key);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Public-Key Authentication Successful!');
}