Esempio n. 1
0
        public async Task CompletesWithLastRead()
        {
            var pipe      = new Pipe();
            var encryptor = new PipelinedSimpleModulusEncryptor(pipe.Writer);

            encryptor.Writer.Write(new byte[] { 0xC1, 0x03, 0x01 });
            encryptor.Writer.Complete();
            var result = await pipe.Reader.ReadAsync().ConfigureAwait(false);

            Assert.That(result.IsCompleted, Is.True);
        }
Esempio n. 2
0
        public async Task CompleteAfterReadAsyncIsCalled()
        {
            var pipe      = new Pipe();
            var encryptor = new PipelinedSimpleModulusEncryptor(pipe.Writer);
            var reading   = pipe.Reader.ReadAsync();

            encryptor.Writer.Complete();
            var result = await reading;
            await Task.Delay(10).ConfigureAwait(false);

            Assert.That(result.IsCompleted, Is.True);
        }
Esempio n. 3
0
        public async Task CompleteAfterReadAsyncIsCalled()
        {
            var pipe      = new Pipe();
            var encryptor = new PipelinedSimpleModulusEncryptor(pipe.Writer);
            var completed = false;

            pipe.Reader.OnWriterCompleted((e, o) => completed = true, null);
            var reading = pipe.Reader.ReadAsync();

            encryptor.Writer.Complete();
            var result = await reading;

            Assert.That(completed && result.IsCompleted, Is.True);
        }
Esempio n. 4
0
        private async Task Encrypt(byte[] decrypted, byte[] encrypted)
        {
            var pipe      = new Pipe();
            var encryptor = new PipelinedSimpleModulusEncryptor(pipe.Writer);

            encryptor.Writer.Write(decrypted);
            await encryptor.Writer.FlushAsync().ConfigureAwait(false);

            var readResult = await pipe.Reader.ReadAsync().ConfigureAwait(false);

            var result = readResult.Buffer.ToArray();

            Assert.That(result, Is.EquivalentTo(encrypted));
        }
        /// <summary>
        /// Tests the encryption-decryption cycle for the packet from server to client. The specified packet must be the same after the packet has passed this cycle.
        /// Packets from server to client are never encrypted by Xor32, so these encryptor/decryptors are not involved here.
        /// </summary>
        /// <param name="packet">The packet.</param>
        /// <returns>The task.</returns>
        private async Task EncryptDecryptFromServerToClient(byte[] packet)
        {
            // this pipe connects the encryptor with the decryptor. You can imagine this as the server-to-client pipe of a network socket, for example.
            var pipe = new Pipe();

            var encryptor = new PipelinedSimpleModulusEncryptor(pipe.Writer);
            var decryptor = new PipelinedSimpleModulusDecryptor(pipe.Reader, PipelinedSimpleModulusDecryptor.DefaultClientKey);

            encryptor.Writer.Write(packet);
            await encryptor.Writer.FlushAsync().ConfigureAwait(false);

            var readResult = await decryptor.Reader.ReadAsync().ConfigureAwait(false);

            var result = readResult.Buffer.ToArray();

            Assert.That(result, Is.EquivalentTo(packet));
        }
Esempio n. 6
0
        public async ValueTask SimpleModulusEncryption()
        {
            var pipe = new Pipe();
            var pipelinedEncryptor = new PipelinedSimpleModulusEncryptor(pipe.Writer);
            var readBuffer         = new byte[256];

            for (int i = 0; i < PacketCount; i++)
            {
                await pipelinedEncryptor.Writer.WriteAsync(this.c3Packet);

                await pipelinedEncryptor.Writer.FlushAsync();

                var readResult = await pipe.Reader.ReadAsync();

                readResult.Buffer.CopyTo(readBuffer);
                //// In the server, I would process the readBuffer here
                pipe.Reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
            }

            pipelinedEncryptor.Writer.Complete();
        }