473,471 Members | 4,650 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple Hash algorithm to detect duplicate content

Hello,

I need a simple hash algorithm that will detect duplicate content in
my application.

I want to hash not just the content, but a few other parameters also
like EmployeeID and DepartmentID.

So something like:

int hash = DoHash(string Message, int EmployeeID, int DepartmentID);
Now the hash has to be unique with unique inputs i.e. can't duplicate
the hash value if the inputs are the same.

Ideas?
Feb 27 '08 #1
9 6411
On Wed, 27 Feb 2008 13:33:26 -0800 (PST), DotNetNewbie
<sn***********@yahoo.comwrote:
>Hello,

I need a simple hash algorithm that will detect duplicate content in
my application.

I want to hash not just the content, but a few other parameters also
like EmployeeID and DepartmentID.

So something like:

int hash = DoHash(string Message, int EmployeeID, int DepartmentID);
Now the hash has to be unique with unique inputs i.e. can't duplicate
the hash value if the inputs are the same.
Probably not possible with a reasonable sized hash. If the size of
the hash is limited, and thare are more than that possible inputs then
there must be some collisions. How many possible values are there for
the Message string for example?

However it is possible to do something with a reasonably low
probability of a collision, something along the lines of:

int DoHash(string Message, int EmployeeID, int DepartmentID) {
const int multiplier = 29;
const int startValue = 37;
int hash = startValue;
hash = multiplier * hash + Message.GetHashCode();
hash = multiplier * hash + EmployeeID;
hash = multiplier * hash + DepartmentID;
return hash;
}

This relies on Messsage.GetHashCode() returning a suitable value.
Depending on your exact requirement you may need to put in your own
function there.

Remember that collisions are possible, though they should be rare. If
you get matching hash values then you must do a full check for
equality.

rossum
>
Ideas?
Feb 27 '08 #2
DotNetNewbie wrote:
I need a simple hash algorithm that will detect duplicate content in
my application.

I want to hash not just the content, but a few other parameters also
like EmployeeID and DepartmentID.

So something like:

int hash = DoHash(string Message, int EmployeeID, int DepartmentID);

Now the hash has to be unique with unique inputs i.e. can't duplicate
the hash value if the inputs are the same.
A hash that has to be unique for all input will need to have the same
size as the input meaning that it is useless.

It is really a trade off between risk of collisions with size and
computational effort.

int DoHash(string Message, int EmployeeID, int DepartmentID)
{
return (Message+EmployeeID+DepartmentID).GetHashCode();
}

only has 2^32 possible values.

string DoHash(string Message, int EmployeeID, int DepartmentID)
{
MD5 md5 = new MD5CryptoServiceProvider();
return
Convert.ToBase64String(md5.ComputeHash(Encoding.UT F8.GetBytes(Message+EmployeeID+DepartmentID)));
}

has 2^128 possible values.

Arne
Feb 27 '08 #3
I am not sure that using a hash computation is the best way to detect
"duplicate content". Are you storing your content in a database? You really
haven't specified much detail. Perhaps you should be looking into a more
robust computation such as CRC32.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
"DotNetNewbie" wrote:
Hello,

I need a simple hash algorithm that will detect duplicate content in
my application.

I want to hash not just the content, but a few other parameters also
like EmployeeID and DepartmentID.

So something like:

int hash = DoHash(string Message, int EmployeeID, int DepartmentID);
Now the hash has to be unique with unique inputs i.e. can't duplicate
the hash value if the inputs are the same.

