Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I am currently working on a project and my friend told me what I need to do to progress but he didnt tell me how to so now im staring into the screen without knowing what to do.

This is what he said


Within your click handler you can call functions. So why not add that
function to your form class. Then call it passing in the filename that
you got when you filled in the filename textbox? Store the result of
the function into the text for the hash.


What I am trying to do is to compute the MD5 Hash from a file that I select on my PC. This is what my code looks like.

C#
namespace MD5_Hash

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

        public string MD5HashFile(string fn)
        {
            byte[] hash = MD5.Create().ComputeHash(File.ReadAllBytes(fn));
            return BitConverter.ToString(hash).Replace("-", "");
        }

        private void lblTitle_Load(object sender, EventArgs e)
        {

        }

        private void scanButton_Click(object sender, EventArgs e)
        {
            //Right here
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtFilePath.Text = (ofd.FileName);

            }
        }
    }
}


basically im trying to use the
scanButton
to compute the MD5 Hash from the file that I get from
private void browseButton_Click



I kinda have an idea of what he is talking about but not exactly because I am new to Cryptography.

If you could simplify what he is asking me to do I would be VERY greatful!

What I have tried:

I've tried calling the hash but I know I am doing it completly wrong
Posted
Updated 7-May-16 4:12am

Read the filename from your textbox:
C#
string path = txtFilePath.Text;

Check the file exists:
C#
if (!File.Exists(path))
   {
   ... report problem to user.
   return;
   }

Then just call your method:
C#
txtHashValue.Text = MD5HashFile(path);
Simple enough, yes?
 
Share this answer
 
v2
Comments
BladeLogan 7-May-16 9:53am    
Its so much easier to understand when its visually presented to you like this, I will use this concept to create my own unique way of doing the same thing, thanks Griff!
OriginalGriff 7-May-16 10:11am    
You're welcome!
BladeLogan 7-May-16 10:19am    
If you got time feelf ree to check out my answer, Its still suing your code but it will be alot easier to change it to make it more unique to fit my software in the fuutre now that I understand :-)
Here is what I came to conclusion with Thanks to OrigianalGriff


C#
//---------------I STILL DONT UDNERSTAND WHAT THESE LINES DO--------------------
public string MD5HashFile(string fn)
{
    byte[] hash = MD5.Create().ComputeHash(File.ReadAllBytes(fn));
    return BitConverter.ToString(hash).Replace("-", "");

}

//---------------I STILL DONT UDNERSTAND WHAT THESE LINES DO--------------------^

private void lblTitle_Load(object sender, EventArgs e)
{

}



private void scanButton_Click(object sender, EventArgs e)
{

    //Create a path to the textBox that holds the value of the file that is going to be scanned
    string path = txtFilePath.Text;

    //if there is something in the textbox to scan we need to make sure that its doing it.
    if (!File.Exists(path))
    {
                            // ... report problem to user.
      return;

    }
    else
    {
        MessageBox.Show("Scan Complete");
    }

    //Display the computed MD5 Hash in the path we declared earlier
    hashDisplay.Text = MD5HashFile(path);


}


Thanks again OriginalGriff for explaining!
 
Share this answer
 
Comments
OriginalGriff 7-May-16 10:47am    
No, the comment

// ... report problem to user.

Needs to actually do something. You know what it's like - you click a button, nothing happens. Why? What's up? Did it do something? Is that the code for this file?
Use MessageBox.Show to tell the user that the file doesn't exist.

Your message box comes up before the scan is done! So if there is a problem reading the file you app tells the user "Yes, that worked" and then crashes... bad design!

What use are your comments?
No, seriously: what do they add to the code?
Nothing. In fact, they are wrong and misleading - the first line creates nothing. And it doesn't "hold the value of the file" it holds a path to the file.
The second comment you added - unhelpful.
Same for the third.

Comment when it helps. Not "just because" - if you add comments then they have to be relevant, helpful, and above all correct. Because if they aren't, if they try to explain what the code is already saying it does - then they will not get modified when the code does and will be extremely misleading.
Dump them all!
BladeLogan 7-May-16 11:01am    
About the " // ... report problem to user " comment im going to work on that, and for my comments, they explain what the line under them does, I dont know it made sense to me.

i.e

string path = txtFilePath.Text; is just holding the path to the file that we are going to scan later on, If thats not corrent I've got my whole coding idea messed up.

OriginalGriff 7-May-16 11:26am    
Yes it does hold the path, but there is no point in spelling that out in more words than the code is using! :laugh:
And your comment says:
"Create a path to the textBox that holds the value of the file that is going to be scanned"
It doesn't create a path (and certainly not to a textbox); it doesn't hold a "file value" - it creates a variable which holds a path to a file, "Creating a path" is a more specialised operation, involving Directory.CreateDirectory and an optional security specification!
Comment the method (try typing /// in the line immediately above the "private void scanButton_Click" line and see what happens!) by all means, but comments which explain each line of code are normally redundant (and often wrong).
That's part of why you use sensible names for controls, variables and so on - because the code then becomes to a large extent self-documenting.
Look at the line the first comment is "explaining":

string path = txtFilePath.Text;

It creates a string variable called "path" and assigns the path the user entered to it - but there is no point in writing that as a comment, because the code does the same thing in less words!
Comment why code does things, if it's not obvious. Never comment what code does!
BladeLogan 7-May-16 11:30am    
Good point! Will keep that in mind!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900