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
TThe 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
Properties
BlockSizeBytes
Gets the block size in bytes for this algorithm.
protected abstract int BlockSizeBytes { get; }
Property Value
OutputSizeBytes
Gets the hash output size in bytes.
protected abstract int OutputSizeBytes { get; }
Property Value
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
disposingbooltrue 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
sourceReadOnlySpan<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
destinationSpan<byte>The destination span for the hash output.
stateT[]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
bufferSpan<byte>The buffer containing remaining unprocessed bytes.
bufferLengthintThe number of valid bytes in the buffer.
bytesProcessedlongTotal bytes processed before this finalization.
stateSpan<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
blockReadOnlySpan<byte>The block data to process.
stateSpan<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
destinationSpan<byte>The target buffer to write the hash to.
bytesWrittenintThe number of bytes written into the buffer.
Returns
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.