Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Relationship Oriented Programming

Rate me:
Please Sign up or sign in to vote.
4.87/5 (20 votes)
12 Dec 2011CPOL14 min read 57.8K   805   58  
Modeling the Romeo and Juliet meta-model.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions