Skip to content

Commit b25ad65

Browse files
authored
Update factory with better collection-based functions for hash algorithms
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent d38364f commit b25ad65

File tree

1 file changed

+89
-26
lines changed

1 file changed

+89
-26
lines changed

HashifyNet/Core/HashFactory.cs

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,94 @@ private HashFactory()
274274
}
275275

276276
/// <summary>
277-
/// Retrieves all available hash algorithm types registered in the system.
277+
/// Retrieves an array of hash function instances based on the specified hash function type.
278278
/// </summary>
279-
/// <remarks>The returned array contains the types of all registered hash algorithm implementations. If no
280-
/// hash algorithms are registered, the method returns <see langword="null"/>.</remarks>
281-
/// <returns>An array of <see cref="Type"/> objects representing the registered hash algorithm types, or <see
282-
/// langword="null"/> if no hash algorithms are available.</returns>
283-
public Type[] GetAllHashAlgorithms()
279+
/// <param name="type">The type of hash functions to retrieve. This determines the set of hash algorithms to be instantiated.</param>
280+
/// <param name="defaultConfigMap">An optional dictionary mapping hash function types to their corresponding configuration objects. If provided, the
281+
/// configurations in this dictionary will be used to initialize the hash function instances. If a type is not present
282+
/// in the dictionary, a default configuration will be used if available.</param>
283+
/// <param name="ignoredFunctions">An optional array of hash function types to be ignored during instantiation. Any types present in this array (and any types derive from the types in this array) will be skipped.</param>
284+
/// <returns>An array of <see cref="IHashFunctionBase"/> instances representing the hash functions for the specified type.</returns>
285+
/// <exception cref="InvalidOperationException">Thrown if no hash algorithms are found for the specified <paramref name="type"/>.</exception>
286+
public IHashFunctionBase[] GetHashFunctions(HashFunctionType type, Dictionary<Type, IHashConfigBase> defaultConfigMap = null, params Type[] ignoredFunctions)
287+
{
288+
Type[] functions = GetHashAlgorithms(type);
289+
290+
if (functions == null || functions.Length < 1)
291+
{
292+
throw new InvalidOperationException("No hash algorithms found.");
293+
}
294+
295+
List<IHashFunctionBase> instances = new List<IHashFunctionBase>();
296+
for (int i = 0; i < functions.Length; ++i)
297+
{
298+
Type funcType = functions[i];
299+
300+
if (ignoredFunctions != null && ignoredFunctions.Length > 0)
301+
{
302+
foreach (Type ignoredType in ignoredFunctions)
303+
{
304+
if ((funcType.IsGenericType && (funcType.GetGenericTypeDefinition() == ignoredType || ignoredType.IsAssignableFrom(funcType.GetGenericTypeDefinition()))) || funcType == ignoredType || ignoredType.IsAssignableFrom(funcType))
305+
{
306+
funcType = null;
307+
break;
308+
}
309+
}
310+
}
311+
312+
if (funcType == null)
313+
{
314+
continue;
315+
}
316+
317+
if (defaultConfigMap != null && defaultConfigMap.ContainsKey(funcType))
318+
{
319+
instances.Add(Create(funcType, defaultConfigMap[funcType]));
320+
}
321+
else
322+
{
323+
instances.Add(Create(funcType));
324+
}
325+
}
326+
327+
return instances.ToArray();
328+
}
329+
330+
/// <summary>
331+
/// Retrieves an array of hash algorithm types based on the specified hash function category.
332+
/// </summary>
333+
/// <remarks>The returned array may include both cryptographic and non-cryptographic hash algorithms if the
334+
/// specified <paramref name="type"/> includes both categories.</remarks>
335+
/// <param name="type">A bitwise combination of <see cref="HashFunctionType"/> values that specifies the category of hash functions to
336+
/// retrieve. Use <see cref="HashFunctionType.Cryptographic"/> to include cryptographic hash algorithms, <see
337+
/// cref="HashFunctionType.Noncryptographic"/> to include non-cryptographic hash algorithms, or both.</param>
338+
/// <returns>An array of <see cref="Type"/> objects representing the hash algorithms that match the specified category. If no
339+
/// algorithms match the specified category, an empty array is returned. If the given <paramref name="type"/> neither contains <seealso cref="HashFunctionType.Cryptographic"/> nor <seealso cref="HashFunctionType.Noncryptographic"/>, the return value will be <see langword="null"/>.</returns>
340+
public static Type[] GetHashAlgorithms(HashFunctionType type)
341+
{
342+
Type[] functions = null;
343+
if ((type & HashFunctionType.Cryptographic) == HashFunctionType.Cryptographic)
344+
{
345+
functions = GetAllCryptographicHashAlgorithms();
346+
}
347+
348+
if ((type & HashFunctionType.Noncryptographic) == HashFunctionType.Noncryptographic)
349+
{
350+
Type[] nonCrypto = GetAllNonCryptographicHashAlgorithms();
351+
if (functions == null)
352+
{
353+
functions = nonCrypto;
354+
}
355+
else
356+
{
357+
functions = functions.Union(nonCrypto).ToArray();
358+
}
359+
}
360+
361+
return functions;
362+
}
363+
364+
private static Type[] GetAllHashAlgorithms()
284365
{
285366
if (_implementations.Count < 1)
286367
{
@@ -292,15 +373,7 @@ public Type[] GetAllHashAlgorithms()
292373
return types;
293374
}
294375

295-
/// <summary>
296-
/// Retrieves all hash algorithm types that implement cryptographic hash functions.
297-
/// </summary>
298-
/// <remarks>This method filters the available hash algorithm types to include only those that implement the
299-
/// <see cref="ICryptographicHashFunction{T}"/> interface. The returned array may be empty if no matching types are
300-
/// found.</remarks>
301-
/// <returns>An array of <see cref="Type"/> objects representing cryptographic hash algorithm types. Returns <see
302-
/// langword="null"/> if no hash algorithms are available.</returns>
303-
public Type[] GetAllCryptographicHashAlgorithms()
376+
private static Type[] GetAllCryptographicHashAlgorithms()
304377
{
305378
Type[] all = GetAllHashAlgorithms();
306379
if (all == null || all.Length < 1)
@@ -314,16 +387,7 @@ public Type[] GetAllCryptographicHashAlgorithms()
314387
).ToArray();
315388
}
316389

317-
/// <summary>
318-
/// Retrieves all hash algorithm types that implement non-cryptographic hash functions.
319-
/// </summary>
320-
/// <remarks>This method filters the available hash algorithm types to include only those that implement the
321-
/// <see cref="IHashFunction{T}"/> interface and exclude those that implement the <see
322-
/// cref="ICryptographicHashFunction{T}"/> interface. The returned array may be empty if no matching types are
323-
/// found.</remarks>
324-
/// <returns>An array of <see cref="Type"/> objects representing non-cryptographic hash algorithm types. Returns <see
325-
/// langword="null"/> if no hash algorithms are available.</returns>
326-
public Type[] GetAllNonCryptographicHashAlgorithms()
390+
private static Type[] GetAllNonCryptographicHashAlgorithms()
327391
{
328392
Type[] all = GetAllHashAlgorithms();
329393
if (all == null || all.Length < 1)
@@ -422,4 +486,3 @@ public IHashFunctionBase Create(Type type, IHashConfigBase config)
422486
}
423487
}
424488
}
425-

0 commit comments

Comments
 (0)