public void Test_Decrypt_DecryptsLargeBlock(int bufferLength)
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext = RandomBytesGenerator.NextBytes(1024 * 1024);

                    using (var encryptionStream =
                               new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Encrypt, bufferLength, leaveOpen: true))
                    {
                        encryptionStream.Write(plaintext);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream =
                               new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Decrypt, bufferLength))
                    {
                        var decryptedPlainText  = new byte[plaintext.Length];
                        var numberOfBytesOutput = decryptionStream.Read(decryptedPlainText);

                        Assert.Equal(plaintext.Length, numberOfBytesOutput);
                        Assert.Equal(plaintext.ToArray(), decryptedPlainText);
                    }
                }
        }
        public void Test_Decrypt_OverReadDecryptionStream_OutputsCorrectNumberOfBytes()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext = TestConstants.MessageBytes;

                    using (var encryptionStream =
                               new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Encrypt, leaveOpen: true))
                    {
                        encryptionStream.Write(plaintext);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Decrypt))
                    {
                        var decryptedPlainText  = new byte[plaintext.Length * 2];
                        var numberOfBytesOutput = decryptionStream.Read(decryptedPlainText);

                        Assert.Equal(plaintext.Length, numberOfBytesOutput);
                        Assert.Equal(plaintext, decryptedPlainText.Take(plaintext.Length));
                    }
                }
        }
        public void Test_Decrypt_MultipleSmallBlocks()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext1 = RandomBytesGenerator.NextBytes(17);
                    var plaintext2 = RandomBytesGenerator.NextBytes(23);

                    using (var encryptionStream =
                               new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Encrypt, leaveOpen: true))
                    {
                        encryptionStream.Write(plaintext1);
                        encryptionStream.Write(plaintext2);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Decrypt))
                    {
                        var plaintextLength     = plaintext1.Length + plaintext2.Length;
                        var decryptedPlainText  = new byte[plaintextLength];
                        var numberOfBytesOutput = decryptionStream.Read(decryptedPlainText);

                        Assert.Equal(plaintextLength, numberOfBytesOutput);
                        Assert.Equal(plaintext1.Concat(plaintext2), decryptedPlainText);
                    }
                }
        }
Exemplo n.º 4
0
 public void BufferedStream()
 {
     this.bufferedCiphertextStream.Position = 0;
     using (var decryptionStream = new XChaChaBufferedStream(
                this.bufferedCiphertextStream, this.key, EncryptionMode.Decrypt, leaveOpen: true))
     {
         decryptionStream.Read(this.bufferedOutput);
     }
 }
        public void Test_Decrypt_VerifyEndOfMessage_TrueWhenFullyDecrypted()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext = RandomBytesGenerator.NextBytes(1024 * 1024);

                    using (var encryptionStream =
                               new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Encrypt, leaveOpen: true))
                    {
                        encryptionStream.Write(plaintext);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Decrypt))
                    {
                        var decryptedPlainText = new byte[plaintext.Length];

                        decryptionStream.Read(decryptedPlainText);
                        Assert.True(decryptionStream.VerifyEndOfMessage());
                    }
                }
        }