About Me

Age:25 --- B.E.(Computer Science), Android/Java Developer, CCNA, CCNA SECURITY (IINS), CCNP (R&S), 4011 Recognized(NSA & CNSS)U.S.A. , MCSA, MCTS, REDHAT CERTIFIED NETWORK SECURITY ADMINISTRATOR(RH253), AFCEH.

Friday, March 21, 2014

C# code to calculate the Checksum using MD5

       



/**
 * Coded by Kumar Vivek Mitra 16_1_2014, for comparing the checksum of files.
 *
 */



using System.Security.Cryptography;
using System.IO;
using System;

class CheckSumTest
 {
   static void Main()
    {

      CheckSumTest instance  = new CheckSumTest();
      

      System.Console.WriteLine("Generating Checksum.................." );
      System.Console.WriteLine();
      string FileOneCheckSum = instance.GetCheckSum("D:\\VIVEK_1_1_2014\\csharpworkspace\\ABCD Bezubaan 320Kbps.mp3");
      string FileTwoCheckSum = instance.GetCheckSum("D:\\VIVEK_1_1_2014\\csharpworkspace\\test_cbr.mp3");
      System.Console.WriteLine("File One CheckSum :"+ FileOneCheckSum);
      System.Console.WriteLine("File Two CheckSum :"+ FileTwoCheckSum );
      System.Console.WriteLine("================ Comparing ============");
      

      int Differ = instance.CheckKaro(FileOneCheckSum ,FileTwoCheckSum);

      if(Differ == 0)
        {

           System.Console.WriteLine("Both files are exactly same");
        }
      else
        {

           System.Console.WriteLine("Both files are different");
        }
    }




   // Method to check checksum of files

   public int CheckKaro(string strOne, string strTwo)
    {

      return string.Compare(strOne,strTwo);

    }




   // Method to obtain the checksum of file

   public string GetCheckSum(string file)
    {
      

      using (var md5 = MD5.Create())
      {
        using (var stream = File.OpenRead(file))

         {

           return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-","").ToLower();
         }

      }
      

    }



 }



      
 

No comments:

Post a Comment