Skip to content

OnixLabs.Security.Cryptography.ICryptoPrimitive

matthew edited this page May 27, 2026 · 1 revision

ICryptoPrimitive<T> Interface

Defines a cryptographic primitive with value semantics, binary convertibility, and format-provider-aware string representation.

Namespace OnixLabs.Security.Cryptography
Assembly OnixLabs.Security.Cryptography.dll (NuGet)
Implements IValueEquatable<T>, IBinaryConvertible

Definition

public interface ICryptoPrimitive<T> : IValueEquatable<T>, IBinaryConvertible
    where T : ICryptoPrimitive<T>

Type Parameters

Parameter Description
T The underlying type of the cryptographic primitive. The self-referential constraint where T : ICryptoPrimitive<T> is the curiously recurring template pattern, which binds the equality operators inherited from IValueEquatable<T> to the concrete implementing type.

Remarks

ICryptoPrimitive<T> composes three capabilities:

  • Value equality — from IValueEquatable<T>: the == and != operators, plus the IEquatable<T>.Equals(T?) method. Two primitive instances with identical byte content compare as equal regardless of reference.
  • Binary conversion — from IBinaryConvertible: AsReadOnlySpan() and AsReadOnlyMemory() expose the raw bytes without forcing a byte[] allocation.
  • Format-aware formatting — the single member declared directly on this interface, ToString(IFormatProvider), allows the caller to supply a culture or custom IFormatProvider when formatting the primitive as a string.

Every concrete cryptographic primitive in the library — Hash, DigitalSignature, Secret, PrivateKey, PublicKey, and so on — implements ICryptoPrimitive<T>.

An implementing type must also supply operator bodies, override object.Equals and object.GetHashCode, and implement IEquatable<T>.Equals(T?) so that equality is consistent across all call paths.

Members

Member Returns Description
ToString(IFormatProvider) string Returns a string representation of the current object using the specified format provider.

ToString(IFormatProvider)

string ToString(IFormatProvider provider);

Returns a string that represents the current object, formatted according to the specified provider.

Parameter Type Description
provider IFormatProvider The format provider used to determine the format of the returned string. Typically an IBaseCodec-compatible provider such as Base16FormatProvider, Base58FormatProvider, or Base64FormatProvider.

Returns — A string representing the current object, formatted by provider.

Examples

Using ToString(IFormatProvider) on a Hash

using System.Security.Cryptography;
using OnixLabs.Core.Text;
using OnixLabs.Security.Cryptography;

using HashAlgorithm sha256 = SHA256.Create();
Hash hash = Hash.Compute(sha256, "Hello, World!"u8);

// Default hex encoding (Base16)
Console.WriteLine(hash.ToString());

// Base58 encoding via IFormatProvider
Console.WriteLine(hash.ToString(Base58FormatProvider.Bitcoin));

Accepting any ICryptoPrimitive<T> in a generic helper

using OnixLabs.Security.Cryptography;

static string Encode<T>(T primitive, IFormatProvider provider)
    where T : ICryptoPrimitive<T>
    => primitive.ToString(provider);

See Also

ONIXLabs .NET Library


Clone this wiki locally