Skip to content

OnixLabs.Security.Cryptography.NamedHash

matthew edited this page May 27, 2026 · 2 revisions

NamedHash Struct

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>

Definition

public readonly partial record struct NamedHash :
    ICryptoPrimitive<NamedHash>,
    ISpanParsable<NamedHash>

Remarks

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 NamedHash implements ICryptoPrimitive<NamedHash>, which transitively requires IValueEquatable<NamedHash> and IBinaryConvertible. The binary-convertible methods (AsReadOnlySpan, AsReadOnlyMemory) delegate to the underlying Hash, not the algorithm name.

Examples

Creating a NamedHash

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>

Round-tripping through a string

using OnixLabs.Security.Cryptography;

string serialised = named.ToString(); // "SHA256:<hex>"

NamedHash restored = NamedHash.Parse(serialised);

Console.WriteLine(restored == named); // True

Using a format provider for Base58

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

// Encode the hash bytes as Base58 rather than hex
string base58Form = named.ToString(Base58FormatProvider.Invariant);

Constructors

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.

NamedHash(Hash, string)

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.

NamedHash(Hash, HashAlgorithmName)

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.

Properties

Property Type Description
Hash Hash Gets the underlying hash value.
AlgorithmName string Gets the name of the hash algorithm that produced the hash.

Hash

public Hash Hash { get; }

Gets the underlying Hash value associated with this instance.

AlgorithmName

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".

Methods

Static Methods

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.

Instance Methods

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.

Parse(string, IFormatProvider?)

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.

Parse(ReadOnlySpan<char>, IFormatProvider?)

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.

TryParse(string?, IFormatProvider?, out NamedHash)

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.


TryParse(ReadOnlySpan<char>, IFormatProvider?, out NamedHash)

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.


AsReadOnlyMemory()

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.


AsReadOnlySpan()

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.


ToString()

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.


ToString(IFormatProvider)

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>.

Operators

Operator Description
operator == Determines whether two NamedHash values are equal.
operator != Determines whether two NamedHash values are not equal.

operator ==

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>.

operator !=

public static bool operator !=(NamedHash? left, NamedHash? right)

Determines whether two NamedHash values are not equal. The negation of operator ==.

See Also

ONIXLabs .NET Library


Clone this wiki locally