HashAlgorithmHelper Class
Definition
- Namespace
- Bodu.Security.Cryptography
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
- Source
- HashAlgorithmHelper.cs
Provides high-performance utility methods for one-shot hashing using factory-created HashAlgorithm instances.
public static class HashAlgorithmHelper
- Inheritance
-
HashAlgorithmHelper
- Inherited Members
Examples
// Configured SipHash-2-4 factory; the configuration applies to every call to HashData.
var factory = HashAlgorithmFactory.From(() => new SipHash64
{
Key = key,
CompressionRounds = 2,
FinalizationRounds = 4,
});
byte[] hash = HashAlgorithmHelper.HashData(factory, input);
Remarks
These methods simplify hashing workflows by accepting an IHashAlgorithmFactory<T> implementation, allowing consumers to construct and configure hash algorithms (including keyed or parameterized variants) without managing lifecycle manually.
This is ideal for use cases that require stateless or ephemeral hashing operations without incremental updates or state reuse. Each call constructs a fresh algorithm instance, runs the hash, and disposes deterministically - callers do not need to track IDisposable lifetimes themselves.
When to choose this over the BCL. Pick HashAlgorithmHelper when the algorithm
requires per-call configuration (a key, round counts, a variant flag) - the factory consistently applies it to every
fresh instance. For stateless one-shot hashing of unconfigured algorithms (SHA-256, SHA-512) the BCL's static
HashData on each algorithm class is simpler and faster. For tree-hashing workloads pass the factory's
Create() to MerkleTree.
Methods
HashDataAsync<T>(IHashAlgorithmFactory<T>, Stream, CancellationToken)
Asynchronously computes the hash of a stream using a factory-created algorithm.
public static ValueTask<byte[]> HashDataAsync<T>(IHashAlgorithmFactory<T> factory, Stream stream, CancellationToken cancellationToken = default) where T : HashAlgorithm
Parameters
factoryIHashAlgorithmFactory<T>The factory used to create the hash algorithm.
streamStreamThe stream to hash.
cancellationTokenCancellationTokenAn optional cancellation token.
Returns
Type Parameters
TThe type of HashAlgorithm.
Exceptions
- ArgumentNullException
Thrown when
factoryorstreamis null.- CryptographicException
Thrown when the underlying cryptographic algorithm fails.
HashData<T>(IHashAlgorithmFactory<T>, Stream)
Computes the hash of a stream using a factory-created algorithm.
public static byte[] HashData<T>(IHashAlgorithmFactory<T> factory, Stream stream) where T : HashAlgorithm
Parameters
factoryIHashAlgorithmFactory<T>The factory used to create the hash algorithm.
streamStreamThe stream to hash.
Returns
- byte[]
The computed hash as a byte array.
Type Parameters
TThe type of HashAlgorithm.
Exceptions
- ArgumentNullException
Thrown when
factoryorstreamis null.- CryptographicException
Thrown when the underlying cryptographic algorithm fails.
HashData<T>(IHashAlgorithmFactory<T>, ReadOnlySpan<byte>)
Computes the hash for the given input using a factory-created algorithm.
public static byte[] HashData<T>(IHashAlgorithmFactory<T> factory, ReadOnlySpan<byte> input) where T : HashAlgorithm
Parameters
factoryIHashAlgorithmFactory<T>The factory used to create the hash algorithm.
inputReadOnlySpan<byte>The input data to hash.
Returns
- byte[]
The computed hash as a byte array.
Type Parameters
TThe type of HashAlgorithm.
Exceptions
- ArgumentNullException
Thrown when
factoryis null.- CryptographicException
Thrown when the underlying cryptographic algorithm fails.
TryHashData<T>(IHashAlgorithmFactory<T>, ReadOnlySpan<byte>, Span<byte>, out int)
Attempts to compute the hash and write it to the specified destination buffer.
public static bool TryHashData<T>(IHashAlgorithmFactory<T> factory, ReadOnlySpan<byte> input, Span<byte> destination, out int bytesWritten) where T : HashAlgorithm
Parameters
factoryIHashAlgorithmFactory<T>The factory used to create the hash algorithm.
inputReadOnlySpan<byte>The input data to hash.
destinationSpan<byte>The buffer to receive the hash value.
bytesWrittenintReceives the number of bytes written to
destination.
Returns
Type Parameters
TThe type of HashAlgorithm.
Exceptions
- ArgumentNullException
Thrown when
factoryis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |