Skip to main content

Authentication Methods

The React Native SSH SFTP library supports two authentication methods: password-based and key-based authentication. Both methods establish a secure SSH connection to your remote server.

Password Authentication

Password authentication is the simplest method. Use SSHClient.connectWithPassword() to connect with username and password credentials.

Method Signature

Key-Based Authentication

Key-based authentication provides stronger security by using SSH key pairs. Use SSHClient.connectWithKey() to connect with a private key.

Method Signature

KeyPair Interface

The KeyPair interface represents an SSH key pair used for authentication. It contains the private key and optionally the public key and passphrase.

KeyPair Properties

string
required
The private key in PEM format (e.g., -----BEGIN RSA PRIVATE KEY-----)
string
The public key in OpenSSH format (e.g., ssh-rsa AAAAB3NzaC1yc2EA...)
string
The passphrase used to encrypt/decrypt the private key

Generating Key Pairs

You can generate SSH key pairs programmatically using the generateKeyPair() static method.

Method Signature

Return Type

Getting Key Details

Use getKeyDetails() to retrieve information about an SSH private key, including its type and size.

Method Signature

Return Type

Security Best Practices

Never hardcode credentials or private keys in your application code. Always load them from secure storage.

Recommendations

  1. Use Key-Based Authentication: Prefer key-based authentication over passwords for better security
  2. Protect Private Keys: Store private keys in secure storage (e.g., iOS Keychain, Android Keystore)
  3. Use Passphrases: Always encrypt private keys with strong passphrases
  4. Rotate Keys Regularly: Implement a key rotation policy for long-lived applications
  5. Validate Connections: Always handle connection errors and validate the server’s identity when possible

Secure Storage Example

The example above uses expo-secure-store, but you should use the appropriate secure storage solution for your platform and framework.

Authentication Error Handling

Both authentication methods return Promises that reject on error. Always implement proper error handling:
See Error Handling for more details on handling connection and authentication errors.
Last modified on July 4, 2026