Table of Contents

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

factory IHashAlgorithmFactory<T>

The factory used to create the hash algorithm.

stream Stream

The stream to hash.

cancellationToken CancellationToken

An optional cancellation token.

Returns

ValueTask<byte[]>

A task representing the asynchronous hash computation.

Type Parameters

T

The type of HashAlgorithm.

Exceptions

ArgumentNullException

Thrown when factory or stream is 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

factory IHashAlgorithmFactory<T>

The factory used to create the hash algorithm.

stream Stream

The stream to hash.

Returns

byte[]

The computed hash as a byte array.

Type Parameters

T

The type of HashAlgorithm.

Exceptions

ArgumentNullException

Thrown when factory or stream is 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

factory IHashAlgorithmFactory<T>

The factory used to create the hash algorithm.

input ReadOnlySpan<byte>

The input data to hash.

Returns

byte[]

The computed hash as a byte array.

Type Parameters

T

The type of HashAlgorithm.

Exceptions

ArgumentNullException

Thrown when factory is 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

factory IHashAlgorithmFactory<T>

The factory used to create the hash algorithm.

input ReadOnlySpan<byte>

The input data to hash.

destination Span<byte>

The buffer to receive the hash value.

bytesWritten int

Receives the number of bytes written to destination.

Returns

bool

true if the hash fits in the destination buffer; otherwise, false.

Type Parameters

T

The type of HashAlgorithm.

Exceptions

ArgumentNullException

Thrown when factory is null.

Applies to

ProductVersions
.NET8, 10

See Also