Table of Contents

DelegateHashAlgorithmFactory<T> Class

Definition

Namespace
Bodu.Security.Cryptography
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.2.0
Source
DelegateHashAlgorithmFactory{T}.cs

Provides a delegate-based implementation of IHashAlgorithmFactory<T> for constructing hash algorithm instances.

public sealed class DelegateHashAlgorithmFactory<T> : IHashAlgorithmFactory<T> where T : HashAlgorithm

Type Parameters

T

The concrete HashAlgorithm type this factory produces.

Inheritance
DelegateHashAlgorithmFactory<T>
Implements
Inherited Members
Extension Methods

Examples

// Wrap a delegate via the static helper - the call site reads more naturally than the
// constructor and avoids the trailing generic-type repetition.
IHashAlgorithmFactory<SipHash64> factory = HashAlgorithmFactory.From(() =>
    new SipHash64 { Key = sharedKey });

// Resolve and use - the factory hands out a freshly-keyed instance each time.
using SipHash64 hash = factory.Create();
byte[] digest = hash.ComputeHash("hello"u8.ToArray());

// Or construct directly when the field type needs to be the concrete factory.
var concreteFactory = new DelegateHashAlgorithmFactory<Tiger>(() => new Tiger
{
    HashingVariant = TigerHashingVariant.Tiger2,
});

Remarks

This factory allows consumers to specify the construction logic for the hash algorithm via a delegate. It is particularly useful for scenarios requiring configuration of keyed or parameterized algorithms where DI or explicit configuration is preferred.

Common use cases include configuring KeyedHashAlgorithm instances or variant-based hashes such as SipHash or CubeHash.

Construction. Most callers use From<T>(Func<T>) to wrap a delegate rather than calling this constructor directly - the static helper makes the call site read more naturally and avoids the trailing generic-type repetition. Direct construction is appropriate when the type needs to be assigned to a field that has the concrete DelegateHashAlgorithmFactory<T> type rather than the IHashAlgorithmFactory<T> interface.

Constructors

DelegateHashAlgorithmFactory(Func<T>)

Initializes a new instance of the DelegateHashAlgorithmFactory<T> class using the specified construction delegate.

public DelegateHashAlgorithmFactory(Func<T> builder)

Parameters

builder Func<T>

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

Exceptions

ArgumentNullException

Thrown if builder is null.

Methods

Create()

Creates and returns a new instance of the hash algorithm with any necessary configuration applied.

public T Create()

Returns

T

A fully initialized T instance, ready for data input.

Applies to

ProductVersions
.NET8, 10

See Also