public void BufferedStream() { using (var encryptionStream = new XChaChaBufferedStream(Stream.Null, this.key, EncryptionMode.Encrypt)) { encryptionStream.Write(data); } }
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_UsingCompressionStream_DecryptsSmallBlock() { 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)) { using (var compressionStream = new BrotliStream(encryptionStream, CompressionMode.Compress)) { compressionStream.Write(plaintext); } } ciphertextStream.Position = 0; using (var decryptionStream = new XChaChaBufferedStream(ciphertextStream, key, EncryptionMode.Decrypt)) { using (var decompressionStream = new BrotliStream(decryptionStream, CompressionMode.Decompress)) { var decryptedPlainText = new byte[plaintext.Length]; var numberOfBytesOutput = decompressionStream.Read(decryptedPlainText); Assert.Equal(plaintext.Length, numberOfBytesOutput); Assert.Equal(plaintext, 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); } } }
public void Test_Encrypt_WriteDifferentAmountsToStream(int bufferLength) { 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; var numberOfBlocks = Math.Ceiling((decimal)totalPlainTextLength / bufferLength); var expectedOutputLength = StreamHeaderLength + totalPlainTextLength + (numberOfBlocks * StreamABytes); using (var outputStream = new MemoryStream()) { using (var key = XChaChaKey.Generate()) using (var encryptionStream = new XChaChaBufferedStream(outputStream, key, EncryptionMode.Encrypt, bufferLength)) { encryptionStream.Write(plaintext1); encryptionStream.Write(plaintext2); encryptionStream.Write(plaintext3); } var ciphertext = outputStream.ToArray(); Assert.Equal(expectedOutputLength, ciphertext.Length); } }
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 Encryption() { var bufferLength = this.BufferLengthKb * 1024; using (var encryptionStream = new XChaChaBufferedStream( Stream.Null, this.key, EncryptionMode.Encrypt, bufferLength)) { encryptionStream.Write(data); } }
public void GlobalSetup() { new Random(31).NextBytes(data); this.key = XChaChaKey.Generate(); using (var bufferedEncryptionStream = new XChaChaBufferedStream( this.bufferedCiphertextStream, this.key, EncryptionMode.Encrypt, leaveOpen: true)) { bufferedEncryptionStream.Write(data); } }
public void Test_Flush_FlushesHeader() { using (var outputStream = new MemoryStream()) using (var key = XChaChaKey.Generate()) using (var encryptionStream = new XChaChaBufferedStream(outputStream, key, EncryptionMode.Encrypt)) { var plaintext = Array.Empty <byte>(); encryptionStream.Write(plaintext); encryptionStream.Flush(); var ciphertext = outputStream.ToArray(); Assert.Equal(StreamHeaderLength, ciphertext.Length); } }
public void Test_Encrypt_WithLargeData_ProducesCorrectOutputLength(int bufferLength) { var plaintext = RandomBytesGenerator.NextBytes(1024 * 1024); using (var outputStream = new MemoryStream()) { using (var key = XChaChaKey.Generate()) using (var encryptionStream = new XChaChaBufferedStream(outputStream, key, EncryptionMode.Encrypt, bufferLength)) { encryptionStream.Write(plaintext); } var ciphertext = outputStream.ToArray(); var numberOfBlocks = Math.Ceiling((decimal)plaintext.Length / bufferLength); var expectedCipherTextLength = plaintext.Length + StreamHeaderLength + (StreamABytes * numberOfBlocks); Assert.Equal(expectedCipherTextLength, ciphertext.Length); } }
public void Test_Encrypt_ProducesCorrectOutputLength() { var plaintext = TestConstants.MessageBytes; using (var outputStream = new MemoryStream()) { using (var key = XChaChaKey.Generate()) using (var encryptionStream = new XChaChaBufferedStream(outputStream, key, EncryptionMode.Encrypt)) { encryptionStream.Write(plaintext); } var ciphertext = outputStream.ToArray(); // The encryption stream encrypts in 128KB blocks const int numberOfBlocks = 1; var expectedCipherTextLength = plaintext.Length + StreamHeaderLength + (StreamABytes * numberOfBlocks); Assert.Equal(expectedCipherTextLength, ciphertext.Length); } }
public void GlobalSetup() { var data = new byte[this.DataLengthKb * 1024]; new Random(31).NextBytes(data); this.nonBufferedCiphertextStream = new MemoryStream(); this.bufferedCiphertextStream = new MemoryStream(); this.key = XChaChaKey.Generate(); using (var bufferedEncryptionStream = new XChaChaBufferedStream( this.bufferedCiphertextStream, this.key, EncryptionMode.Encrypt, leaveOpen: true)) { bufferedEncryptionStream.Write(data); } using (var encryptionStream = new XChaChaStream( this.nonBufferedCiphertextStream, this.key, EncryptionMode.Encrypt, leaveOpen: true)) { encryptionStream.WriteFinal(data); } }
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()); } } }