Class HashAlgorithm
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Hash
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Base class for the CryptoHives hash algorithm implementations.
public abstract class HashAlgorithm : HashAlgorithm, ICryptoTransform, IDisposable, IResettable
- Inheritance
-
HashAlgorithm
- Implements
- Derived
- Inherited Members
Remarks
This class extends HashAlgorithm to provide a common base for all CryptoHives hash implementations. It ensures consistent behavior and provides helper methods for derived implementations.
All derived classes implement hash algorithms without OS or hardware dependencies, providing deterministic behavior across all platforms.
Constructors
HashAlgorithm()
Initializes a new instance of the HashAlgorithm class.
protected HashAlgorithm()
Properties
AlgorithmName
Gets the name of the hash algorithm.
public abstract string AlgorithmName { get; }
Property Value
BlockSize
Gets the block size in bytes used by the hash algorithm.
public abstract int BlockSize { get; }
Property Value
Methods
AppendData(in ReadOnlySequence<byte>)
Appends all segments of source to the data already processed in the hash algorithm.
public void AppendData(in ReadOnlySequence<byte> source)
Parameters
sourceReadOnlySequence<byte>The (possibly multi-segment) input sequence to append to the hash computation.
Remarks
Use this overload when data arrives from System.IO.Pipelines or any other source that
provides a ReadOnlySequence<T>. Each segment is fed directly into the algorithm
without copying the data into a contiguous buffer.
Call TryGetHashAndReset(Span<byte>, out int) after all data has been appended to retrieve the final hash value and reset the algorithm for reuse.
AppendData(ReadOnlySpan<byte>)
Appends the specified data to the data already processed in the hash algorithm.
public void AppendData(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The input to append to the hash computation.
Remarks
This is a zero-allocation alternative to TransformBlock(byte[], int, int, byte[], int) for incremental hashing with span-based buffers.
Call TryGetHashAndReset(Span<byte>, out int) after all data has been appended to retrieve the final hash value and reset the algorithm for reuse.
using var sha256 = SHA256.Create();
sha256.AppendData(chunk1);
sha256.AppendData(chunk2);
Span<byte> hash = stackalloc byte[32];
sha256.TryGetHashAndReset(hash, out _);
ClearBuffer(Span<byte>)
Clears sensitive data from a span.
protected static void ClearBuffer(Span<byte> data)
Parameters
Create(string)
Creates a new instance of the specified hash algorithm.
public static HashAlgorithm Create(string hashName)
Parameters
hashNamestringThe name of the hash algorithm to create.
Returns
- HashAlgorithm
A new hash algorithm instance.
Exceptions
- ArgumentException
The specified algorithm name is unknown.
- PlatformNotSupportedException
The specified algorithm is not supported.
Create(string, bool)
Creates a new instance of the specified hash algorithm. Optionally uses the OS version (mostly faster) if available.
public static HashAlgorithm Create(string hashName, bool osVersion = false)
Parameters
hashNamestringThe name of the hash algorithm to create.
osVersionboolSet to true to instantiate the OS version of the algorithm, if available.
Returns
- HashAlgorithm
A new hash algorithm instance.
Exceptions
- ArgumentException
The specified algorithm name is unknown.
- PlatformNotSupportedException
The specified algorithm is not supported.
HashCore(byte[], int, int)
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(byte[] array, int ibStart, int cbSize)
Parameters
arraybyte[]The input to compute the hash code for.
ibStartintThe offset into the byte array from which to begin using data.
cbSizeintThe number of bytes in the byte array to use as data.
Remarks
Overridden to route to the HashCore(ReadOnlySpan{byte} method. Implementations should override that method instead.
HashCore(ReadOnlySpan<byte>)
When overridden in a derived class, routes data written to the object into the hash algorithm for computing the hash.
protected abstract void HashCore(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The input to compute the hash code for.
HashFinal()
When overridden in a derived class, finalizes the hash computation after the last data is processed by the cryptographic stream object.
protected override sealed byte[] HashFinal()
Returns
- byte[]
The computed hash code.
TryComputeHash(ReadOnlySpan<byte>, Span<byte>, out int)
Attempts to compute the hash value for the specified read-only byte span and writes the result into the provided destination span.
public bool TryComputeHash(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
Parameters
sourceReadOnlySpan<byte>The input to compute the hash code for.
destinationSpan<byte>The buffer to receive the hash value.
bytesWrittenintWhen this method returns, the total number of bytes written into
destination.
Returns
Remarks
The algorithm is automatically reset after a successful computation, allowing the instance to be reused for subsequent AppendData(ReadOnlySpan<byte>) calls or another TryComputeHash(ReadOnlySpan<byte>, Span<byte>, out int) without calling Initialize() first.
TryGetHashAndReset(Span<byte>, out int)
Finalizes the hash computation, writes the result into the provided buffer, and resets the algorithm for reuse.
public bool TryGetHashAndReset(Span<byte> destination, out int bytesWritten)
Parameters
destinationSpan<byte>The buffer to receive the hash value.
bytesWrittenintWhen this method returns, the total number of bytes written into
destination.
Returns
Remarks
This is a zero-allocation alternative to reading the Hash property after TransformFinalBlock(byte[], int, int).
The algorithm is automatically reset after a successful call, allowing the instance to be reused for a new computation without calling Initialize().
Exceptions
- ObjectDisposedException
The instance has been disposed.
TryHashFinal(Span<byte>, out int)
When overridden in a derived class, finalizes the hash computation and writes the result into the provided buffer.
protected abstract 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
TryReset()
Resets this instance to its initial state so it can be returned to an object pool for reuse.
public virtual bool TryReset()
Returns
- bool
true if the instance was reset and may be returned to the pool; false if it must instead be disposed and discarded (see remarks).
Remarks
This method implements IResettable from
Microsoft.Extensions.ObjectPool, enabling any CryptoHives hash algorithm to be
used with DefaultObjectPool<T> without a custom policy.
Overridden by algorithms that can carry caller-supplied secret material (e.g. a keyed BLAKE3 instance): those return false so the pool's policy disposes the instance — erasing the secret — instead of recycling it for an unrelated caller.