locked
Hash Calculation of a file RRS feed

  • Question

  • This is my full code I want to check SHA1 hash of a file using dilogbox i am getting this hash after calculation but the problem is that the other Hash calculators are showing a different hash. Can anyone tell me what is the problem in my code.

    Hash Generated by my application is  :  3mPOLXx5XQATxBIbtiNSrXetOzM= 

    Hash Generated by other applications (Checked with 2 different applications) is : de63ce2d7c795d0013c4121bb62352ad77ad3b33

    I have searched it on Google and msdn too but nothing is working.

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

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

            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.InitialDirectory = "c:\\";
                dlg.Filter = "Iso files (*.iso)|*.iso|All Files (*.*)|*.*";
                dlg.RestoreDirectory = true;

                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string FileName = dlg.FileName;
                    textBox1.Text = FileName;
                }
            }

            private void button2_Click(object sender, EventArgs e)
            {
                if (!String.IsNullOrEmpty(textBox1.Text))
                {

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

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

                    string HashInString = Convert.ToBase64String(computedHashCode);

                    textBox2.Text = HashInString;
                    fs.Close();
                }
            }
        }
    }

    Monday, June 25, 2012 10:23 AM

Answers

  • The thing you are doing wrong; Converting it to a Base64 string.

    using (FileStream fs = new FileStream(textBox1.Text, FileMode.Open))
        using (SHA1 hasher = SHA1.Create())
            foreach (byte b in hasher.ComputeHash(fs))
                textBox2.Text += b.ToString("x2");

    That should do the thing for you.


    Regards, MusicDemon

    • Marked as answer by Mirza.Sahaib Monday, June 25, 2012 2:35 PM
    Monday, June 25, 2012 11:04 AM
  • thanks for the help its working now 

    The thing you are doing wrong; Converting it to a Base64 string.

    using (FileStream fs = new FileStream(textBox1.Text, FileMode.Open))
        using (SHA1 hasher = SHA1.Create())
            foreach (byte b in hasher.ComputeHash(fs))
                textBox2.Text += b.ToString("x2");

    That should do the thing for you.


    Regards, MusicDemon


    • Marked as answer by Mirza.Sahaib Sunday, November 4, 2012 1:47 PM
    Monday, June 25, 2012 2:36 PM

All replies

  • The thing you are doing wrong; Converting it to a Base64 string.

    using (FileStream fs = new FileStream(textBox1.Text, FileMode.Open))
        using (SHA1 hasher = SHA1.Create())
            foreach (byte b in hasher.ComputeHash(fs))
                textBox2.Text += b.ToString("x2");

    That should do the thing for you.


    Regards, MusicDemon

    • Marked as answer by Mirza.Sahaib Monday, June 25, 2012 2:35 PM
    Monday, June 25, 2012 11:04 AM
  • thanks for the help its working now 

    The thing you are doing wrong; Converting it to a Base64 string.

    using (FileStream fs = new FileStream(textBox1.Text, FileMode.Open))
        using (SHA1 hasher = SHA1.Create())
            foreach (byte b in hasher.ComputeHash(fs))
                textBox2.Text += b.ToString("x2");

    That should do the thing for you.


    Regards, MusicDemon


    • Marked as answer by Mirza.Sahaib Sunday, November 4, 2012 1:47 PM
    Monday, June 25, 2012 2:36 PM