Table of Contents

HashValue Struct

Definition

Namespace
Bodu.Security.Cryptography
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.2.0
Source
HashValue.cs

Represents the immutable output of a hash operation, with strict hexadecimal parsing, common text formattings, and explicit fixed-time comparison.

public readonly struct HashValue : IEquatable<HashValue>
Implements
Inherited Members
Extension Methods

Examples

// Wrap a freshly computed digest, then format it for logging or storage.
HashValue digest = HashValue.FromBytes(SHA256.HashData(payload));
string hex = digest.ToHexString();   // lowercase, e.g. "e3b0c442..."

// Compare the digest against an expected value in fixed time before trusting the payload.
HashValue expected = HashValue.ParseHex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
if (!digest.FixedTimeEquals(expected))
    throw new CryptographicException("Digest mismatch.");

Remarks

HashValue carries the digest bytes by defensive copy: the value is unaffected by later mutation of the source buffer, and no member exposes the internal storage as a mutable array.

The default instance (default(HashValue)) is the empty value: Length is 0, IsEmpty is true, and it compares equal to FromBytes(ReadOnlySpan<byte>) over an empty span.

Equals(HashValue) compares content in constant time via FixedTimeEquals(ReadOnlySpan<byte>, ReadOnlySpan<byte>), so even the ordinary equality operators are safe when the comparison is security-relevant - for example validating a received digest against a locally computed one. FixedTimeEquals(HashValue) remains available and behaves identically, making the intent explicit at the call site.

Properties

IsEmpty

Gets a value indicating whether the hash value is empty.

public bool IsEmpty { get; }

Property Value

bool

true if the value contains no bytes; otherwise, false.

Length

Gets the number of bytes in the hash value.

public int Length { get; }

Property Value

int

The digest length in bytes, or 0 for the empty value.

Methods

AsSpan()

Returns a read-only view over the digest bytes.

public ReadOnlySpan<byte> AsSpan()

Returns

ReadOnlySpan<byte>

A read-only span over the value; empty for the empty value.

Equals(HashValue)

Determines whether this hash value equals another.

public bool Equals(HashValue other)

Parameters

other HashValue

The hash value to compare against.

Returns

bool

true if both values contain identical bytes; otherwise, false.

Remarks

The byte comparison is constant-time in content - it runs through FixedTimeEquals(ReadOnlySpan<byte>, ReadOnlySpan<byte>), so its duration depends only on the operand length, not on where the bytes first differ. A length mismatch returns false immediately (the length is not secret). FixedTimeEquals(HashValue) remains available and behaves identically.

Equals(object?)

Determines whether this hash value equals the specified object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare against.

Returns

bool

true if obj is a HashValue with identical bytes; otherwise, false.

FixedTimeEquals(HashValue)

Compares this hash value to another in fixed time.

public bool FixedTimeEquals(HashValue other)

Parameters

other HashValue

The hash value to compare against.

Returns

bool

true if both values have the same length and identical bytes; otherwise, false.

Remarks

Built on FixedTimeEquals(ReadOnlySpan<byte>, ReadOnlySpan<byte>): the comparison time depends only on the length of the operands, not on their content. A length mismatch returns false immediately, so the lengths themselves are not concealed.

FromBytes(ReadOnlySpan<byte>)

Creates a HashValue from the provided bytes.

public static HashValue FromBytes(ReadOnlySpan<byte> value)

Parameters

value ReadOnlySpan<byte>

The digest bytes to copy. An empty span yields the empty value.

Returns

HashValue

A new HashValue containing a defensive copy of value.

GetHashCode()

Returns a hash code computed over the digest bytes.

public override int GetHashCode()

Returns

int

A hash code consistent with Equals(HashValue).

ParseHex(string)

Parses a strict hexadecimal string into a HashValue.

public static HashValue ParseHex(string text)

Parameters

text string

The hexadecimal text. Both uppercase and lowercase digits are accepted.

Returns

HashValue

A HashValue containing the decoded bytes.

Remarks

Parsing is strict: whitespace, separators, and 0x prefixes are rejected. An empty string parses to the empty value.

Exceptions

ArgumentNullException

text is null.

FormatException

text has odd length or contains a character that is not an ASCII hexadecimal digit.

ToArray()

Copies the digest bytes into a new array.

public byte[] ToArray()

Returns

byte[]

A new array containing the digest bytes; an empty array for the empty value.

ToBase64String()

Formats the hash value as a Base64 string.

public string ToBase64String()

Returns

string

The Base64 representation, or Empty for the empty value.

ToHexString()

Formats the hash value as a lowercase hexadecimal string.

public string ToHexString()

Returns

string

The lowercase hexadecimal representation, or Empty for the empty value.

ToString()

Returns the lowercase hexadecimal representation of the hash value.

public override string ToString()

Returns

string

The same text as ToHexString().

Remarks

Hash values are not secrets, so the digest content is intentionally included in the string representation.

TryParseHex(string?, out HashValue)

Attempts to parse a strict hexadecimal string into a HashValue.

public static bool TryParseHex(string? text, out HashValue result)

Parameters

text string

The hexadecimal text, or null.

result HashValue

When this method returns true, the parsed value; otherwise the empty value.

Returns

bool

true if text is a well-formed hexadecimal string; otherwise, false.

Operators

operator ==(HashValue, HashValue)

Determines whether two hash values are equal.

public static bool operator ==(HashValue left, HashValue right)

Parameters

left HashValue

The first hash value.

right HashValue

The second hash value.

Returns

bool

true if the values contain identical bytes; otherwise, false.

operator !=(HashValue, HashValue)

Determines whether two hash values are not equal.

public static bool operator !=(HashValue left, HashValue right)

Parameters

left HashValue

The first hash value.

right HashValue

The second hash value.

Returns

bool

true if the values differ; otherwise, false.

Applies to

ProductVersions
.NET8, 10