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
.publine 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), asssh-keygen -lfprints 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-nistp256orssh-rsa, as KeyType and KeyType name it. An RSA key's type isssh-rsawhichever signature algorithm it signs with.
Methods
- Equals(SshPublicKey?)
Whether
otheris 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, asssh-keygen -l -Eprints it: SHA256 gives Fingerprint, and MD5 the legacyMD5: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.pubfile, the output ofssh-keygen -y, or anauthorized_keysline without options. White space around the line is ignored.
- ToAuthorizedKeysLine()
The key as an
authorized_keysor.publine: 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'secdsa-sha2-*, orrsa-sha2-256andrsa-sha2-512for an RSA key (SHA-1ssh-rsasignatures 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?)).