Sample code for 30+ languages & platforms
Rust 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 Rust Downloads

Rust

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

let sb_ssh_cert = chilkat::StringBuilder::new();
if sb_ssh_cert.load_file("qa_data/sshCert/user_ecdsa_key-cert.pub", "utf-8").is_err() {
    println!("Failed to load user_ecdsa_key-cert.pub");
    return;
}

let sb_priv_key = chilkat::StringBuilder::new();
if sb_priv_key.load_file("qa_data/sshKeys/user_ecdsa_key", "utf-8").is_err() {
    println!("Failed to load user_ecdsa_key");
    return;
}

let key = chilkat::SshKey::new();
// Provide the password if the user_ecdsa_key is stored in an encrypted format.
key.set_password("secret");
if key.from_open_ssh_private_key(&sb_priv_key.get_as_string().unwrap_or_default()).is_err() {
    println!("{}", key.last_error_text());
    return;
}

// Indicate that the SSH certificate is to be used for authentication.
// The UseSshCertificate method was added in Chilkat v11.0.0
let _ = key.use_ssh_certificate(&sb_ssh_cert.get_as_string().unwrap_or_default());

let sftp = chilkat::SFtp::new();

let hostname = "sftp.example.com".to_string();
let port = 22;
if sftp.connect(&hostname, port).is_err() {
    println!("{}", sftp.last_error_text());
    return;
}

if sftp.authenticate_pk("myLogin", &key).is_err() {
    println!("{}", sftp.last_error_text());
    return;
}

println!("Public-Key Authentication using an SSH Certificate was Successful!");