Zig Requires Chilkat v11.0.0+
Zig
SFTP Authentication using an SSH Certificate
See more SFTP Examples
Demonstrates how to SFTP authenticate using an SSH certificate.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const sb_ssh_cert = try chilkat.StringBuilder.init();
defer sb_ssh_cert.deinit();
sb_ssh_cert.loadFile("qa_data/sshCert/user_ecdsa_key-cert.pub", "utf-8") catch {
std.debug.print("Failed to load user_ecdsa_key-cert.pub\n", .{});
return;
};
const sb_priv_key = try chilkat.StringBuilder.init();
defer sb_priv_key.deinit();
sb_priv_key.loadFile("qa_data/sshKeys/user_ecdsa_key", "utf-8") catch {
std.debug.print("Failed to load user_ecdsa_key\n", .{});
return;
};
const key = try chilkat.SshKey.init();
defer key.deinit();
// Provide the password if the user_ecdsa_key is stored in an encrypted format.
key.setPassword("secret");
key.fromOpenSshPrivateKey(try sb_priv_key.getAsString(alloc)) catch {
std.debug.print("{s}\n", .{try key.getLastErrorText(alloc)});
return;
};
// Indicate that the SSH certificate is to be used for authentication.
// The UseSshCertificate method was added in Chilkat v11.0.0
key.useSshCertificate(try sb_ssh_cert.getAsString(alloc)) catch {};
const sftp = try chilkat.SFtp.init();
defer sftp.deinit();
const hostname = "sftp.example.com";
const port = 22;
sftp.connect(hostname, port) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
sftp.authenticatePk("myLogin", key) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
std.debug.print("Public-Key Authentication using an SSH Certificate was Successful!\n", .{});
}