Example #1
0
        public void Test_Encrypt_WriteDifferentAmountsToStream()
        {
            var plaintext1 = RandomBytesGenerator.NextBytes(157 * 1024);
            var plaintext2 = RandomBytesGenerator.NextBytes(314 * 1024);
            var plaintext3 = RandomBytesGenerator.NextBytes(273 * 1024);

            var       totalPlainTextLength = plaintext1.Length + plaintext2.Length + plaintext3.Length;
            const int numberOfWrites       = 3;
            var       expectedOutputLength = StreamHeaderLength + totalPlainTextLength + (numberOfWrites * StreamABytes);

            using (var outputStream = new MemoryStream())
            {
                using (var key = XChaChaKey.Generate())
                    using (var encryptionStream = new XChaChaStream(outputStream, key, EncryptionMode.Encrypt))
                    {
                        encryptionStream.Write(plaintext1);
                        encryptionStream.Write(plaintext2);
                        encryptionStream.WriteFinal(plaintext3);
                    }

                var ciphertext = outputStream.ToArray();

                Assert.Equal(expectedOutputLength, ciphertext.Length);
            }
        }
Example #2
0
        public void Test_Decrypt_VerifyEndOfMessage_FalseWhenPartiallyDecrypted()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext1 = RandomBytesGenerator.NextBytes(1024);
                    var plaintext2 = RandomBytesGenerator.NextBytes(1024);

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

                    ciphertextStream.Position = 0;

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

                        decryptionStream.Read(decryptedPlainText);
                        Assert.False(decryptionStream.VerifyEndOfMessage());
                    }
                }
        }
Example #3
0
        public void Test_Decrypt_DecryptsTwoBlocks()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    const int blockLength = 128 * 1024;
                    var       block1      = RandomBytesGenerator.NextBytes(blockLength);
                    var       block2      = RandomBytesGenerator.NextBytes(blockLength);

                    using (var encryptionStream = new XChaChaStream(ciphertextStream, key, EncryptionMode.Encrypt, leaveOpen: true))
                    {
                        encryptionStream.Write(block1);
                        encryptionStream.WriteFinal(block2);
                    }

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaStream(ciphertextStream, key, EncryptionMode.Decrypt))
                    {
                        var decryptedBlock1      = new byte[blockLength];
                        var decryptedBlock2      = new byte[blockLength];
                        var numberOfBytesOutput1 = decryptionStream.Read(decryptedBlock1);
                        var numberOfBytesOutput2 = decryptionStream.Read(decryptedBlock2);

                        Assert.Equal(blockLength, numberOfBytesOutput1);
                        Assert.Equal(block1, decryptedBlock1);
                        Assert.Equal(blockLength, numberOfBytesOutput2);
                        Assert.Equal(block2, decryptedBlock2);
                    }
                }
        }
Example #4
0
        public void Test_Encrypt_EncryptsTwoBlocks()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    const int blockLength = 128 * 1024;
                    var       block1      = RandomBytesGenerator.NextBytes(blockLength);
                    var       block2      = RandomBytesGenerator.NextBytes(blockLength);

                    using (var encryptionStream = new XChaChaStream(ciphertextStream, key, EncryptionMode.Encrypt))
                    {
                        encryptionStream.Write(block1);
                        encryptionStream.WriteFinal(block2);
                    }

                    var ciphertext = ciphertextStream.ToArray();

                    const int numberOfWrites           = 2;
                    const int expectedCiphertextLength = StreamHeaderLength + 2 * blockLength + (numberOfWrites * StreamABytes);

                    Assert.Equal(expectedCiphertextLength, ciphertext.Length);
                }
        }