|
using System;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using Clifton.Tools.Data;
using Vts.UnitTest;
namespace UnitTests
{
[TestFixture]
public class StreamingTests
{
[Test]
public void EncryptionStreaming()
{
// string -> RawStreamEncoder -> Encryptor -> MemoryStream
MemoryStream ms = new MemoryStream(); // final destination stream
EncryptTransformer et = new EncryptTransformer(EncryptionAlgorithm.Des);
ICryptoTransform ict = et.GetCryptoServiceProvider(null);
CryptoStream encStream = new CryptoStream(ms, ict, CryptoStreamMode.Write);
RawSerializer rs=new RawSerializer(encStream); // serializer outputs to encoder
rs.Serialize("Hello World"); // serialize
((CryptoStream)encStream).FlushFinalBlock(); // MUST BE APPLIED! Flush() does not output the last block.
ms.Position=0;
// MemoryStream -> Decryptor -> RawStreamDecoder -> string
DecryptTransformer dt = new DecryptTransformer(EncryptionAlgorithm.Des);
dt.IV = et.IV;
ict = dt.GetCryptoServiceProvider(et.Key);
CryptoStream decStream = new CryptoStream(ms, ict, CryptoStreamMode.Read);
RawDeserializer rd=new RawDeserializer(decStream); // Deserializes from decryptor stream
string str=(string)rd.Deserialize(typeof(string)); // Gets the data.
Assertion.Assert(str=="Hello World", "Unexpected return.");
}
[Test]
public void CompressionStreaming()
{
// string -> RawStreamEncoder -> Compressor -> MemoryStream
MemoryStream ms=new MemoryStream(); // final destination stream
GZipStream comp = new GZipStream(ms, CompressionMode.Compress, true); // important to be set to true!
RawSerializer rs=new RawSerializer(comp); // serializer outputs to compression
rs.Serialize("Hello World"); // serialize
comp.Close(); // outputs last part of the data
ms.Position=0;
// MemoryStream -> Decompressor -> RawStreamDecoder -> string
GZipStream decomp = new GZipStream(ms, CompressionMode.Decompress);
RawDeserializer rd=new RawDeserializer(decomp); // Deserializes from decompressor stream
string str=(string)rd.Deserialize(typeof(string)); // Gets the data.
Assertion.Assert(str=="Hello World", "Unexpected return.");
}
[Test]
public void CompressionEncryptionStreaming()
{
// string -> RawStreamEncoder -> Compressor -> Encryptor -> MemoryStream
MemoryStream ms=new MemoryStream(); // final destination stream
EncryptTransformer et = new EncryptTransformer(EncryptionAlgorithm.Des);
ICryptoTransform ict = et.GetCryptoServiceProvider(null);
CryptoStream encStream = new CryptoStream(ms, ict, CryptoStreamMode.Write);
GZipStream comp = new GZipStream(encStream, CompressionMode.Compress, true); // important to be set to true!
RawSerializer rs = new RawSerializer(comp); // serializer outputs to compression
rs.Serialize("Hello World"); // serialize
comp.Close(); // must close to get final bytes
((CryptoStream)encStream).FlushFinalBlock(); // MUST BE APPLIED! Flush() does not output the last block.
// Reset the position and read the stream back in.
ms.Position=0;
// MemoryStream -> Decryptor -> Decompressor -> RawStreamDecoder -> string
DecryptTransformer dt = new DecryptTransformer(EncryptionAlgorithm.Des);
dt.IV = et.IV;
ict = dt.GetCryptoServiceProvider(et.Key);
CryptoStream decStream = new CryptoStream(ms, ict, CryptoStreamMode.Read);
GZipStream decomp = new GZipStream(decStream, CompressionMode.Decompress);
RawDeserializer rd = new RawDeserializer(decomp); // Deserializes from decompressor stream
string str=(string)rd.Deserialize(typeof(string)); // Gets the data.
Assertion.Assert(str=="Hello World", "Unexpected return.");
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.