Hash value of file?

  • Thread starter Thread starter Iwan Petrow
  • Start date Start date
I

Iwan Petrow

Hi,

I will save name and path of files in a database and I want to check
(before insert a new file) if a given file is in the database. I think
I could use hash codes. The class FileInfo has GetHashCode() method. Is
this method good enough for my task? Are there any other ways of doing
this task?

Thanks.
 
Hi,

Actually I think that using the hashcode you are adding an extra step.

You have your filename, you can easily search on your database for that
string, if you get the hash then you also has to get the hash from the
database, so you have several extra steps. You must have the hash key on the
database as well in order to do it in the correct way, if this is the case
numbers are much fasters than strings to search so it will be the correct
scenario.

Note: There is a slight chance that two files have the same hash code... is
not guaranteed to be unique.

hope this helps
Salva
 
Iwan Petrow said:
I will save name and path of files in a database and I want to check
(before insert a new file) if a given file is in the database. I think
I could use hash codes. The class FileInfo has GetHashCode() method. Is
this method good enough for my task? Are there any other ways of doing
this task?

No - GetHashCode has nothing to do with the contents of the file, it's
for the purposes of putting objects into hashtables.

Have a look at the MD5 class, and in particular its ComputeHash method.
 
Hi Iwan,

I did exactly this kind of thing in a project I wrote in Delphi.
Unfortunately, the fully qualified file name isn't enough to ensure that the
file hasn't been uploaded to the database.
The reason is that the user can use Windows explorer to File -> Copy ->
Paste and you have a brand new file name with the exact same contents.

Instead of using the fully qualified file name to identify whether the file
had been uploaded, I used an MD5 hash code.
The Delphi implementation can be downloaded here:
http://www.fichtner.net/delphi/md5.delphi.phtml
In my Delphi code, it was as simple as making a call like this:

MD5Value := MD5Print(MD5File(Filename)) ;

That gets the Message Digest hash code, and converts it to a string
representation, which you can then store as char/varchar in your database.

I have no ideas whether there is a C#/.net implementation, you might take a
look on sourceforge or open source related web sites.

Good luck,

Richard
 
I did this exact thing in Delphi as well, later converted it to C#. I'd
share but the power went out at home and I can't get to the code. I got the
code example from www.codeproject.com I believe. You may want to check there
and see if you can find something. If not I'll try to remember to post the
code tonight when I get home.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
Here is the code that I've been using to compute hashes on files. I've had
rather good success with the ComputeSHA1Hash. Hope it helps.

public static string ComputeMD5Hash(string fileName)
{
return ComputeHash(fileName, new MD5CryptoServiceProvider());
}

public static string ComputeSHA1Hash(string fileName)
{
return ComputeHash(fileName, new SHA1CryptoServiceProvider());
}

public static string ComputeHash(string fileName, HashAlgorithm
hashAlgorithm)
{
FileStream stmcheck = File.OpenRead(fileName);
try
{
byte[] hash = hashAlgorithm.ComputeHash(stmcheck);
string computed = BitConverter.ToString(hash).Replace("-", "");
return computed;
}
finally
{
stmcheck.Close();
}
}


--
Thanks
Wayne Sepega
Jacksonville, Fl


Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
Back
Top