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

React Native
import { SFtp, SshKey, StringBuilder } from '@chilkat/react-native'

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

  const sbSshCert = new StringBuilder();
  try {
    sbSshCert.loadFile('qa_data/sshCert/user_ecdsa_key-cert.pub', 'utf-8');
  } catch {
    console.log('Failed to load user_ecdsa_key-cert.pub');
    return;
  }

  const sbPrivKey = new StringBuilder();
  try {
    sbPrivKey.loadFile('qa_data/sshKeys/user_ecdsa_key', 'utf-8');
  } catch {
    console.log('Failed to load user_ecdsa_key');
    return;
  }

  const key = new SshKey();
  // Provide the password if the user_ecdsa_key is stored in an encrypted format.
  key.password = 'secret';
  try {
    key.fromOpenSshPrivateKey(sbPrivKey.getAsString());
  } catch {
    console.log(key.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());

  const sftp = new SFtp();

  const hostname = 'sftp.example.com';
  const port = 22;
  try {
    await sftp.connectAsync(hostname, port);
  } catch {
    console.log(sftp.lastErrorText);
    return;
  }

  try {
    await sftp.authenticatePkAsync('myLogin', key);
  } catch {
    console.log(sftp.lastErrorText);
    return;
  }

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