Repository navigation
OnixLabs.Security.Cryptography.NamedHash
Represents a named cryptographic hash: a Hash value paired with the name of the algorithm that produced it.
| Namespace | OnixLabs.Security.Cryptography |
| Assembly | OnixLabs.Security.Cryptography.dll (NuGet) |
| Inheritance |
object → ValueType → NamedHash
|
| Implements |
ICryptoPrimitive<NamedHash>, IValueEquatable<NamedHash>, IEquatable<NamedHash>, IBinaryConvertible, ISpanParsable<NamedHash>
|
public readonly partial record struct NamedHash :
ICryptoPrimitive<NamedHash>,
ISpanParsable<NamedHash>NamedHash solves a common serialisation problem: a raw Hash value alone is uninterpretable without knowing which algorithm produced it. By bundling the Hash together with an AlgorithmName string, NamedHash creates a self-describing token that can survive a round-trip through storage or a wire protocol and be validated on the other side without out-of-band metadata.
The canonical string representation — produced by ToString() and consumed by Parse / TryParse — is:
<AlgorithmName>:<hex-encoded hash bytes>
For example, a SHA-256 hash might render as SHA256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.
Value semantics — as a record struct, NamedHash uses structural equality: two instances are equal when both their Hash value and their AlgorithmName string are equal. The compiler-generated == and != operators, Equals, GetHashCode, and Deconstruct all follow from this.
Note
NamedHashimplementsICryptoPrimitive<NamedHash>, which transitively requiresIValueEquatable<NamedHash>andIBinaryConvertible. The binary-convertible methods (AsReadOnlySpan,AsReadOnlyMemory) delegate to the underlyingHash, not the algorithm name.
using System.Security.Cryptography;
using OnixLabs.Security.Cryptography;
// Compute a hash and wrap it with its algorithm name
byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello, World!");
byte[] hashBytes = SHA256.HashData(data);
Hash hash = new Hash(hashBytes);
NamedHash named = new NamedHash(hash, HashAlgorithmName.SHA256);
Console.WriteLine(named.AlgorithmName); // SHA256
Console.WriteLine(named.Hash); // (hex of hash bytes)
Console.WriteLine(named); // SHA256:<hex>using OnixLabs.Security.Cryptography;
string serialised = named.ToString(); // "SHA256:<hex>"
NamedHash restored = NamedHash.Parse(serialised);
Console.WriteLine(restored == named); // Trueusing OnixLabs.Core.Text;
using OnixLabs.Security.Cryptography;
// Encode the hash bytes as Base58 rather than hex
string base58Form = named.ToString(Base58FormatProvider.Invariant);| Constructor | Description |
|---|---|
NamedHash(Hash, string) |
Initializes a new instance with a hash value and a string algorithm name. |
NamedHash(Hash, HashAlgorithmName) |
Initializes a new instance with a hash value and a HashAlgorithmName. |
public NamedHash(Hash hash, string algorithmName)Initializes a new instance of the NamedHash struct.
| Parameter | Type | Description |
|---|---|---|
hash |
Hash |
The underlying hash value. |
algorithmName |
string |
The name of the hash algorithm that was used to produce the associated hash. |
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
algorithmName is null, empty, or consists only of whitespace. |
public NamedHash(Hash hash, HashAlgorithmName algorithmName)Initializes a new instance of the NamedHash struct using a HashAlgorithmName.
| Parameter | Type | Description |
|---|---|---|
hash |
Hash |
The underlying hash value. |
algorithmName |
HashAlgorithmName |
The name of the hash algorithm that was used to produce the associated hash. The Name property of this struct is used. |
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
The Name of algorithmName is null, empty, or consists only of whitespace. |
| Property | Type | Description |
|---|---|---|
Hash |
Hash |
Gets the underlying hash value. |
AlgorithmName |
string |
Gets the name of the hash algorithm that produced the hash. |
public Hash Hash { get; }Gets the underlying Hash value associated with this instance.
public string AlgorithmName { get; }Gets the name of the hash algorithm that was used to produce the associated hash. This is a non-null, non-empty, non-whitespace string that identifies the algorithm — for example, "SHA256" or "SHA512".
| Method | Returns | Description |
|---|---|---|
Parse(string, IFormatProvider?) |
NamedHash |
Parses a string into a NamedHash value. |
Parse(ReadOnlySpan<char>, IFormatProvider?) |
NamedHash |
Parses a ReadOnlySpan<char> into a NamedHash value. |
TryParse(string?, IFormatProvider?, out NamedHash) |
bool |
Tries to parse a string into a NamedHash value. |
TryParse(ReadOnlySpan<char>, IFormatProvider?, out NamedHash) |
bool |
Tries to parse a ReadOnlySpan<char> into a NamedHash value. |
| Method | Returns | Description |
|---|---|---|
AsReadOnlyMemory() |
ReadOnlyMemory<byte> |
Returns the underlying hash bytes as a ReadOnlyMemory<byte>. |
AsReadOnlySpan() |
ReadOnlySpan<byte> |
Returns the underlying hash 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 hash bytes. |
public static NamedHash Parse(string value, IFormatProvider? provider = null)Parses the specified string into a NamedHash value. The expected format is <AlgorithmName>:<encoded-hash-bytes> where the hash bytes are encoded according to provider (defaulting to Base16/hex when provider is null).
| Parameter | Type | Description |
|---|---|---|
value |
string |
The string to parse. |
provider |
IFormatProvider? |
A format provider that controls how the hash bytes are decoded, or null to use Base16 (hex). |
Returns — A new NamedHash parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value does not contain a : separator, either segment is empty, or the hash portion cannot be decoded. |
public static NamedHash Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)Parses the specified ReadOnlySpan<char> into a NamedHash value.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The character span to parse. |
provider |
IFormatProvider? |
A format provider that controls how the hash bytes are decoded, or null to use Base16 (hex). |
Returns — A new NamedHash parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value does not contain a : separator, either segment is empty, or the hash portion cannot be decoded. |
public static bool TryParse(string? value, IFormatProvider? provider, out NamedHash result)Tries to parse the specified string into a NamedHash value.
| Parameter | Type | Description |
|---|---|---|
value |
string? |
The string to parse. |
provider |
IFormatProvider? |
A format provider that controls how the hash bytes are decoded, or null to use Base16 (hex). |
result |
out NamedHash |
On return, contains the parsed value, or the default if parsing failed. |
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out NamedHash result)Tries to parse the specified ReadOnlySpan<char> into a NamedHash value.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The character span to parse. |
provider |
IFormatProvider? |
A format provider that controls how the hash bytes are decoded, or null to use Base16 (hex). |
result |
out NamedHash |
On return, contains the parsed value, or the default if parsing failed. |
Returns — true if parsing succeeded; otherwise, false.
public ReadOnlyMemory<byte> AsReadOnlyMemory()Returns the underlying hash bytes as a ReadOnlyMemory<byte>. Delegates to the wrapped Hash.
Returns — A ReadOnlyMemory<byte> view of the hash bytes.
public ReadOnlySpan<byte> AsReadOnlySpan()Returns the underlying hash bytes as a ReadOnlySpan<byte>. Delegates to the wrapped Hash.
Returns — A ReadOnlySpan<byte> view of the hash bytes.
public override string ToString()Returns the canonical string representation of this instance, using Base16 (lowercase hex) encoding for the hash bytes.
Returns — A string of the form <AlgorithmName>:<hex-encoded-hash>, for example SHA256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.
public string ToString(IFormatProvider provider)Returns a string representation of this instance, encoding the hash 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 hash bytes are encoded. |
Returns — A string of the form <AlgorithmName>:<encoded-hash>.
| Operator | Description |
|---|---|
operator == |
Determines whether two NamedHash values are equal. |
operator != |
Determines whether two NamedHash values are not equal. |
public static bool operator ==(NamedHash? left, NamedHash? right)Determines whether two NamedHash values are equal. Two values are equal when both their Hash and AlgorithmName are equal. Synthesised by the record struct compiler feature and required by IValueEquatable<NamedHash>.
public static bool operator !=(NamedHash? left, NamedHash? right)Determines whether two NamedHash 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