Skip to content

OnixLabs.Security.Cryptography.NamedPrivateKey

matthew edited this page May 27, 2026 · 2 revisions

NamedPrivateKey Struct

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

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

Definition

public readonly partial record struct NamedPrivateKey :
    ICryptoPrimitive<NamedPrivateKey>

Remarks

NamedPrivateKey solves the same self-describing problem for asymmetric keys that NamedHash solves for hashes: a raw PrivateKey alone does not carry information about the algorithm that owns it. By bundling the PrivateKey together with an AlgorithmName string, NamedPrivateKey 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, NamedPrivateKey uses structural equality: two instances are equal when both their PrivateKey and AlgorithmName are equal. The compiler-generated ==, !=, Equals, GetHashCode, and Deconstruct members all follow from this.

Warning The binary-convertible methods AsReadOnlySpan and AsReadOnlyMemory expose raw private key material. Treat the returned bytes with appropriate care and avoid holding references beyond their required lifetime.

Note Because PrivateKey is a reference-typed wrapper around key material, it can be disposed. If the underlying PrivateKey has been disposed when AsReadOnlySpan, AsReadOnlyMemory, ToString, or ToString(IFormatProvider) is called, those methods throw ObjectDisposedException. When PrivateKey is null, the methods return empty sequences or an empty-byte-encoded string rather than throwing NullReferenceException.

Examples

Creating a NamedPrivateKey

using OnixLabs.Security.Cryptography;

// Assume privateKey was obtained from a key-generation method
PrivateKey privateKey = /* ... */;

NamedPrivateKey named = new NamedPrivateKey(privateKey, "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;

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

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

Constructors

Constructor Description
NamedPrivateKey(PrivateKey, string) Initializes a new instance with a private key value and a string algorithm name.

NamedPrivateKey(PrivateKey, string)

public NamedPrivateKey(PrivateKey privateKey, string algorithmName)

Initializes a new instance of the NamedPrivateKey struct.

Parameter Type Description
privateKey PrivateKey The underlying private key value.
algorithmName string The name of the key algorithm that was used to produce the associated private key.

Exceptions

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

Properties

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

PrivateKey

public PrivateKey PrivateKey { get; }

Gets the underlying PrivateKey value associated with this instance.

AlgorithmName

public string AlgorithmName { get; }

Gets the name of the key algorithm that was used to produce the associated private 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 PrivateKey. Returns ReadOnlyMemory<byte>.Empty when PrivateKey is null.

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

Exceptions

Exception Condition
ObjectDisposedException The underlying PrivateKey has been disposed.

AsReadOnlySpan()

public ReadOnlySpan<byte> AsReadOnlySpan()

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

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

Exceptions

Exception Condition
ObjectDisposedException The underlying PrivateKey has been disposed.

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 RSA:3082....

Exceptions

Exception Condition
ObjectDisposedException The underlying PrivateKey has been disposed.

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>.

Exceptions

Exception Condition
ObjectDisposedException The underlying PrivateKey has been disposed.

Operators

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

operator ==

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

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

operator !=

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

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

See Also

ONIXLabs .NET Library


Clone this wiki locally