Ideas?
Feb 28 '08 #4
On Feb 27, 10:16 pm, Peter Bromberg [C# MVP]
<pbromb...@yahoo.NoSpamMaam.comwrote:
I am not sure that using a hash computation is the best way to detect
"duplicate content". Are you storing your content in a database? You really
haven't specified much detail. Perhaps you should be looking into a more
robust computation such as CRC32.
-- Peter
Site:http://www.eggheadcafe.com
UnBlog:http://petesbloggerama.blogspot.com
Short Urls & more:http://ittyurl.net

"DotNetNewbie" wrote:
Hello,
I need a simple hash algorithm that will detect duplicate content in
my application.
I want to hash not just the content, but a few other parameters also
like EmployeeID and DepartmentID.
So something like:
int hash = DoHash(string Message, int EmployeeID, int DepartmentID);
Now the hash has to be unique with unique inputs i.e. can't duplicate
the hash value if the inputs are the same.
Ideas?
Peter,

Yes the content is stored in the database, before anyone inserts new
content I need to check if the same user has posted the same content
before, if he has, then don't insert it again.

Same content means: same employee ID, same departmentID and same
Message.

Meaning that the user can insert the same message text, but it has to
be in a different departmentID.
Feb 28 '08 #5
On Feb 27, 6:03 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
DotNetNewbie wrote:
I need a simple hash algorithm that will detect duplicate content in
my application.
I want to hash not just the content, but a few other parameters also
like EmployeeID and DepartmentID.
So something like:
int hash = DoHash(string Message, int EmployeeID, int DepartmentID);
Now the hash has to be unique with unique inputs i.e. can't duplicate
the hash value if the inputs are the same.

A hash that has to be unique for all input will need to have the same
size as the input meaning that it is useless.

It is really a trade off between risk of collisions with size and
computational effort.

int DoHash(string Message, int EmployeeID, int DepartmentID)
{
return (Message+EmployeeID+DepartmentID).GetHashCode();

}

only has 2^32 possible values.

string DoHash(string Message, int EmployeeID, int DepartmentID)
{
MD5 md5 = new MD5CryptoServiceProvider();
return
Convert.ToBase64String(md5.ComputeHash(Encoding.UT F8.GetBytes(Message+EmployeeID+DepartmentID)));

}

has 2^128 possible values.

Arne
Arne, that looks like it is good for me (the string version).
Is that always going to be 32 characters in length?
Feb 28 '08 #6
DotNetNewbie wrote:
>string DoHash(string Message, int EmployeeID, int DepartmentID)
{
MD5 md5 = new MD5CryptoServiceProvider();
return
Convert.ToBase64String(md5.ComputeHash(Encoding.U TF8.GetBytes(Message+EmployeeID+DepartmentID)));

}

has 2^128 possible values.

Arne, that looks like it is good for me (the string version).
Is that always going to be 32 characters in length?
Yes.

Arne
Feb 29 '08 #7
DotNetNewbie wrote:
On Feb 29, 2:38 am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
>Christopher Van Kirk <chris.vank...@fdcjapan.comwrote:
>>I'm not a fan of this approach. The message column could be quite
large, and may affect the performance of such an index. Seems like it
would be better to compute a hash of some kind of the message, store
the hashed value in the database, and index on that along with the
other two key fields.
But that's exactly what an indexed unique constraint would do, but in a
more transparent fashion.

I've only ever had to manually store a hash in a database once, and
that was to effectively hash an unknown-until-execution-time number of
Guids when populating a set of sets.

Databases know how to index text columns. I think it's best to let them
do their job.

My message column is NTEXT(MAX), and it is going to have articles in
it.
I'll look into this approach....
You mean NTEXT *or* NVARCHAR(MAX) ?

Well - neither can be indexed by SQLServer ...

Arne
Mar 3 '08 #8
Jon Skeet [C# MVP] wrote:
Christopher Van Kirk <ch***********@fdcjapan.comwrote:
>I'm not a fan of this approach. The message column could be quite
large, and may affect the performance of such an index. Seems like it
would be better to compute a hash of some kind of the message, store
the hashed value in the database, and index on that along with the
other two key fields.

But that's exactly what an indexed unique constraint would do, but in a
more transparent fashion.

I've only ever had to manually store a hash in a database once, and
that was to effectively hash an unknown-until-execution-time number of
Guids when populating a set of sets.

Databases know how to index text columns. I think it's best to let them
do their job.
SQLServer does not.

To quote from BOL:

#Columns that are of the large object (LOB) data types ntext, text,
#varchar(max), nvarchar(max), varbinary(max), xml, or image cannot be
#specified as key columns for an index.

Arne
Mar 3 '08 #9
On Mon, 3 Mar 2008 07:44:48 -0000, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
>Arne Vajhoj <ar**@vajhoej.dkwrote:
Databases know how to index text columns. I think it's best to let them
do their job.

SQLServer does not.

To quote from BOL:

#Columns that are of the large object (LOB) data types ntext, text,
#varchar(max), nvarchar(max), varbinary(max), xml, or image cannot be
#specified as key columns for an index.

That's a pity - and it makes life a bit awkward.

The OP could use a hash and then fetch all values which have the same
hash, then performing the comparison.
Indeed. If you "scroll up" you'll see that this is exactly what I
suggested.

--
Posted via a free Usenet account from http://www.teranews.com

Mar 7 '08 #10

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

Similar topics

7
by: Benoît Dejean | last post by:
hi. Is the hash() algorithm standard ? Does hash(some_string) will always return the same hash code on every arch ? i need to use a ~checksum function, like md5, but i was also thinking about...
3
by: Murali | last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL hash map. A couple of ways to do this is 1. create a struct with two unsigned ints and use that as key (write my own...
34
by: pembed2003 | last post by:
Hi All, Does C++/STL have hashtable where I can do stuff like: Hashtable h<int>; h.store("one",1); h.store("two",2); and then later retrieve them like:
4
by: Bo Peng | last post by:
Dear list, I am looking for a way to store a large amount of unique sequences that will be accessed by objects. The most important operations are: 1. Direct access to the sequences (from...
1
by: Wayne Deleersnyder | last post by:
Hi All, I was going to write and ask if someone could help me fix the formatting of my output for hash values, but I believe I got it right now. But, because I couldn't find any website or...
2
by: =?Utf-8?B?TW91dGhPZk1hZG5lc3M=?= | last post by:
How can I add an MD5 hash to XMLSerializer.Serialize without corrupting the content of the file; then how to read it back to verify is correct? I'd like to code up something (see below) that...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
6
by: j1mb0jay | last post by:
I am currently working on a dictionary populating program. I currently have a socket connection my local news server and am trawling through all of the articles looking for new words. I am...
4
by: macm | last post by:
Hi Folks I tested <?php echo 'sha256=>' .hash('sha256', 'The quick brown fox jumped over the lazy dog.') .'</br>'; echo 'sha384=>' .hash('sha384', 'The quick brown fox jumped over the lazy...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.