473,763 Members | 5,396 Online
Bytes | Software Development & Data Engineering Community
+ 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 6434
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.GetHash Code();
hash = multiplier * hash + EmployeeID;
hash = multiplier * hash + DepartmentID;
return hash;
}

This relies on Messsage.GetHas hCode() 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+Employ eeID+Department ID).GetHashCode ();
}

only has 2^32 possible values.

string DoHash(string Message, int EmployeeID, int DepartmentID)
{
MD5 md5 = new MD5CryptoServic eProvider();
return
Convert.ToBase6 4String(md5.Com puteHash(Encodi ng.UTF8.GetByte s(Message+Emplo yeeID+Departmen tID)));
}

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
"DotNetNewb ie" 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...@yaho o.NoSpamMaam.co mwrote:
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

"DotNetNewb ie" 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.d kwrote:
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+Employ eeID+Department ID).GetHashCode ();

}

only has 2^32 possible values.

string DoHash(string Message, int EmployeeID, int DepartmentID)
{
MD5 md5 = new MD5CryptoServic eProvider();
return
Convert.ToBase6 4String(md5.Com puteHash(Encodi ng.UTF8.GetByte s(Message+Emplo yeeID+Departmen tID)));

}

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 MD5CryptoServic eProvider();
return
Convert.ToBase 64String(md5.Co mputeHash(Encod ing.UTF8.GetByt es(Message+Empl oyeeID+Departme ntID)));

}

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.co mwrote:
>Christopher Van Kirk <chris.vank...@ fdcjapan.comwro te:
>>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.comwro te:
>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.co mwrote:
>Arne Vajhoj <ar**@vajhoej.d kwrote:
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
2749
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 hash() which is obviously simpler. So i can safely rely on hash() behaviour so i can use it to generate ~strong and portable identifier/checksum ? thank you
3
4175
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 HashFcn and EqualKey template args) or, 2. convert the two unsigned ints to char*s, concatenate them and use that as Key. For method 1, the difficulty I am having is in writing the HashFcn. HashFcn requires the following method
34
14479
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
3161
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 pointers stored in each object). Access through key lookup is not acceptable. 2. Given a new sequence, determine if it is already in the factory of sequences. If so, increase the reference count of the existing sequence
1
3597
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 tutorial to help me with this issue I figured I'd make a post just in case someone else runs into the same issue. ....
2
2669
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 looks like this, but I'm not sure this is correct approach to the problem. Once I add the signature, the file won't test the same way again. I know I could add it to the bottom of the file, but then everyone would have to know my algorithm for...
139
14216
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
10044
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 currently using Java to do this but would like to move the source to C#. Java's String class has a method that hashes strings. I was wondering if C# has a method which does the same? In my Java version of the program I am using the Multiply Add and...
4
3463
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 dog.') .'</br>'; echo 'sha512=>' .hash('sha512', 'The quick brown fox jumped over the
0
9564
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
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 we have to send another system
2
3528
muto222
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.