Repository navigation
OnixLabs.Security.Cryptography.NamedPublicKey
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
|
public readonly partial record struct NamedPublicKey :
ICryptoPrimitive<NamedPublicKey>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
AsReadOnlySpanandAsReadOnlyMemorydelegate to the underlyingPublicKey. WhenPublicKeyisnull, the methods return empty sequences rather than throwingNullReferenceException.
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.
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>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;
NamedPublicKey a = new NamedPublicKey(key, "RSA");
NamedPublicKey b = new NamedPublicKey(key, "RSA");
Console.WriteLine(a == b); // True — same key bytes and same algorithm name| Constructor | Description |
|---|---|
NamedPublicKey(PublicKey, string) |
Initializes a new instance with a public key value and a string algorithm name. |
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. |
| 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. |
public PublicKey PublicKey { get; }Gets the underlying PublicKey value associated with this instance.
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".
| 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 PublicKey. Returns ReadOnlyMemory<byte>.Empty when PublicKey is null.
Returns — A ReadOnlyMemory<byte> view of the public key bytes.
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.
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....
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>.
| Operator | Description |
|---|---|
operator == |
Determines whether two NamedPublicKey values are equal. |
operator != |
Determines whether two NamedPublicKey values are not equal. |
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>.
public static bool operator !=(NamedPublicKey? left, NamedPublicKey? right)Determines whether two NamedPublicKey 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