0

I have this code:

...
   private void bBrowse_Click(object sender, EventArgs e)
    {

    OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string FileName = ofd.FileName;
                tbKeyFile.Text = FileName;
            }
        if (!String.IsNullOrEmpty(tbKeyFile.Text))
        {

            FileStream fs = new FileStream(tbKeyFile.Text, FileMode.Open);

            MD5 hashFunction = MD5.Create();
            byte[] computedHashCode = hashFunction.ComputeHash(fs);

            string HashInString = Convert.ToBase64String(computedHashCode);

            lHash.Text = HashInString;
         }

    }
...

But it's not calculating a md5 hash. It's calculating a SHA1 checksum. What do I do wrong ?

Lasse
  • 597
  • 2
  • 10
  • 33

2 Answers2

1

As CodesInChaos said: MD5.ComputeHash() returns value in base64. You have to convert it to HEX format by yourself (of course if you want that). Example of how it can be achieved can be found for example here:

string HashInString = BitConverter.ToString(computedHashCode ).Replace("-", "");

Provided example is correct:

M1UOF9zlCe+LwCfDNcWGPw== in Base64 is equal to 33550e17dce509ef8bc027c335c5863f HEX. Use Base64 to Hex Converter to check it!

Community
  • 1
  • 1
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

Input: 213441dasses Output: M1UOF9zlCe+LwCfDNcWGPw== Expected output: 33550e17dce509ef8bc027c335c5863f

Your expected output is a hexadecimal string, while your actual output isn't. You're currently converting the hash into a 64-based string, which is obvisouly not what you want.

See this answer for how to return your expected hex string from the hash.

Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • I don't get what the "str" in your link stands for ? – Lasse Feb 26 '13 at 17:50
  • @Lasse It's the string you're hashing. The important part is what's after "To convert the hash to a hex string..." – ken2k Feb 26 '13 at 17:54
  • I have now done as in your link, but it's still wrong. Expected output 33550e17dce509ef8bc027c335c5863f Output: C81414CA06C3AAA287AB6B2978A1DF38EEF308AF – Lasse Feb 26 '13 at 18:03