• 0

[C#.NET] MD5 Sum Help


Question

I can't figure out how to use the MD5 clases in System.Security.Cryptography. I'm trying to write a app that will get the MD5 sum of a file. There are more components to this app than just what you see, I can't figure out how to do this.

Box that should output the MD5 sum is: md5Box.Text

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Stream fileOpen;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.InitialDirectory = @"C:\";
        openFileDialog1.Filter = "All Files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if ((fileOpen = openFileDialog1.OpenFile()) != null)
            {
                HashAlgorithm e = new MD5CryptoServiceProvider();
                e.ComputeHash(fileOpen);

                 /* STUCK HERE */

            }
        }
    }
}

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

e.ComputeHash returns a byte array (should be 32 bits) if u want it to look like the standard u would see next to files on websites then u need to convert it to hex this gives u the 16 lenth string commonly found then u can put this in ur text box

to do the conversion to hex the following sample might help (its decompiled from one of my programs, cant find the source at the moment)

private string HashToString(byte[] b)
{
	string text1;
	string text2;
	string text3;
	int num1;

	text1 = "";
	text3 = "0";

	for (num1 = 0; (num1 < b.Length); num1 = (num1 + 1))
	{
  text2 = Convert.ToString(b[num1], 16);
  
  if (text2.Length != 2)
  {
 	 text2 = string.Concat(text3, text2);
  }
  
  text1 = string.Concat(text1, text2);
	}
	return text1; 
}

Edited by spike232
Link to comment
Share on other sites

  • 0

Well, thank you but now that I have gotten farther in the code and I get this error. I don't know what to do:

Cannot create an instance of the abstract class or interface 'System.Security.Cryptography.HashAlgorithm'

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

namespace WinHash
{
    partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stream fileOpen;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.Filter = "All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((fileOpen = openFileDialog1.OpenFile()) != null)
                {
                    HashAlgorithm h = new HashAlgorithm();
                    Converter cv = new Converter();
                    byte[] result = h.ComputeHash(fileOpen);
                    string hashVal = cv.HashToString(result);
                    md5Box.Text = hashVal;
                }
            }
        }
    }
    public class Converter
    {
        public string HashToString(byte[] b)
        {
            string text1;
            string text2;
            string text3;
            int num1;

            text1 = "";
            text3 = "0";

            for (num1 = 0; (num1 < b.Length); num1 = (num1 + 1))
            {
                text2 = Convert.ToString(b[num1], 16);

                if (text2.Length != 2)
                {
                    text2 = string.Concat(text3, text2);
                }

                text1 = string.Concat(text1, text2);
            }
            return text1;
        }
    }
}

Link to comment
Share on other sites

  • 0

You get it because HashAlgorithm is an abstract class. Abstract classes cannot be instantiated. You have to use the MD5CryptoServiceProvider class which inherits from MD5, which in turn inherits from HashAlgorithm.

MD5 md5= new MD5CryptoServiceProvider();

byte[] result = md5.ComputeHash( data );

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.