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

Monday, January 31, 2011

Deleting files past a certain amount of days

Recently I have come across a need to create a console tool to delete files after X amount of days and found that there wasn't any good tutorials or examples out for this language. So I decided to write my own version. This one currently does not support entering a target directory. If I get enough attention on this I will make one. Also if you like this tool give me a shout out in the comments.


Purpose:
Delete files modifed after X amount of days.

Code:
using System;
using System.IO;

//  Developed by PhyberOptic
//  PhyberOptic.blogger.com
//
//  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 .

class Program
{
    public static bool delete = false;
    public static int number = 0;
    static Int32 Main(string[] args)
    {
        if (args.Length < 2)
        {
            Console.WriteLine("Checks last file modification times greater than (#) days\r\nand if found deletes if set to true.\r\n\r\nUsage: {0} [days] [delete:true|false]\r\n\r\nExample:{0} 7 true", Environment.GetCommandLineArgs()[0]);//, Environment.GetCommandLineArgs()[0]);
            return 0;
        }
        if(args[1] == "true")
        {
            delete = true;
        }

        string[] files = Directory.GetFiles(Environment.CurrentDirectory, "*.*",  SearchOption.AllDirectories);

        // Display all the files.
        foreach (string file in files)
        {
            FileInfo fileInfo = new FileInfo(file);
            DateTime dt = fileInfo.LastWriteTime;
            DateTime dtToday = System.DateTime.Now;
            if (DateDiff(dt, dtToday) >= Convert.ToInt32(args[0]))
            {
                if (delete)
                {
                    File.Delete(file);
                    Console.WriteLine("{0}: Deleted", file);
                }
                else
                {
                    Console.WriteLine(file);
                }
            }
        }
        return 0;
    }

    public static int DateDiff(DateTime StartDate, DateTime EndDate)
    {
        int NumDaysDiff;
        TimeSpan ts = EndDate.Subtract(StartDate);
        NumDaysDiff = ts.Days;
        return NumDaysDiff;
    }
    
}
Download here: Here

Anti Spam Campaign

Saturday, January 8, 2011

PS3Proxy Tool

Lately, I have been developing a new proxy tool mainly for backup, Debugging, etc. This tool can also be used for the playstation as I have encountered and works better and has more features then the previous versions like CF3B5 or 3r1c. Allowing options such as save log, Auto-Download files, Configuration settings, Auto-select Network IP address(No more ipconfig copy&paste!) just to start out with.

Tell me what you think about this.

Sample




An elephant