Table of Contents

Class Sha2HashAlgorithm<T>

Namespace
CryptoHives.Foundation.Security.Cryptography.Hash
Assembly
CryptoHives.Foundation.Security.Cryptography.dll

Base class for SHA-2 family hash algorithms, providing common buffering and padding logic.

public abstract class Sha2HashAlgorithm<T> : HashAlgorithm, ICryptoTransform, IDisposable, IResettable where T : struct

Type Parameters

T

The word type (uint for SHA-256 family, ulong for SHA-512 family).

Inheritance
Sha2HashAlgorithm<T>
Implements
Derived
Inherited Members

Remarks

This base class eliminates code duplication across SHA-2 variants by providing common implementations of HashCore, TryHashFinal, and Dispose. Derived classes only need to provide initialization values, block processing, and output formatting.

Constructors

Sha2HashAlgorithm()

Initializes a new instance of the Sha2HashAlgorithm<T> class.

protected Sha2HashAlgorithm()

Fields

StateSizeWords

The number of words in the state.

public const int StateSizeWords = 8

Field Value

int

Properties

BlockSizeBytes

Gets the block size in bytes for this algorithm.

protected abstract int BlockSizeBytes { get; }

Property Value

int

OutputSizeBytes

Gets the hash output size in bytes.

protected abstract int OutputSizeBytes { get; }

Property Value

int

Methods

Dispose(bool)

Releases the unmanaged resources used by the HashAlgorithm and optionally releases the managed resources.

protected override sealed void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

HashCore(ReadOnlySpan<byte>)

When overridden in a derived class, routes data written to the object into the hash algorithm for computing the hash.

protected override sealed void HashCore(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<byte>

The input to compute the hash code for.

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.

Initialize()

Initializes an implementation of the HashAlgorithm class.

public override sealed void Initialize()

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.

InitializeState()

Initializes the hash state with algorithm-specific initial values.

protected abstract void InitializeState()

OutputHash(Span<byte>, T[])

Writes the final hash output from the state to the destination.

protected abstract void OutputHash(Span<byte> destination, T[] state)

Parameters

destination Span<byte>

The destination span for the hash output.

state T[]

The state array containing the hash values.

PadAndFinalize(Span<byte>, int, long, Span<T>)

Pads and finalizes the hash computation.

protected abstract void PadAndFinalize(Span<byte> buffer, int bufferLength, long bytesProcessed, Span<T> state)

Parameters

buffer Span<byte>

The buffer containing remaining unprocessed bytes.

bufferLength int

The number of valid bytes in the buffer.

bytesProcessed long

Total bytes processed before this finalization.

state Span<T>

The state array to update.

ProcessBlock(ReadOnlySpan<byte>, Span<T>)

Processes a single block, updating the state.

protected abstract void ProcessBlock(ReadOnlySpan<byte> block, Span<T> state)

Parameters

block ReadOnlySpan<byte>

The block data to process.

state Span<T>

The state array to update.

TryHashFinal(Span<byte>, out int)

When overridden in a derived class, finalizes the hash computation and writes the result into the provided buffer.

protected override sealed bool TryHashFinal(Span<byte> destination, out int bytesWritten)

Parameters

destination Span<byte>

The target buffer to write the hash to.

bytesWritten int

The number of bytes written into the buffer.

Returns

bool

true if the destination buffer was large enough; otherwise false.

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.