Skip to content

OnixLabs.Security.Cryptography.NamedPublicKey

matthew edited this page May 27, 2026 · 2 revisions

NamedPublicKey Struct

Represents a named cryptographic public key: a PublicKey value paired with the name of the algorithm that produced it.

Namespace OnixLabs.Security.Cryptography
Assembly OnixLabs.Security.Cryptography.dll (NuGet)
Inheritance object → ValueType → NamedPublicKey
Implements ICryptoPrimitive<NamedPublicKey>, IValueEquatable<NamedPublicKey>, IEquatable<NamedPublicKey>, IBinaryConvertible

Definition

public readonly partial record struct NamedPublicKey :
    ICryptoPrimitive<NamedPublicKey>

Remarks

NamedPublicKey solves the same self-describing problem for asymmetric keys that NamedHash solves for hashes: a raw PublicKey alone does not carry information about the algorithm that owns it. By bundling the PublicKey together with an AlgorithmName string, NamedPublicKey creates a self-contained value that records which key scheme (for example, "RSA" or "ECDsa") the key material belongs to.

The string representation produced by ToString() uses the format:

<AlgorithmName>:<hex-encoded key bytes>

An alternative encoding (Base58, Base64, etc.) can be obtained via ToString(IFormatProvider).

Value semantics — as a record struct, NamedPublicKey uses structural equality: two instances are equal when both their PublicKey and AlgorithmName are equal. The compiler-generated ==, !=, Equals, GetHashCode, and Deconstruct members all follow from this.

Note The binary-convertible methods AsReadOnlySpan and AsReadOnlyMemory delegate to the underlying PublicKey. When PublicKey is null, the methods return empty sequences rather than throwing NullReferenceException.

NamedPublicKey is the public-key counterpart of NamedPrivateKey. The two types share an identical public API surface and are typically used together when a key pair needs to travel with its algorithm identity.

Examples

Creating a NamedPublicKey

using OnixLabs.Security.Cryptography;

// Assume publicKey was obtained from a key-generation or export method
PublicKey publicKey = /* ... */;

NamedPublicKey named = new NamedPublicKey(publicKey, "ECDsa");

Console.WriteLine(named.AlgorithmName); // ECDsa
Console.WriteLine(named);               // ECDsa:<hex>

Encoding with a custom format provider

using OnixLabs.Core.Text;
using OnixLabs.Security.Cryptography;

// Encode the key bytes as Base58 instead of hex
string base58Form = named.ToString(Base58FormatProvider.Invariant);
// "ECDsa:<base58>"

Value equality

using OnixLabs.Security.Cryptography;

NamedPublicKey a = new NamedPublicKey(key, "RSA");
NamedPublicKey b = new NamedPublicKey(key, "RSA");

Console.WriteLine(a == b); // True — same key bytes and same algorithm name

Constructors

Constructor Description
NamedPublicKey(PublicKey, string) Initializes a new instance with a public key value and a string algorithm name.

NamedPublicKey(PublicKey, string)

public NamedPublicKey(PublicKey publicKey, string algorithmName)

Initializes a new instance of the NamedPublicKey struct.

Parameter Type Description
publicKey PublicKey The underlying public key value.
algorithmName string The name of the key algorithm that was used to produce the associated public key.

Exceptions

Exception Condition
ArgumentNullException publicKey is null.
ArgumentException algorithmName is null, empty, or consists only of whitespace.

Properties

Property Type Description
PublicKey PublicKey Gets the underlying public key value.
AlgorithmName string Gets the name of the key algorithm that produced the public key.

PublicKey

public PublicKey PublicKey { get; }

Gets the underlying PublicKey value associated with this instance.

AlgorithmName

public string AlgorithmName { get; }

Gets the name of the key algorithm that was used to produce the associated public key. This is a non-null, non-empty, non-whitespace string that identifies the algorithm — for example, "RSA" or "ECDsa".

Methods

Instance Methods

Method Returns Description
AsReadOnlyMemory() ReadOnlyMemory<byte> Returns the underlying key bytes as a ReadOnlyMemory<byte>.
AsReadOnlySpan() ReadOnlySpan<byte> Returns the underlying key bytes as a ReadOnlySpan<byte>.
ToString() string Returns the canonical <AlgorithmName>:<hex> string representation.
ToString(IFormatProvider) string Returns the <AlgorithmName>:<encoded> string using the given format provider to encode the key bytes.

AsReadOnlyMemory()

public ReadOnlyMemory<byte> AsReadOnlyMemory()

Returns the underlying key bytes as a ReadOnlyMemory<byte>. Delegates to the wrapped PublicKey. Returns ReadOnlyMemory<byte>.Empty when PublicKey is null.

Returns — A ReadOnlyMemory<byte> view of the public key bytes.


AsReadOnlySpan()

public ReadOnlySpan<byte> AsReadOnlySpan()

Returns the underlying key bytes as a ReadOnlySpan<byte>. Delegates to the wrapped PublicKey. Returns ReadOnlySpan<byte>.Empty when PublicKey is null.

Returns — A ReadOnlySpan<byte> view of the public key bytes.


ToString()

public override string ToString()

Returns the canonical string representation of this instance, using Base16 (lowercase hex) encoding for the key bytes.

Returns — A string of the form <AlgorithmName>:<hex-encoded-key>, for example ECDsa:0482....


ToString(IFormatProvider)

public string ToString(IFormatProvider provider)

Returns a string representation of this instance, encoding the key bytes using the specified format provider (for example, a Base58 or Base64 provider from OnixLabs.Core.Text).

Parameter Type Description
provider IFormatProvider The format provider that controls how the key bytes are encoded.

Returns — A string of the form <AlgorithmName>:<encoded-key>.

Operators

Operator Description
operator == Determines whether two NamedPublicKey values are equal.
operator != Determines whether two NamedPublicKey values are not equal.

operator ==

public static bool operator ==(NamedPublicKey? left, NamedPublicKey? right)

Determines whether two NamedPublicKey values are equal. Two values are equal when both their PublicKey and AlgorithmName are equal. Synthesised by the record struct compiler feature and required by IValueEquatable<NamedPublicKey>.

operator !=

public static bool operator !=(NamedPublicKey? left, NamedPublicKey? right)

Determines whether two NamedPublicKey values are not equal. The negation of operator ==.

See Also

ONIXLabs .NET Library


Clone this wiki locally