Thursday, December 28, 2017

GetMD5

This is my first post. So I will make this simple.

Purpose:
Get MD5 hash of a file.

Code:
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

//  Developed by PhyberOptic
//  
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see .

namespace GetMD5
{
   public partial class Form1 : Form
   {
       private static StringBuilder build = new StringBuilder();

       public Form1()
       {
           InitializeComponent();
       }

       private void txtFile_DragEnter(object sender, DragEventArgs e)
       {
           try
           {
               if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
               {
                   if (txtMD5.Text.Length > 0)
                   {
                       txtMD5.Clear();
                       build.Clear();
                   }
                   e.Effect = DragDropEffects.All;
               }
               else
               {
                   e.Effect = DragDropEffects.None;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("Error: " + ex.Message);
           }
       }

       private void txtFile_DragDrop(object sender, DragEventArgs e)
       {
           string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop);
           txtFile.Text = (fileName[0]);
           txtMD5.Text = getmd5(txtFile.Text);
       }

       private void btnMD5_Click(object sender, EventArgs e)
       {
           try
           {
               if (txtMD5.Text.Length > 0)
               {
                   txtMD5.Clear();
                   build.Clear();
               }

               OpenFileDialog open = new OpenFileDialog();
               open.CheckFileExists = true;
               open.CheckPathExists = true;
               open.ShowDialog();

               MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
               FileStream File = new FileStream(open.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
               txtFile.Text = (open.FileName);
               MD5.ComputeHash(File);
               byte[] hash = MD5.Hash;

               foreach (byte num in hash)
               {
                   build.Append(String.Format("{0:X2}", num).Trim());
                   txtMD5.Text = build.ToString();
               }
               File.Close();
           }
           catch (NullReferenceException ex)
           {
               MessageBox.Show("NullReferenceException: " + ex.Message);
           }
       }

       private string getmd5(string filename)
       {
           try
           {
               MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
               FileStream File = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);// open.FileName
               MD5.ComputeHash(File);
               byte[] hash = MD5.Hash;

               foreach (byte num in hash)
               {
                   build.Append(String.Format("{0:X2}", num).Trim());
                   build.ToString();
               }
               File.Close();
           }
           catch (NullReferenceException ex)
           {
               MessageBox.Show("NullReferenceException: " + ex.Message);
           }
           return build.ToString();
       }

       private void txtMD5_TextChanged(object sender, EventArgs e)
       {
           try
           {
               if (this.Text.Length > 32)
               {
                   txtMD5.Clear();
               }

                   lblLength.Text = "Length: " + txtMD5.Text.Length.ToString();
           }
           catch (Exception ex)
           {
               MessageBox.Show("Error: " + ex.Message);
           }
       }

       private void linkLabel1_MouseDown(object sender, MouseEventArgs e)
       {
           if (txtMD5.Text.Length > 0)
               Clipboard.SetText(txtMD5.Text);
           else
               MessageBox.Show("No hash to Copy!");
       }
   }
}
Download here: Here

Anti Spam Campaign