473,473 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hash value of file?

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.

Nov 17 '05 #1
7 21363
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" wrote:
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.

Nov 17 '05 #2
Iwan Petrow <xx****@abv.bg> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
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

Nov 17 '05 #4
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

"Richard Rogers" <ri*******@NOSPAMrogers.com> wrote in message
news:o4********************@rogers.com...
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

Nov 17 '05 #5
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

"Wayne" <Me******@community.nospam> wrote in message
news:e9**************@tk2msftngp13.phx.gbl...
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

"Richard Rogers" <ri*******@NOSPAMrogers.com> wrote in message
news:o4********************@rogers.com...
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


Nov 17 '05 #6
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

"Wayne" <Me******@community.nospam> wrote in message
news:e9**************@tk2msftngp13.phx.gbl...
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

"Richard Rogers" <ri*******@NOSPAMrogers.com> wrote in message
news:o4********************@rogers.com...
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


Nov 17 '05 #7
Thanks.

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bill Burwell | last post by:
I am converting a VB6 WebClass application to VB.Net. Used the VB upgrade tool to do the conversion - and it left me a lot of little code things to do. Did those code things and got my app to...
5
by: Michael H | last post by:
Hi all, I guess I don't fully understand how a SHA1 hash value is calculated in C# / .NET for a large file... I'm trying to calculate SHA1 values for large files that are much larger than my...
0
MMcCarthy
by: MMcCarthy | last post by:
This is a module that imports information to a Query or a Table from comma separated values file in a text format! It is very helpful for getting information from other applications and from files! ...
1
by: st12iker | last post by:
I managed to write a file to a hash. However the file contains multiple keys and value in the format listed. Note how the values are sorted in descending order. i.e. of file written to hash ...
4
by: jimhce | last post by:
Hi, I want to generate a hash key from a string, and I want to the hash key is the same value for the same string. But it turns out all the hash functions I have tried, generate different keys...
0
by: equand | last post by:
is there a way to open it? and edit?
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
0
by: cheng99 | last post by:
Hi all, Anyone use the FCIV utility before. In windows xp and 2003, i type in this cmd. fciv -add "C:\test.txt" -xml "C:\aaa.xml" A aaa.xml is produced and with the hash code of the file...
0
by: eli meli | last post by:
Hi. I am looking to query our of the db2 internal system tables the unique identifier of a SQL statement as well as the unique identifier of the execution plan. I need to see those for an active...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.