Table of Contents

Method SaveAsync

Namespace
SshSharp
Assembly
SshSharp.dll

SaveAsync(string, string?, CancellationToken)

Writes the key as ssh-keygen does: the private key to path (ToOpenSshPrivateKey(string?, int)), readable only by the current user (mode 0600 on Unix, an owner-only ACL on Windows), and the public key to path.pub. Neither file may exist yet: an existing key is never overwritten.

public Task SaveAsync(string path, string? passphrase = null, CancellationToken cancellationToken = default)

Parameters

path string

The private key file, for example ~/.ssh/id_ed25519.

passphrase string

The passphrase to encrypt with; null or empty for an unencrypted key.

cancellationToken CancellationToken

Cancels writing.

Returns

Task

Exceptions

IOException

One of the files exists already, or could not be written.

NotSupportedException

The private key is not available: a key held by an agent or an external signer, or one paired with a certificate (export the key it was made from).

SaveAsync(string, string?, int, CancellationToken)

Writes the key as SaveAsync(string, string?, CancellationToken) does, encrypted with kdfRounds rounds of bcrypt_pbkdf (ssh-keygen -a).

public Task SaveAsync(string path, string? passphrase, int kdfRounds, CancellationToken cancellationToken = default)

Parameters

path string

The private key file, for example ~/.ssh/id_ed25519.

passphrase string

The passphrase to encrypt with; null or empty for an unencrypted key.

kdfRounds int

bcrypt_pbkdf rounds: 16 is ssh-keygen's default; more make guessing the passphrase slower, and loading the key too. From 1 to 65536.

cancellationToken CancellationToken

Cancels writing.

Returns

Task

Exceptions

ArgumentOutOfRangeException

kdfRounds is out of range.

IOException

One of the files exists already, or could not be written.

NotSupportedException

The private key is not available: a key held by an agent or an external signer, or one paired with a certificate (export the key it was made from).

SaveAsync(string, SshPrivateKeyFormat, string?, CancellationToken)

Writes the key in format: the private key to path (ToOpenSshPrivateKey(string?, int), ToPkcs8PrivateKey(string?) or ToPuttyPrivateKey(string?)), readable only by the current user (mode 0600 on Unix, an owner-only ACL on Windows), and the public key to path.pub as an authorized_keys line, for every format. Neither file may exist yet: an existing key is never overwritten.

public Task SaveAsync(string path, SshPrivateKeyFormat format, string? passphrase = null, CancellationToken cancellationToken = default)

Parameters

path string

The private key file, for example ~/.ssh/id_ed25519, or id_ed25519.ppk for PuTTY.

format SshPrivateKeyFormat

The file format.

passphrase string

The passphrase to encrypt with; null or empty for an unencrypted key.

cancellationToken CancellationToken

Cancels writing.

Returns

Task

Exceptions

ArgumentOutOfRangeException

format is not a defined format.

IOException

One of the files exists already, or could not be written.

NotSupportedException

The private key is not available (a key held by an agent or an external signer, or one paired with a certificate), or the format cannot hold this key: see ToPkcs8PrivateKey(string?) and ToPuttyPrivateKey(string?).