Question How to detect a file with a certain SHA1 string hash

What?

Member
Joined
Aug 11, 2012
Messages
5
Programming Experience
1-3
Hello there.

So currently, i've experienced some problems, i've tryed to detect a file by listin the SHA1 string of the file but no luck at all.

I know how to gain files and list them and so on, but i don't know how i should detect files that have certains SHA1 strings, by the way i store all of the SHA1 strings in a simple text file.

I havent been able to come up with a code as that's why i'm posting this and hopefully someone would guide me.


I highly appricate everyone that tryes to help and thanks in advance.


-What?
 
Last edited:
The hash for a file is the output produced by inputting the contents of the file into a hash algorithm. That's what you have to do. You need to get the binary contents of the file and feed it to a SHA1 hash algorithm and then compare the output to your list. There are lots of examples of hashing a Byte array already on the web, using the SHA1Managed, SHA1CryptoServiceProvider or SHA1Cng classes. You can get a Byte array from a file using the File.ReadAllBytes method. If the file is large then you can read it in blocks by opening a FileStream and calling Read in a loop, passing each block to the hash algorithm. Once you have the output as a Byte array, you will probably need to convert that to a String. The most common way to do that is to call Convert.ToBase64String, but it depends how the Strings you already have were created in the first place.
 
I gave it a shot but still it doesnt seem to work.

Here is the code by the way.

VB.NET:
Private Sub FileSystemTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileSystemTimer.Tick

           Try

            Files.Text = e.FullPath
            ListBox1.Items.Add(Files.Text)
            Me.OpenFileDialog1.FileName = ""
            Dim scan As New TextBox
            scan.Text = My.Computer.FileSystem.ReadAllText("sha1.txt").ToString
            Dim m As New SHA1CryptoServiceProvider
            Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            m.ComputeHash(f)
            Dim hash As Byte() = m.Hash
            Dim buff As New StringBuilder
            Dim hashByte As Byte
            For Each hashByte In hash
                buff.Append(String.Format("{0:X2}", hashByte))
            Next
            f.Close()
            If scanbox.Text.Contains(buff.ToString) Then
                Me.OpenFileDialog1.FileName = e.FullPath
                ListBox2.Items.Add(OpenFileDialog1.FileName)
                MsgBox("It works")

            End Sub
 
Last edited:
If things don't happen as you expect then it's generally a good idea to tell us what actually does happen, so that we know what we're looking for. Many bugs are not easy to spot simply by reading code. You're the one who ran it so you're the one who knows what the symptoms are.

That said, I can see one issue straight away:
Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
Why are you opening the file twice?
 
If things don't happen as you expect then it's generally a good idea to tell us what actually does happen, so that we know what we're looking for. Many bugs are not easy to spot simply by reading code. You're the one who ran it so you're the one who knows what the symptoms are.

That said, I can see one issue straight away:
Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
Why are you opening the file twice?

I didn't notice that i wrote it multiple times honestly, i didnt bother changing it either since everything worked fine when i tested it with md5 strings. (Yeah i know, i'm quite lazy sometimes)

Anyway, the message box at the end is supposed to pop up when the software has detected the file with the sha1 string listed in the text file, but the thing is, it doesnt and i have no idea why.

I appricate you're help by the way, thank you.
 
Then you need to debug your code. With that in mind, temporarily move the code out of the Tick event of a Timer and place it in the Click event of a Button. You can then place a breakpoint (F9) on the first line of code and, when execution breaks there, you can step through the code line by line (F10) and into and out of methods (F11, Shift+F11) so you can see exactly what happens at each step. You would first decide exactly what you expect to happen and, after stepping, you would check what actually did happen. If the two don't coincide then you have found where an issue occurs. You then need to determine why. At each step, you have lots of tools available to you to determine the state of the application. You have the Autos, Locals and Watch windows, the Quick Watch window and the Immediate window, amongst others. These allow you to check the values of variables and properties and evaluate arbitrarily complex expressions.
 
I will try this tomorrow as it's getting pretty late here and i'm very tired.

Feel free to try it yourself and see what the outcome would be.

I also edited the previous post with a line of code that i missed.


Again, thank you for you're help and kindess @jmcilhinney
 
Back
Top