public async Task ValidateCryptoHandshake()
        {
            var     server = new WsServer();
            var     serverKey = CryptoExtensions.GenerateKey();
            var     clientKey = CryptoExtensions.GenerateKey();
            ECPoint serverPub, clientPub = new ECPoint();
            await server.StartAsync(new IPEndPoint(IPAddress.Loopback, 0), async stream =>
            {
                clientPub = await stream.EncryptAsync(serverKey);
            });

            await Task.Delay(500);

            // start client
            var client = new WsClient();
            var conn   = await client.ConnectAsync(new Uri($"ws://" + server.ListeningAddresses[0].Substring(7)));

            serverPub = await conn.EncryptAsync(clientKey);

            await Task.Delay(500);

            Assert.Equal(serverKey.Q.X, serverPub.X);
            Assert.Equal(clientKey.Q.X, clientPub.X);
            server.Dispose();
            client.Dispose();
        }
Beispiel #2
0
        public async Task Test()
        {
            // create the server
            var server    = new WsServer();
            var serverKey = CryptoExtensions.GenerateKey();
            var clientKey = CryptoExtensions.GenerateKey();

            Console.WriteLine($"Server key is: [{serverKey.Q.GetFingerprintString()}]");
            Console.WriteLine($"Client key is: [{clientKey.Q.GetFingerprintString()}]");
            await server.StartAsync(new IPEndPoint(IPAddress.Any, 8080), async stream =>
            {
                // encrypt
                // called when a client connects
                var br = new BinaryWriter(stream);
                var ct = Encoding.ASCII.GetBytes($"Hello from Server! {DateTime.Now}");
                br.Write(ct.Length);
                br.Write(ct);
                var res = await stream.EncryptAsync(serverKey);
                Console.WriteLine($"Client connected with fingerprint of [{res.GetFingerprintString()}]");
                while (stream.Connected)
                {
                    var bytes = Encoding.ASCII.GetBytes($"Hello from Server! {DateTime.Now}");
                    br.Write(bytes.Length);
                    br.Write(bytes);
                    await Task.Delay(1000);
                }
                await stream.CloseAsync();
            });

            // start client
            var client     = new WsClient();
            var connection = await client.ConnectAsync(new Uri("ws://localhost:8080"));

            // measure client data

            // read data
            var binReader = new BinaryReader(connection);
            int e         = binReader.ReadInt32();
            var g         = binReader.ReadBytes(e);

            Console.WriteLine(Encoding.ASCII.GetString(g));

            var serverRes = await connection.EncryptAsync(clientKey);

            Console.WriteLine($"Connected to server with fingerprint of [{serverRes.GetFingerprintString()}]");

            for (int i = 0; i < 100; i++)
            {
                int b         = binReader.ReadInt32();
                var bytesRead = binReader.ReadBytes(b);
                Console.WriteLine(Encoding.ASCII.GetString(bytesRead));
            }
            // cleanup
            client.Dispose();
            server.Dispose();
        }
        public void Decode_AfterEncode_ReturnsOriginalValue()
        {
            var original = "ZWNBAgL5cLH6UaDs";
            var key      = CryptoExtensions.GenerateKey();

            var encoded = original.Encode(key);
            var decoded = encoded.Decode(key);

            encoded.Should().NotBe(decoded).And.Should().NotBe(original);
            decoded.Should().Be(original);
        }