namespace DigitalData.Core.Security.Cryptographer { public static class CryptographerExtensions { public static IEnumerable GetByIssuer(this IEnumerable cryptographers, string issuer) where TRSACryptographer: RSACryptographer => cryptographers.Where(c => c.Issuer == issuer); public static IEnumerable GetByAudience(this IEnumerable cryptographers, string audience) where TRSACryptographer : RSACryptographer => cryptographers.Where(c => c.Audience == audience); public static TRSACryptographer Get(this IEnumerable cryptographers, string issuer, string audience) where TRSACryptographer : RSACryptographer => cryptographers.Where(c => c.Issuer == issuer && c.Audience == audience).SingleOrDefault() ?? throw new InvalidOperationException($"No {cryptographers.GetTypeName()} found with Issuer: {issuer} and Audience: {audience}"); public static bool TryGet(this IEnumerable cryptographers, string issuer, string audience, out TRSACryptographer? cryptographer) where TRSACryptographer : RSACryptographer { cryptographer = cryptographers.Where(c => c.Issuer == issuer && c.Audience == audience).SingleOrDefault(); return cryptographer is not null; } public static RSACryptographerList ToCryptographerList(this IEnumerable cryptographers, Func keyGenerator) where TRSACryptographer : RSACryptographer => new(keyGenerator: keyGenerator, cryptographers: cryptographers); private static string GetTypeName(this IEnumerable cryptographers) where TRSACryptographer : RSACryptographer => typeof(TRSACryptographer).GetType().Name.TrimStart(InterfaceIdentifierChar); private static readonly char InterfaceIdentifierChar = 'I'; } }