Repository navigation
OnixLabs.Security.Cryptography.ICryptoPrimitive
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
|
public interface ICryptoPrimitive<T> : IValueEquatable<T>, IBinaryConvertible
where T : ICryptoPrimitive<T>| 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. |
ICryptoPrimitive<T> composes three capabilities:
-
Value equality — from
IValueEquatable<T>: the==and!=operators, plus theIEquatable<T>.Equals(T?)method. Two primitive instances with identical byte content compare as equal regardless of reference. -
Binary conversion — from
IBinaryConvertible:AsReadOnlySpan()andAsReadOnlyMemory()expose the raw bytes without forcing abyte[]allocation. -
Format-aware formatting — the single member declared directly on this interface,
ToString(IFormatProvider), allows the caller to supply a culture or customIFormatProviderwhen 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.
| Member | Returns | Description |
|---|---|---|
ToString(IFormatProvider) |
string |
Returns a string representation of the current object using the specified format provider. |
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.
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));using OnixLabs.Security.Cryptography;
static string Encode<T>(T primitive, IFormatProvider provider)
where T : ICryptoPrimitive<T>
=> primitive.ToString(provider);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