using System; using System.Security.Cryptography; using System.Text; namespace GameCult.Networking { /// /// Carries the client-side networking configuration required to talk to a server. /// public sealed class ClientSecurityOptions { private readonly byte[] _encryptionKey; /// /// Shared LiteNetLib connection key and encryption seed. /// public string ConnectionKey { get; } /// /// Initializes a new client security configuration. /// /// The shared connection key used by both client and server. public ClientSecurityOptions(string connectionKey) { if (string.IsNullOrWhiteSpace(connectionKey)) throw new ArgumentException("Connection key must be provided.", nameof(connectionKey)); ConnectionKey = connectionKey; _encryptionKey = ComputeSha256(Encoding.UTF8.GetBytes(connectionKey)); } /// /// Creates a deterministic development-only configuration. /// public static ClientSecurityOptions Development() { return new ClientSecurityOptions("gamecult-dev-connection-key"); } internal byte[] GetEncryptionKey() { return _encryptionKey; } private static byte[] ComputeSha256(byte[] data) { using var sha256 = SHA256.Create(); return sha256.ComputeHash(data); } } }