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

Lua
local chilkat = require("chilkat")

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

local sbSshCert = chilkat.StringBuilder.new()
local success = sbSshCert:load_file("qa_data/sshCert/user_ecdsa_key-cert.pub", "utf-8")
if not success then
    print("Failed to load user_ecdsa_key-cert.pub")
    return
end

local sbPrivKey = chilkat.StringBuilder.new()
success = sbPrivKey:load_file("qa_data/sshKeys/user_ecdsa_key", "utf-8")
if not success then
    print("Failed to load user_ecdsa_key")
    return
end

local key = chilkat.SshKey.new()
-- Provide the password if the user_ecdsa_key is stored in an encrypted format.
key.password = "secret"
success = key:from_open_ssh_private_key(sbPrivKey:get_as_string())
if not success then
    print(key.last_error_text)
    return
end

-- Indicate that the SSH certificate is to be used for authentication.
key:use_ssh_certificate(sbSshCert:get_as_string())

local sftp = chilkat.SFtp.new()

local hostname = "sftp.example.com"
local port = 22
success = sftp:connect(hostname, port)
if not success then
    print(sftp.last_error_text)
    return
end

success = sftp:authenticate_pk("myLogin", key)
if not success then
    print(sftp.last_error_text)
    return
end

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