Geeks With Blogs
Tex-blog Mobile and other stuff
Cryptography namespace is really very powerfull, as a test application I wrote a simple MD5 check utility that computes md5 checksum for file and compares it with md5 file that was provided with downloaded file. Because most of the files that are being checked are quite huge - I added simple user feedback as a dots showing in console.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
using System.Text.RegularExpressions;

namespace EncryptionTest
{
    class Program
    {
        static AutoResetEvent dotEvent = new AutoResetEvent(false);
        static void ConsoleDotWriter() {
            do {
                Console.Write(".");
            } while(!dotEvent.WaitOne(1500, false));
            Console.Write("\n");
        }
        static byte[] ComputeMD5ForFile(String filePath)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            using (FileStream fs = File.OpenRead(filePath)) {
                return md5.ComputeHash(fs);
            }
        }

        static void Main(string[] args)
        {
            if ( args.Length == 0 ) {
                Console.WriteLine("File name required");
                return;
            }
            if (File.Exists(args[0]))
            {
                // Create thread that will output dots - "." - while large files md5 is computed.
                ThreadStart ts = new ThreadStart(ConsoleDotWriter);
                Thread t = new Thread(ts);
                t.Start();
                byte[] md5 = ComputeMD5ForFile(args[0]);

                // Stop dot generating thread.
                dotEvent.Set();
                t.Join();

                // Output md5               
                String md5String="";
                foreach (byte bt in md5) { md5String += bt.ToString("X2"); }
                md5String = md5String.ToLower();
                Console.Write(md5String);

                // Check if there is a file to be compared with just computed md5 value
                String orgMd5File = args[0] + ".md5";
                if ( !File.Exists(orgMd5File) ) {
                    orgMd5File = args[0];
                    orgMd5File = Regex.Replace(orgMd5File, @"\.\w*$", ".md5");
                }
                if (File.Exists(orgMd5File))
                {
                    String orgMd5 = File.ReadAllText(orgMd5File);
                    int ind = orgMd5.IndexOf(md5String);
                    if (ind != -1)
                    {
                        Console.WriteLine("\nOK! - checksum in .md5 file was found");
                    }
                    else
                    {
                        Console.Write("\nError! - no such checksum in .md5 file was found\n");
                    }
                }
            }
        }
    }
} Posted on Sunday, September 23, 2007 5:06 PM 70-536 preparation | Back to top


Comments on this post: Simple MD5 command line check utility (Ch12)

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Martinez | Powered by: GeeksWithBlogs.net