up
This commit is contained in:
@@ -14,10 +14,12 @@ public sealed class CryptoProviderRegistry : ICryptoProviderRegistry
|
||||
private readonly IReadOnlyDictionary<string, ICryptoProvider> providersByName;
|
||||
private readonly IReadOnlyList<string> preferredOrder;
|
||||
private readonly HashSet<string> preferredOrderSet;
|
||||
private readonly CryptoRegistryProfiles profiles;
|
||||
|
||||
public CryptoProviderRegistry(
|
||||
IEnumerable<ICryptoProvider> providers,
|
||||
IEnumerable<string>? preferredProviderOrder = null)
|
||||
IEnumerable<string>? preferredProviderOrder = null,
|
||||
CryptoRegistryProfiles? registryProfiles = null)
|
||||
{
|
||||
if (providers is null)
|
||||
{
|
||||
@@ -33,10 +35,17 @@ public sealed class CryptoProviderRegistry : ICryptoProviderRegistry
|
||||
providersByName = providerList.ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
|
||||
this.providers = new ReadOnlyCollection<ICryptoProvider>(providerList);
|
||||
|
||||
preferredOrder = preferredProviderOrder?
|
||||
var baseOrder = preferredProviderOrder?
|
||||
.Where(name => providersByName.ContainsKey(name))
|
||||
.Select(name => providersByName[name].Name)
|
||||
.ToArray() ?? Array.Empty<string>();
|
||||
profiles = registryProfiles ?? new CryptoRegistryProfiles(baseOrder, "default",
|
||||
new Dictionary<string, IReadOnlyList<string>>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["default"] = baseOrder
|
||||
});
|
||||
|
||||
preferredOrder = profiles.ResolvePreferredOrder();
|
||||
preferredOrderSet = new HashSet<string>(preferredOrder, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace StellaOps.Cryptography;
|
||||
|
||||
public sealed class CryptoRegistryProfiles
|
||||
{
|
||||
public IReadOnlyList<string> PreferredProviders { get; }
|
||||
public string ActiveProfile { get; }
|
||||
public IReadOnlyDictionary<string, IReadOnlyList<string>> Profiles { get; }
|
||||
|
||||
public CryptoRegistryProfiles(
|
||||
IEnumerable<string> preferredProviders,
|
||||
string activeProfile,
|
||||
IDictionary<string, IReadOnlyList<string>> profiles)
|
||||
{
|
||||
PreferredProviders = (preferredProviders ?? throw new ArgumentNullException(nameof(preferredProviders)))
|
||||
.Where(p => !string.IsNullOrWhiteSpace(p))
|
||||
.Select(p => p.Trim())
|
||||
.ToArray();
|
||||
|
||||
ActiveProfile = string.IsNullOrWhiteSpace(activeProfile)
|
||||
? throw new ArgumentException("Active profile is required", nameof(activeProfile))
|
||||
: activeProfile.Trim();
|
||||
|
||||
Profiles = new ReadOnlyDictionary<string, IReadOnlyList<string>>(profiles ??
|
||||
throw new ArgumentNullException(nameof(profiles)));
|
||||
}
|
||||
|
||||
public IReadOnlyList<string> ResolvePreferredOrder(string? profileName = null)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(profileName) && Profiles.TryGetValue(profileName!, out var specific))
|
||||
{
|
||||
return specific;
|
||||
}
|
||||
|
||||
if (Profiles.TryGetValue(ActiveProfile, out var active))
|
||||
{
|
||||
return active;
|
||||
}
|
||||
|
||||
return PreferredProviders;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user