Table of Contents

Class IPrivateKeyExtensions

Namespace
Yubico.YubiKey.Cryptography
Assembly
Yubico.YubiKey.dll

Extension methods for IPrivateKey to provide type-safe casting operations.

public static class IPrivateKeyExtensions
Inheritance
object
IPrivateKeyExtensions

Methods

Cast<T>(IPrivateKey)

Safely casts an IPrivateKey instance to a specific derived type.

public static T Cast<T>(this IPrivateKey key) where T : class, IPrivateKey

Parameters

key IPrivateKey

The private key instance to cast.

Returns

T

The private key cast to the specified type T.

Type Parameters

T

The target type that implements IPrivateKey.

Examples

IPrivateKey genericKey = GetPrivateKey();
RsaPrivateKey rsaKey = genericKey.Cast<RsaPrivateKey>();
// throws InvalidCastException if genericKey is not an RsaPrivateKey

Remarks

This method provides a more explicit alternative to direct casting with clearer error messages. Prefer this over unsafe casting operations when type safety is critical for cryptographic operations.

Exceptions

InvalidCastException

Thrown when the key cannot be cast to type T. The exception message includes both the source and target type names for debugging.