Flow Group SASFlow Group
L'erp des Processus Relationnels
Systeme d'Information – Audit Industriel
> Home > Expertise & Knowledge > Technical Articles

Articles


Implementation of the MD5 Algorithm in C# for the Compact Framework on PocketPC

lire la version francophone Version française

Using the MD5 and MD5CryptoServiceProvider classes
Test Suite
Output
Some useful links...
Revision history...

The cryptography classes are not available on the Compact Framework 1.0, so we ported the "MD5 Message-Digest Algorithm" from RSA Data Security, Inc. in C#.

These classes are supported by the Compact Framework 2.0

Using the MD5 and MD5CryptoServiceProvider classes

With the .Net Framework, the MD5 hash of the buffer can be computed this way:

using System; using System.Text; using System.Security.Cryptography; ... byte[] ComputeMD5(byte[] buffer) { //Create the md5 hash provider. MD5 md = new MD5CryptoServiceProvider(); //Compute the hash value from the array of bytes. return md.ComputeHash(buffer); } ...

We implemented both classes, trying to keep the same signatures for their methods. So we switch to our implementation simply by changing the using.

using System; using System.Text; //using System.Security.Cryptography; using FlowGroup.Crypto; ... byte[] ComputeMD5(byte[] buffer) { //Create the md5 hash provider. MD5 md = new MD5CryptoServiceProvider(); //Compute the hash value from the array of bytes. return md.ComputeHash(buffer); } ...
For your information

We could have used the following methods to get the hash algoritm:

  • MD5 md = MD5.Create();
  • MD5 md = MD5.Create("MD5");

But, HashAlgorithm doest not exist on the Compact Framework and we did not defined it, so the following code does not work:

  • HashAlgorithm md = HashAlgorithm.Create("MD5");

Test Suite

The code can be downloaded here. As a sample demonstrating how to use it, let's implement the test suite from the RFC 1321.

using System; using System.Text; using FlowGroup.Crypto; namespace DemoMD5 { /// <summary> /// Summary description for Application. /// </summary> class Application { static private void MDString(string s) { MD5 md = MD5CryptoServiceProvider.Create(); byte[] hash; //Create a new instance of ASCIIEncoding to //convert the string into an array of Unicode bytes. ASCIIEncoding enc = new ASCIIEncoding(); //Convert the string into an array of bytes. byte[] buffer = enc.GetBytes(s); //Create the hash value from the array of bytes. hash = md.ComputeHash(buffer); //Display the hash value to the console. Console.Write("MD5 (\"{0}\") = ", s); foreach(byte b in hash) { Console.Write(b.ToString("x2")); } Console.WriteLine(""); } /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { Console.WriteLine("MD5 test suite:"); MDString(""); MDString("a"); MDString("abc"); MDString("message digest"); MDString("abcdefghijklmnopqrstuvwxyz"); MDString("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"); MDString("123456789012345678901234567890" + "123456789012345678901234567890" + "12345678901234567890"); } } }

Output

The output is the following, as excepted from RFC 1321 :

MD5 test suite:¶
MD5 ("") = d41d8cd98f00b204e9800998ecf8427e¶
MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661¶
MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72¶
MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0¶
MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b¶
MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = 
d174ab98d277d9f5a5611c2c9f419d9f¶
MD5 ("123456789012345678901234567890123456789012345678901234567890123456
78901234567890") = 57edf4a22be3c955ac49da2e2107b67a¶

Some useful links...

Revision history...

daterévision
29/12/2005 00:00Add the implementation of:
  • public byte[] ComputeHash(Stream inputStream)
  • public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  • public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
Modify HashCore to allow it to be called several times.

all the informations here are provided as is, without any warranty of any kind.
© 2015 Flow Group SAS - Flow Group est une marque déposée de GL Conseil SA.