Repository navigation
OnixLabs.Security.Cryptography.EcdhPublicKey
Represents an EC Diffie-Hellman cryptographic public key used for shared-secret key agreement.
| Namespace | OnixLabs.Security.Cryptography |
| Assembly | OnixLabs.Security.Cryptography.dll (NuGet) |
| Inheritance |
object → PublicKey → EcdhPublicKey
|
| Implements |
IEcdhPublicKey, ISpanParsable<EcdhPublicKey>, IPublicKeyImportable<EcdhPublicKey>, IPublicKeyExportable, IBinaryConvertible
|
public sealed partial class EcdhPublicKey : PublicKey, IEcdhPublicKey, ISpanParsable<EcdhPublicKey>EcdhPublicKey is the concrete public-key type for the Elliptic Curve Diffie-Hellman (ECDH) key-agreement protocol. An ECDH public key is the value that is shared with the remote party so that both sides can independently derive the same shared secret via EcdhPrivateKey.DeriveSharedSecret. EcdhPublicKey is not used for signature verification; for that purpose, see EcdsaPublicKey or EddsaPublicKey.
Obtaining instances — The standard way to obtain an EcdhPublicKey is by calling GetPublicKey() on the corresponding EcdhPrivateKey. An existing SubjectPublicKeyInfo (X.509 DER) byte array can be loaded via Import, and RFC 7468 PEM text via ImportPem. Hex- or Base64-encoded key strings can be parsed back via Parse / TryParse.
Export formats — EcdhPublicKey supports two export formats:
-
Export()— SubjectPublicKeyInfo (X.509 DER) bytes. -
ExportPem()— RFC 7468 PEM text with thePUBLIC KEYlabel.
Named key — ToNamedPublicKey() wraps this instance in a NamedPublicKey tagged with the algorithm name "ECDH".
Unlike EcdhPrivateKey, EcdhPublicKey does not implement IDisposable because it contains no sensitive material that requires secure erasure.
Equality comparisons, operators, ToString, AsReadOnlySpan, AsReadOnlyMemory, and GetHash semantics are all inherited from PublicKey and are documented there.
using OnixLabs.Security.Cryptography;
// Generate an ECDH key pair and extract the public key to share.
using EcdhPrivateKey privateKey = EcdhPrivateKey.Create();
EcdhPublicKey publicKey = privateKey.GetPublicKey();
// Export as SubjectPublicKeyInfo bytes to transmit to the remote party.
byte[] spkiBytes = publicKey.Export();
// The remote party imports the bytes and uses them to derive the shared secret.
EcdhPublicKey remoteImported = EcdhPublicKey.Import(spkiBytes);
Console.WriteLine(publicKey == remoteImported); // Trueusing OnixLabs.Security.Cryptography;
// Import from a PEM-encoded SubjectPublicKeyInfo.
EcdhPublicKey publicKey = EcdhPublicKey.ImportPem(pemText);
// Round-trip back to PEM.
string pem = publicKey.ExportPem();
Console.WriteLine(pem); // -----BEGIN PUBLIC KEY-----...using OnixLabs.Security.Cryptography;
using EcdhPrivateKey privateKey = EcdhPrivateKey.Create();
EcdhPublicKey publicKey = privateKey.GetPublicKey();
NamedPublicKey named = publicKey.ToNamedPublicKey();
Console.WriteLine(named.AlgorithmName); // ECDH
Console.WriteLine(named); // ECDH:<hex-encoded key bytes>| Constructor | Description |
|---|---|
EcdhPublicKey(ReadOnlySpan<byte>) |
Initializes a new instance from a span of bytes. |
EcdhPublicKey(ReadOnlyMemory<byte>) |
Initializes a new instance from a memory region. |
EcdhPublicKey(ReadOnlySequence<byte>) |
Initializes a new instance from a byte sequence. |
public EcdhPublicKey(ReadOnlySpan<byte> keyData)Initializes a new instance of the EcdhPublicKey class. The provided span is copied to an internal array; the span itself is not retained.
| Parameter | Type | Description |
|---|---|---|
keyData |
ReadOnlySpan<byte> |
The underlying key data of the EC Diffie-Hellman cryptographic public key. |
public EcdhPublicKey(ReadOnlyMemory<byte> keyData)Initializes a new instance of the EcdhPublicKey class. The provided memory region is copied to an internal array; the original memory is not retained.
| Parameter | Type | Description |
|---|---|---|
keyData |
ReadOnlyMemory<byte> |
The underlying key data of the EC Diffie-Hellman cryptographic public key. |
public EcdhPublicKey(ReadOnlySequence<byte> keyData)Initializes a new instance of the EcdhPublicKey class. The provided byte sequence is copied to an internal array; the original sequence is not retained.
| Parameter | Type | Description |
|---|---|---|
keyData |
ReadOnlySequence<byte> |
The underlying key data of the EC Diffie-Hellman cryptographic public key. |
| Method | Returns | Description |
|---|---|---|
Import(IBinaryConvertible) |
EcdhPublicKey |
Imports a SubjectPublicKeyInfo public key from an IBinaryConvertible value. |
Import(IBinaryConvertible, out int) |
EcdhPublicKey |
Imports a SubjectPublicKeyInfo public key from an IBinaryConvertible value and reports bytes consumed. |
Import(ReadOnlySpan<byte>) |
EcdhPublicKey |
Imports a SubjectPublicKeyInfo public key from a span of bytes. |
Import(ReadOnlySpan<byte>, out int) |
EcdhPublicKey |
Imports a SubjectPublicKeyInfo public key from a span of bytes and reports bytes consumed. |
ImportPem(ReadOnlySpan<char>) |
EcdhPublicKey |
Imports a key from RFC 7468 PEM text. |
ImportPem(ReadOnlySpan<char>, ReadOnlySpan<char>) |
EcdhPublicKey |
Imports a key from RFC 7468 PEM text encrypted with a character-based password. |
ImportPem(ReadOnlySpan<char>, ReadOnlySpan<byte>) |
EcdhPublicKey |
Imports a key from RFC 7468 PEM text encrypted with a byte-based password. |
Parse(string, IFormatProvider?) |
EcdhPublicKey |
Parses an encoded string into an EcdhPublicKey. |
Parse(ReadOnlySpan<char>, IFormatProvider?) |
EcdhPublicKey |
Parses an encoded character span into an EcdhPublicKey. |
TryParse(string?, IFormatProvider?, out EcdhPublicKey) |
bool |
Attempts to parse an encoded string into an EcdhPublicKey. |
TryParse(ReadOnlySpan<char>, IFormatProvider?, out EcdhPublicKey) |
bool |
Attempts to parse an encoded character span into an EcdhPublicKey. |
public static EcdhPublicKey Import(IBinaryConvertible data)Imports a SubjectPublicKeyInfo (X.509 DER format) public key from an IBinaryConvertible value.
| Parameter | Type | Description |
|---|---|---|
data |
IBinaryConvertible |
The binary-convertible value containing the SubjectPublicKeyInfo bytes to import. |
Returns — A new EcdhPublicKey instance.
Exceptions
| Exception | Condition |
|---|---|
CryptographicException |
data does not represent a valid ECDH SubjectPublicKeyInfo. |
public static EcdhPublicKey Import(IBinaryConvertible data, out int bytesRead)Imports a SubjectPublicKeyInfo public key from an IBinaryConvertible value and reports the number of bytes consumed.
| Parameter | Type | Description |
|---|---|---|
data |
IBinaryConvertible |
The binary-convertible value containing the SubjectPublicKeyInfo bytes to import. |
bytesRead |
out int |
When this method returns, contains the number of bytes read from data. |
Returns — A new EcdhPublicKey instance.
Exceptions
| Exception | Condition |
|---|---|
CryptographicException |
data does not represent a valid ECDH SubjectPublicKeyInfo. |
public static EcdhPublicKey Import(ReadOnlySpan<byte> data)Imports a SubjectPublicKeyInfo (X.509 DER format) public key from a span of bytes.
| Parameter | Type | Description |
|---|---|---|
data |
ReadOnlySpan<byte> |
The span containing the SubjectPublicKeyInfo bytes to import. |
Returns — A new EcdhPublicKey instance.
Exceptions
| Exception | Condition |
|---|---|
CryptographicException |
data does not represent a valid ECDH SubjectPublicKeyInfo. |
public static EcdhPublicKey Import(ReadOnlySpan<byte> data, out int bytesRead)Imports a SubjectPublicKeyInfo public key from a span of bytes and reports the number of bytes consumed.
| Parameter | Type | Description |
|---|---|---|
data |
ReadOnlySpan<byte> |
The span containing the SubjectPublicKeyInfo bytes to import. |
bytesRead |
out int |
When this method returns, contains the number of bytes read from data. |
Returns — A new EcdhPublicKey instance.
Exceptions
| Exception | Condition |
|---|---|
CryptographicException |
data does not represent a valid ECDH SubjectPublicKeyInfo. |
public static EcdhPublicKey ImportPem(ReadOnlySpan<char> data)Imports a key from RFC 7468 PEM-encoded text. Supports the PUBLIC KEY label (SubjectPublicKeyInfo).
| Parameter | Type | Description |
|---|---|---|
data |
ReadOnlySpan<char> |
The PEM-encoded character span to import. |
Returns — A new EcdhPublicKey instance.
Exceptions
| Exception | Condition |
|---|---|
CryptographicException |
The PEM data is malformed or does not represent a valid ECDH public key. |
public static EcdhPublicKey ImportPem(ReadOnlySpan<char> data, ReadOnlySpan<char> password)Imports a key from encrypted RFC 7468 PEM text using a character-based password.
| Parameter | Type | Description |
|---|---|---|
data |
ReadOnlySpan<char> |
The encrypted PEM-encoded character span to import. |
password |
ReadOnlySpan<char> |
The password used to decrypt the PEM data. |
Returns — A new EcdhPublicKey instance.
Exceptions
| Exception | Condition |
|---|---|
CryptographicException |
The PEM data is malformed, the password is incorrect, or the key is not a valid ECDH public key. |
public static EcdhPublicKey ImportPem(ReadOnlySpan<char> data, ReadOnlySpan<byte> password)Imports a key from encrypted RFC 7468 PEM text using a byte-based password.
| Parameter | Type | Description |
|---|---|---|
data |
ReadOnlySpan<char> |
The encrypted PEM-encoded character span to import. |
password |
ReadOnlySpan<byte> |
The password bytes used to decrypt the PEM data. |
Returns — A new EcdhPublicKey instance.
Exceptions
| Exception | Condition |
|---|---|
CryptographicException |
The PEM data is malformed, the password is incorrect, or the key is not a valid ECDH public key. |
public static EcdhPublicKey Parse(string value, IFormatProvider? provider = null)Parses an encoded string into an EcdhPublicKey. The default encoding is Base16 (hexadecimal, invariant culture); pass an alternative IFormatProvider (such as a Base58 or Base64 provider) to decode other formats.
| Parameter | Type | Description |
|---|---|---|
value |
string |
The encoded string representation of the key. |
provider |
IFormatProvider? |
An optional format provider that controls decoding. Defaults to Base16 invariant if null. |
Returns — A new EcdhPublicKey instance decoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not a valid encoded representation of an ECDH public key. |
public static EcdhPublicKey Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)Parses an encoded character span into an EcdhPublicKey. The default encoding is Base16; pass an alternative IFormatProvider to decode other formats.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The encoded character span representation of the key. |
provider |
IFormatProvider? |
An optional format provider that controls decoding. Defaults to Base16 invariant if null. |
Returns — A new EcdhPublicKey instance decoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not a valid encoded representation of an ECDH public key. |
public static bool TryParse(string? value, IFormatProvider? provider, out EcdhPublicKey result)Attempts to parse an encoded string into an EcdhPublicKey without throwing on failure.
| Parameter | Type | Description |
|---|---|---|
value |
string? |
The encoded string representation of the key. |
provider |
IFormatProvider? |
An optional format provider that controls decoding. |
result |
out EcdhPublicKey |
When this method returns, contains the parsed EcdhPublicKey if successful, or a default instance if unsuccessful. |
Returns — true if value was parsed successfully; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out EcdhPublicKey result)Attempts to parse an encoded character span into an EcdhPublicKey without throwing on failure.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The encoded character span representation of the key. |
provider |
IFormatProvider? |
An optional format provider that controls decoding. |
result |
out EcdhPublicKey |
When this method returns, contains the parsed EcdhPublicKey if successful, or a default instance if unsuccessful. |
Returns — true if value was parsed successfully; otherwise, false.
| Method | Returns | Description |
|---|---|---|
Export() |
byte[] |
Exports the key as SubjectPublicKeyInfo (X.509 DER) bytes. |
ExportPem() |
string |
Exports the key as RFC 7468 PEM text with the PUBLIC KEY label. |
ToNamedPublicKey() |
NamedPublicKey |
Creates a NamedPublicKey tagging this instance with the algorithm name "ECDH". |
public byte[] Export()Exports the key as SubjectPublicKeyInfo bytes in X.509 DER format. This is the standard wire format for sharing an ECDH public key with a remote party.
Returns — A byte[] containing the SubjectPublicKeyInfo-encoded public key.
Exceptions
| Exception | Condition |
|---|---|
CryptographicException |
The key data is malformed or the export operation fails. |
public string ExportPem()Exports the key as RFC 7468 PEM text with the PUBLIC KEY label (SubjectPublicKeyInfo).
Returns — A PEM-encoded string containing the public key.
Exceptions
| Exception | Condition |
|---|---|
CryptographicException |
The key data is malformed or the export operation fails. |
public override NamedPublicKey ToNamedPublicKey()Creates a new NamedPublicKey pairing this instance with the algorithm name "ECDH".
Returns — A NamedPublicKey whose AlgorithmName is "ECDH" and whose PublicKey is this instance.
| Operator | From | Description |
|---|---|---|
implicit operator EcdhPublicKey |
ReadOnlySpan<byte> |
Creates a new EcdhPublicKey from a span of bytes. |
implicit operator EcdhPublicKey |
ReadOnlyMemory<byte> |
Creates a new EcdhPublicKey from a memory region. |
implicit operator EcdhPublicKey |
ReadOnlySequence<byte> |
Creates a new EcdhPublicKey from a byte sequence. |
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