Table of Contents

Class SshPublicKey

Namespace
SshSharp
Assembly
SshSharp.dll

An SSH public key: what a .pub file, an authorized_keys line or a known_hosts line holds. Read one with Parse(string) (a line of text) or FromBlob(ReadOnlyMemory<byte>, string?) (the SSH wire format), and get one from a key the library already has: GetPublicKey(), GetPublicKey(), GetPublicKey(), GetPublicKey(). Key types: Ed25519, ECDSA (nistp256/384/521), RSA, and the FIDO security-key types sk-ssh-ed25519@openssh.com and sk-ecdsa-sha2-nistp256@openssh.com.

public sealed class SshPublicKey : IEquatable<SshPublicKey>
Inheritance
SshPublicKey
Implements
Inherited Members

Remarks

Immutable and thread-safe. Two keys are equal when their key bytes are (Blob, which includes the key type); the Comment is not part of the key and is ignored.

Parsing checks what verifying a signature relies on, as for a server's host key: an ECDSA point must be on its curve, an RSA key's modulus and exponent must be sane, and its modulus at most 16384 bits (the most OpenSSH accepts). Hostile input fails with FormatException.

Properties

Blob

The key in SSH wire format (RFC 4253 section 6.6): what the base64 field of a .pub line decodes to.

Comment

The comment that came with the key (often user@host), or null (never empty). It is not part of the key.

Fingerprint

The OpenSSH SHA-256 fingerprint (SHA256: and unpadded base64), as ssh-keygen -lf prints it.

KeySizeBits

The key size in bits: 256 for Ed25519, the curve size for ECDSA (256, 384, 521), the modulus size for RSA.

KeyType

The key type name, for example ssh-ed25519, ecdsa-sha2-nistp256 or ssh-rsa, as KeyType and KeyType name it. An RSA key's type is ssh-rsa whichever signature algorithm it signs with.

Methods

Equals(SshPublicKey?)

Whether other is the same key: the same key bytes. The comment is ignored.

Equals(object?)

Determines whether the specified object is equal to the current object.

FromBlob(ReadOnlyMemory<byte>, string?)

Reads a key in SSH wire format, as PublicKey and the other blob members hold it.

GetFingerprint(HashAlgorithmName)

The fingerprint with hashAlgorithm, as ssh-keygen -l -E prints it: SHA256 gives Fingerprint, and MD5 the legacy MD5:aa:bb:... form that older tools and inventories still show.

GetHashCode()

Serves as the default hash function.

Parse(string)

Parses one line in the OpenSSH public key format, <type> <base64> [comment]: a .pub file, the output of ssh-keygen -y, or an authorized_keys line without options. White space around the line is ignored.

ToAuthorizedKeysLine()

The key as an authorized_keys or .pub line: type, base64 and the comment, if any.

ToString()

The key as ToAuthorizedKeysLine() writes it.

TryParse(string?, out SshPublicKey?)

Parses a line as Parse(string) does, without throwing.

VerifySignature(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Verifies an SSH signature made by this key's private key over data. The signature is in the SSH encoding (string algorithm || string signature, RFC 4253 section 6.6), as agents and SshKeySigner produce it, and its algorithm must be one the key type signs with: ssh-ed25519, the key's ecdsa-sha2-*, or rsa-sha2-256 and rsa-sha2-512 for an RSA key (SHA-1 ssh-rsa signatures are refused).

Operators

operator ==(SshPublicKey?, SshPublicKey?)

Whether two keys are the same key (see Equals(SshPublicKey?)).

operator !=(SshPublicKey?, SshPublicKey?)

Whether two keys are different keys (see Equals(SshPublicKey?)).