Tuesday, October 27, 2009

Library of the Week: Silverlight’s MD5 implementation


While there are a slew of cryptographic hashing algorithms a programmer finds at their disposal, and despite the documented vulnerabilities accompanying it, MD5 remains a popular approach to generating hashes. Hashes, as you know, are useful for a variety of things – verifying file integrity and password storage being just a few.

I discovered the Silverlight MD5 implementation because a project I work on leveraged MD5 for file integrity. I was a bit curious about this since it’s long been a recommendation to skip MD5 and use hashing algorithms like SHA-1 and SHA-2 which are proven to be more robust. It’s not just the predecessor of my project, many places still use MD5 despite the bad rap it takes. I posted on SuperUser and got some interesting answers, the most notable of which (I marked as the answer) detailed how MD5 is faster than SHA-1. Other comments can be summarized with this excerpt of an answer that wwas most highly upvoted:

“A MD5 hash is "good enough" for most menial tasks. Recall that it's still incredibly difficult to produce meaningful collisions in the same number of bytes.”

Silverlight’s core assemblies do not support MD5 but a person named Reid Borsuk at Microsoft created an implementation that is robust and easy to use. Because the code is hosted on MSDN Code Gallery you will need to put it into a project on your own but it’s not that hard. Here are some simple steps:

1. Create a Silverlight Library project

2. Download the MD5.cs and MD5Managed.cs (you can also download the MD5Test.cs if you’re interested in unit testing it)

3. Add both files to your Silverlight Library project.

4. Compile and you’re done!

If' that’s not straightforward enough, I’ve created a downloadable Visual Studio 2008 solution with the library, a Silverlight Application and a Web application to show it in action. Here is the code behind generating a hash from a file stream in a button’s click event:

private void cryptoButton_Click(object sender, RoutedEventArgs e)
OpenFileDialog ofd = new OpenFileDialog();
if (true == ofd.ShowDialog()) {
using (FileStream fs = ofd.File.OpenRead())
MD5Managed md5 = new MD5Managed();
byte[] hash = md5.ComputeHash(fs);


Thanks a lot Reid!


Tuesday, October 20, 2009

Library Of The Week: Simple Styles for Silverlight


I’ll admit I have a Man Crush on Robbie Ingebretsen that began when I discovered his work at MIX earlier this year. To expound a bit: Microsoft has worked hard to try to draw a line in the sand between “developer” and “designer” – a line I always had a visceral dislike for because it’s always been people like Robbie Ingebretsen who I’ve always tried to pattern myself after: people who can write code AND produce beautiful designs.

One of the more difficult things I’ve encountered in Silverlight is dealing with control templates. Even something as simple as a button, when opened in Blend, involves quite a few different moving parts so that changing behavior doesn’t always produce a desired effect. For example: changing the background of the button at some given visual state will not affect the button unless you take into account a predefined background gradient that is a part of it.

The Simple Styles samples are stripped down, intuitive versions of the control templates with a concise starting point for anyone working to customize their behavior. To go back to the previous example, changing the background of a button involves changing the BackgroundElement. Rather than changing individual gradient stops in a Visual State, simple modify the entire element related to that Visual State.

Default Button Template Simple Button
102 lines of XAML 38 lines of XAML

The button is a starting point for simple conversations but there are controls like the listbox which can be deceptively unwieldy in how they are managed1.

The structure of the Simple Styles is also beautiful – not simply a resource dictionary with everything crammed together but a separate file for each control, a gesture one would think was straightforward until you see the many examples of Silverlight styles coming from Blend generated files.  

I mentioned previously that I think that the antidote to the “blank slate” of RIA is good precedent and patterns developers and designers turn to when they build things. I think Simple Styles are a step in that direction; what I wouldn’t give to look through Robbie’s resource dictionaries. How about it Robbie?


Thursday, October 08, 2009

Library Of The Week: ImageTools


I’m not exactly sure of the reasoning, but Silverlight does not have support for either GIF or BMP file formats. This is to say that if you use the Image control, you will be unsuccessful setting the source property to a GIF/BMP file. It’s heresay from nearly a year ago but a member of the Silverlight team, Ashish Thapliyal, posted on (his|her) blog the following:

“We don’t want to take the hit for another codec. It may only be a little bit of download time—but our mandate is small and fast and every little bit counts. We are also hesitant to support .gif because it implies support for animated .gif, which we decided would have to be integrated into our animation scheme somehow—and that will be a lot of work.

Looking from Flash perspective, Flash does not support .gif either. However, as of present, this is something being evaluated, but no date has been announced.”

Initially it was this roadblock that led me down the path to ImageTools. ImageTools is a project that provides Silverlight with good support for GIF and BMP amongst other formats, complete with its own Image control that allows a user to preview either format. It’s been a lifesaver for me on my current project since we have a requirement for preview capability with both.

ImageTools is more than just support for those antiquated formats. Once you’ve downloaded the latest release you’ll see there are a series of demos to look at:

This project shows how easy it is to load an image using the AnimatedImage type. All the sample files are GIF format.

This project shows the ability to apply brightness and contrast to images on the fly using the ImageTools.Image type.

Asynchronously loading an image file using ImageTools.Image with IsLoadingSynchronously flag.

Shows the ImageEditorContainer control and some editing features

Shows the ImageTools.Image.Transform method which supplies not just the ability to rotate, but also to “flip” the pixels within an image either on the X or Y axis (horizontally or vertically)

I’m not completely sure why this is called “loading” because there’s a lot more going on here: the AnimatedImage control is on display with the ability to apply filters to images that are loaded. The examples given are  Sepia, Inverter, and GrayscaleBT709.  The IImageFilter interface allows an extensible way to define filters, look into the ImageTools.Filtering library to see the interface and other filters available.

Shows dynamic resizing of an existing image (returns an Image object with resized Image type)

Pretty sick; this handy extension method can return an Image object representing any canvas.

As you can see, my simple problem of needing support for more image formats resulted in finding a library that went the distance in giving a developer flexibility with image manipulation. One more thing, I love the dependency injected goodness of how the library works. Here’s a little snippet of code to give you an idea of how well designed (extensible!) the library is:

1. Add a reference to ImageTools, ImageTools.IO.Bmp, ImageTools.IO.Gif

2. Here is code to load the files – notice the Decoders.AddDecoder takes the generic type parameter to dynamically support the desired file format which means you need only reference the format specific libraries that you need:

    public partial class MainPage : UserControl
public MainPage()
// add decoders


private void Button_Click(object sender, RoutedEventArgs e)
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == true)
// because decoders are taken care of,
// load images as needed!

ImageTools.Image i = new ImageTools.Image();
using(FileStream fs = ofd.File.OpenRead()){
// note: myImage is ImageTools.Controls.AnimatedImage
myImage.Source = i;



It's a bummer when support for things, like well known file formats like GIF are not supported via a more arbitrary decision making process but the consolation is that it’s an opportunity for people make up for it in the community. ImageTools is a great library, every Silverlight developer should check it out.