Serializing POCO’s to Byte Arrays in C#

I have recently had a need to serialize objects in .NET down to a byte array to send to another system for a project that I was working on, so I thought I would share some of the code.

I have written about serializing POCO objects into XML before on this blog.

In the rest of this post I will show a simple implementation of a class called ObjectSerialize that adds a set of extension methods onto the base object class.

ObjectSerialize Class
ObjectSerialize Class

The ObjectSerialize class contains 2 public methods, Serialize and DeSerialize. These methods will be available as extension methods on the base object method in .NET. There are also 2 private methods (Compress and Decompress) that apply GZip compression to the object being serialized to ensure the byte arrays are as compact as possible.

Lets take a look at the Serialize method.

public static byte[] Serialize(this Object obj)
{
    if (obj == null)
    {
        return null;
    }

    using (var memoryStream = new MemoryStream())
    {
        var binaryFormatter = new BinaryFormatter();
        binaryFormatter.Serialize(memoryStream, obj);
        var compressed = Compress(memoryStream.ToArray());

        return compressed;
    }
}

This method takes an object and then serialises the object into a MemoryStream using a BinaryFormatter. The MemoryStream is then run thorough the compress method to apply the GZip compression. The resulting byte array is then returned.

Advertisements

Developer to Manager Pluralsight Course now Live

I am pleased to announce that my first Pluralsight course, Developer to Manager, is now live and available to watch for all Pluralsight subscribers. You can view a demo of the course below.

The course is based off an article I wrote earlier this year called Transition from Developer to Manager. This has been the single most popular article on this blog. I frequently receive emails from people asking advice on moving into a supervisor / leading role, so I hope this course will help everyone who watches it.

Stephen Haunts Pluralsight Author Page
Stephen Haunts Pluralsight Author Page

The course will start off by covering what a typical supervisor / management role looks like and its aim is to set the listeners expectations about the role to help them make an informed decision. The course then goes on to help the listener come up with a 90 day plan to help them make a real impact in the role if they choose to make the leap.

The structure of the course is as follows:

Module 1 : Introduction

Module 2 : What Does it Mean to be a Manager?

Module 3 : Your Team

Module 4 : Your First Month

Module 5 : Your Second Month

Module 6 : Your Third Month

Cryptography in .NET : Digital Signatures

I have previously written a number of articles on Cryptography in .NET, like the following :

Part 1 – Advanced Encryption Standard (AES)

Part 2 – RSA

Part 3 – Random Numbers and Hashes

Part 4 – Hybrid Encryption Protocols

Block Encrypter .NET Library for secure AES Encryption

In this article I will show you how to create and use Digital Signatures in .NET.

A digital signature is a mathematical scheme that demonstrates the authenticity of a message or document. A valid digital signature gives the recipient reason to believe that the message was created by a known sender, such that the sender cannot deny having sent the message (authentication and non-repudiation) and that the message was not altered in transit (integrity). Digital signatures are commonly used for software distribution, financial transactions, and in other cases where it is important to detect forgery or tampering.

Digital signatures are often used to implement a digital analog to hand written signatures. In broader terms this refers to any electronic data that carries the intent of a signature. Digital signatures employ a type of asymmetric cryptography. For messages sent through a non-secure channel, a properly implemented digital signature gives the receiver reason to believe the message was sent by the claimed sender. Digital signatures are equivalent to traditional handwritten signatures in many respects, but properly implemented digital signatures are more difficult to forge than the handwritten type. Digital signature schemes, in the sense used here, are cryptographic based, and must be implemented properly to be effective. Digital signatures can also provide non-repudiation, meaning that the signer cannot successfully claim they did not sign a message, while also claiming their private key remains secret.

Example Digital Signature Flow
Example Digital Signature Flow

A digital signature scheme consists of three algorithms

  • A key generation algorithm that generates a private and public key, such as RSA.
  • A signing algorithm that, given a message and a private key, produces a signature.
  • A signature verifying algorithm that, given a message, public key and a signature, either accepts or rejects the message’s claim to authenticity.

Two main properties are required. First, the authenticity of a signature generated from a fixed message and fixed private key can be verified by using the corresponding public key. Secondly, it should be computationally infeasible to generate a valid signature for a party without knowing that party’s private key. A digital signature is an authentication mechanism that enables the creator of the message to attach a code that act as a signature. It is formed by taking the hash of message and encrypting the message with creator’s private key.

My First Pluralsight Course Update

It’s been a while since I posted an update about my Pluralsight course development, but I am please to say that the course is now complete and has passed all of the peer review and audio/visual review stages. The course now goes off to be encoded into the right format and enters the release queue.

The course will be released on Friday 28th November.

Pluralsight : Developer to Manager : Screenshot
Pluralsight : Developer to Manager : Screenshot

It has been a lot of hard work but I have really enjoyed the process and feel honoured to be a part of the Plurasight family. They are a great company to work for. My first course is called “Developer to Manager” and it is a course that helps developers decide whether going into a team leadership or management role is the right decision for them.

The course is based off an article I wrote earlier this year called Transition from Developer to Manager. This has been the single most popular article on this blog a lot of my articles have been very popular. I frequently receive emails from people asking advice on moving into a supervisor / leading role, so I hope this course will help everyone who watches it.

The course will start off by covering what a typical supervisor / management role looks like and its aim is to set the listeners expectations about the role to help them make an informed decision. The course then goes on to help the listener come up with a 90 day plan to help them make a real impact in the role if they choose to make the leap.