Esempio n. 1
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);
                }
        }
Esempio n. 2
0
        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 XChaChaStream(ciphertextStream, key, EncryptionMode.Encrypt, leaveOpen: true))
                    {
                        encryptionStream.WriteFinal(plaintext);
                    }

                    ciphertextStream.Position = 0;

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

                        decryptionStream.Read(decryptedPlainText);
                        Assert.True(decryptionStream.VerifyEndOfMessage());
                    }
                }
        }
Esempio n. 3
0
        public void Test_Decrypt_OverReadDecryptionStream_OutputsCorrectNumberOfBytes()
        {
            using (var ciphertextStream = new MemoryStream())
                using (var key = XChaChaKey.Generate())
                {
                    var plaintext = TestConstants.MessageBytes;

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

                    ciphertextStream.Position = 0;

                    using (var decryptionStream = new XChaChaStream(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 GlobalSetup()
 {
     this.data = new byte[this.DataLengthKb * 1024];
     new Random(31).NextBytes(data);
     this.key = XChaChaKey.Generate();
 }