Repository navigation
OnixLabs.Security.Cryptography.NamedPrivateKey
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
|
public readonly partial record struct NamedPrivateKey :
ICryptoPrimitive<NamedPrivateKey>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
AsReadOnlySpanandAsReadOnlyMemoryexpose raw private key material. Treat the returned bytes with appropriate care and avoid holding references beyond their required lifetime.
Note Because
PrivateKeyis a reference-typed wrapper around key material, it can be disposed. If the underlyingPrivateKeyhas been disposed whenAsReadOnlySpan,AsReadOnlyMemory,ToString, orToString(IFormatProvider)is called, those methods throwObjectDisposedException. WhenPrivateKeyisnull, the methods return empty sequences or an empty-byte-encoded string rather than throwingNullReferenceException.
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>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>"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| Constructor | Description |
|---|---|
NamedPrivateKey(PrivateKey, string) |
Initializes a new instance with a private key value and a string algorithm name. |
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. |
| 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. |
public PrivateKey PrivateKey { get; }Gets the underlying PrivateKey value associated with this instance.
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".
| 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. |
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. |
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. |
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. |
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. |
| Operator | Description |
|---|---|
operator == |
Determines whether two NamedPrivateKey values are equal. |
operator != |
Determines whether two NamedPrivateKey values are not equal. |
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>.
public static bool operator !=(NamedPrivateKey? left, NamedPrivateKey? right)Determines whether two NamedPrivateKey values are not equal. The negation of operator ==.
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions