|
Fiches TechniqueImplementation of the MD5 Algorithm in C# for the Compact Framework on PocketPC
The cryptography classes are not available on the Compact Framework, so we ported the "MD5 Message-Digest Algorithm" from RSA Data Security, Inc. in C#. Test SuiteThe 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"); } } } OutputThe 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¶ Not implemented functionsThe following functions have not been implemented yet and throw System.NotSupportedException()
Some useful links...
all the informations here are provided as is, without any warranty of any kind. Copyright © FlowGroup SAS, 2002-2005 |