/ GUNRPG.Infrastructure / Security / AuthoritySet.cs
AuthoritySet.cs
 1  namespace GUNRPG.Security;
 2  
 3  public sealed class AuthoritySet
 4  {
 5      private readonly HashSet<string> _allowedKeys;
 6  
 7      public AuthoritySet(IEnumerable<Authority> authorities)
 8      {
 9          ArgumentNullException.ThrowIfNull(authorities);
10  
11          _allowedKeys = new HashSet<string>(
12              authorities.Select(static authority =>
13              {
14                  ArgumentNullException.ThrowIfNull(authority);
15                  return CreateKeyIdentifier(authority.PublicKeyBytes);
16              }),
17              StringComparer.Ordinal);
18      }
19  
20      public bool IsTrusted(byte[] publicKey)
21      {
22          return _allowedKeys.Contains(CreateKeyIdentifier(publicKey));
23      }
24  
25      internal static string CreateKeyIdentifier(byte[] publicKey)
26      {
27          return Convert.ToHexString(AuthorityCrypto.CloneAndValidatePublicKey(publicKey));
28      }
29  }