New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Simple MD5 File Hash Checker

42 Views
Copy Code Show/Hide Line Numbers
using System;
 
using System.Security.Cryptography;
 
using System.IO;
 
using System.Text;
 
public class CompareFileAndMD5
 
{
 
    public static void Main(string[] args)
 
    {
 
        if (args.Length > 0) {
 
            if (args[0].Equals("/?", StringComparison.InvariantCultureIgnoreCase)) {
 
                Console.WriteLine("Usage: CompareFileAndMd5 filename md5");
 
            }
 
        }
 
 
 
        if (args.Length > 1) {
 
            if (File.Exists(args[0])) {
 
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
 
                byte[] toHash = (new UnicodeEncoding()).GetBytes(File.ReadAllText(args[0]));
 
                byte[] hashed = md5.ComputeHash(toHash);
 
                string hash = BitConverter.ToString(hashed);
 
                
 
                if (hash.Equals(args[1], StringComparison.InvariantCultureIgnoreCase)) {
 
                    Console.WriteLine("Hashes Matches");
 
                } else {
 
                    Console.WriteLine("NO MATCH!");
 
                    Console.WriteLine("MD5 Given: " + args[1]);
 
                    Console.WriteLine("Generated: " + hash);
 
                }
 
            } else {
 
                Console.WriteLine("Usage: CompareFileAndMd5 filename md5");
 
            }
 
        }
 
    }
 
}
by jwwishart
  December 11, 2009 @ 4:35am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate