Table of Contents

HashAlgorithmFactory Class

Definition

Namespace
Bodu.Security.Cryptography
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.2.0
Source
HashAlgorithmFactory.cs

Provides static factory helpers for constructing delegate-based implementations of IHashAlgorithmFactory<T>.

public static class HashAlgorithmFactory
Inheritance
HashAlgorithmFactory
Inherited Members

Examples

using System.Security.Cryptography;
using Bodu.Security.Cryptography;

// 1. Bare SHA-256 factory.
IHashAlgorithmFactory<SHA256> sha256 = HashAlgorithmFactory.From(() => SHA256.Create());
byte[] digest = HashAlgorithmHelper.HashData(sha256, payload);

// 2. Configured keyed-hash factory - applied consistently every time the consumer constructs an instance.
IHashAlgorithmFactory<SipHash64> sip = HashAlgorithmFactory.From(() => new SipHash64 { Key = sessionKey });
byte[] tag = HashAlgorithmHelper.HashData(sip, message);

Remarks

This class simplifies creation of lightweight hash algorithm factories that encapsulate algorithm configuration logic, particularly useful for one-shot hashing scenarios where consumers need to consistently configure algorithm instances (e.g., setting a key, tuning rounds, or enabling specific variants).

The returned factory instances are typically passed into HashAlgorithmHelper methods to compute hashes from spans, streams, or buffers in a memory-safe and reusable way. The same factory's Create() serves MerkleTree for tree-hashing pipelines that need a fresh leaf-hash instance per worker.

Methods

From<T>(Func<T>)

Creates a delegate-based factory that constructs new instances of the hash algorithm using the specified builder.

public static DelegateHashAlgorithmFactory<T> From<T>(Func<T> builder) where T : HashAlgorithm

Parameters

builder Func<T>

A delegate that returns a fully configured instance of T. This delegate is invoked each time Create() is called.

Returns

DelegateHashAlgorithmFactory<T>

A delegate-backed implementation of IHashAlgorithmFactory<T> that produces new algorithm instances on demand.

Type Parameters

T

The type of HashAlgorithm returned by the factory.

Remarks

The type argument T must be a concrete HashAlgorithm subclass that builder can construct.

Exceptions

ArgumentNullException

Thrown when builder is null.

Applies to

ProductVersions
.NET8, 10

See Also