473,695 Members | 2,969 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing Hash Codes


I know this is the wrong way to do it, but maybe someone can tell me the
right way to do it...

I have two different databases that I need to synchronize. The database
doesn't have keys exactly, but it does provide a rowid function.

So, I am storing a dictionary of <string, long>, string being the rowid
and long being a sum of the hash code for each column in the row.

With this dictionary, it is very easy to determine if a row is new, has
changed, or is already in sync with the target system:

* If I have a rowid that doesn't exist in the dictionary, then I know
it is a new row and I can call my insert code.

* If I have a rowid that exists in the dictionary, and the sum of hash
codes is the same as in my dictionary, then I know that the row has not
changed and there is no action to take.

* If I have a rowid that exists in the dictionary, and the sum of hash
codes is not the same as my dictionary, then I know the row of has
changed, and I need to call my update code.

-----

So, my question is, with this situation, how can I get the same results,
but not store GetHashCode(). I need some kind of "stable" hash
calculation. And it needs to take all kinds of objects; string, int,
decimal, double, etc.

I started to think I could just use a hash code on string and call
ToString() on each column, but this looks just as dangerous as trusting
GetHashCode() not to change.
Thanks for any tips...
Brian
May 26 '06 #1
3 2016
Brian <no@email.com > wrote:

<snip>
So, my question is, with this situation, how can I get the same results,
but not store GetHashCode(). I need some kind of "stable" hash
calculation. And it needs to take all kinds of objects; string, int,
decimal, double, etc.


Use an MD5 hash (or something similar). You'll need to convert each
type into a stable binary representation, but then you can add each
field to the hash as you go.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 26 '06 #2
Let me just add to Jon's reply. Look at the System.Security .Cryptography
classes.
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Brian" wrote:

I know this is the wrong way to do it, but maybe someone can tell me the
right way to do it...

I have two different databases that I need to synchronize. The database
doesn't have keys exactly, but it does provide a rowid function.

So, I am storing a dictionary of <string, long>, string being the rowid
and long being a sum of the hash code for each column in the row.

With this dictionary, it is very easy to determine if a row is new, has
changed, or is already in sync with the target system:

* If I have a rowid that doesn't exist in the dictionary, then I know
it is a new row and I can call my insert code.

* If I have a rowid that exists in the dictionary, and the sum of hash
codes is the same as in my dictionary, then I know that the row has not
changed and there is no action to take.

* If I have a rowid that exists in the dictionary, and the sum of hash
codes is not the same as my dictionary, then I know the row of has
changed, and I need to call my update code.

-----

So, my question is, with this situation, how can I get the same results,
but not store GetHashCode(). I need some kind of "stable" hash
calculation. And it needs to take all kinds of objects; string, int,
decimal, double, etc.

I started to think I could just use a hash code on string and call
ToString() on each column, but this looks just as dangerous as trusting
GetHashCode() not to change.
Thanks for any tips...
Brian

May 28 '06 #3
And to add to my own reply, here's a sample from an application I wrote a few
years ago:

public static byte[] HashBytes512(st ring pw, bool UE)
{
byte[] pwBytes;
if (UE)
{
pwBytes = Encoding.Unicod e.GetBytes(pw);
}
else
{
pwBytes = Encoding.ASCII. GetBytes(pw);
}
SHA512Managed sha = new SHA512Managed() ;
byte[] hashBytes = sha.ComputeHash (pwBytes);
return hashBytes;
}

--
Dale Preston
MCAD C#
MCSE, MCDBA
"Dale" wrote:
Let me just add to Jon's reply. Look at the System.Security .Cryptography
classes.
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Brian" wrote:

I know this is the wrong way to do it, but maybe someone can tell me the
right way to do it...

I have two different databases that I need to synchronize. The database
doesn't have keys exactly, but it does provide a rowid function.

So, I am storing a dictionary of <string, long>, string being the rowid
and long being a sum of the hash code for each column in the row.

With this dictionary, it is very easy to determine if a row is new, has
changed, or is already in sync with the target system:

* If I have a rowid that doesn't exist in the dictionary, then I know
it is a new row and I can call my insert code.

* If I have a rowid that exists in the dictionary, and the sum of hash
codes is the same as in my dictionary, then I know that the row has not
changed and there is no action to take.

* If I have a rowid that exists in the dictionary, and the sum of hash
codes is not the same as my dictionary, then I know the row of has
changed, and I need to call my update code.

-----

So, my question is, with this situation, how can I get the same results,
but not store GetHashCode(). I need some kind of "stable" hash
calculation. And it needs to take all kinds of objects; string, int,
decimal, double, etc.

I started to think I could just use a hash code on string and call
ToString() on each column, but this looks just as dangerous as trusting
GetHashCode() not to change.
Thanks for any tips...
Brian

May 28 '06 #4

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

Similar topics

7
2742
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
1
11176
by: Hale | last post by:
Hi, I'm having a problem storing an array inside of a hash. I'm bringing in a hash via a reference and I want to assign a value to it. This is what I'm doing: push (@{$$node->{ELEMENTS}}, "New value"); (I need to use "push" because this is going in a for-loop)
4
3097
by: Thomas Christmann | last post by:
Hi! First let me apologize for asking this question when there are so many answers to it on Google, but most of them are really contradicting, and making what I want to do very performant is crucial to my project. So, here's what I have: My C programm connects to a database and gets ca. 50-100K domain name/file path pairs. Those pairs have to be cached by my application. Building the cache may take a second or two, but retrieving from...
2
5002
by: Eduardo Pérez Ureta | last post by:
I was wondering what the best way is to store a file hash (MD5 or SHA1) and make it primary key indexed. I have seen some people storing the hexadecimal encoded MD5 in a CHAR(32) but it may be a better idea to use a CHAR(16) without encoding the string, but that may cause some problems. What do you recommend? Do you have any experiences storing file hashes in a database? Do you know any good opensource software that stores file hashes...
8
13380
by: mortb | last post by:
Hi, How do I write a GenerateHashcode function that will generate guaranteed unique hashcodes for my classes? cheers, mortb
8
9453
by: rgparkins | last post by:
Hi I am creating a sign-up process on a web site much like that of a wizard form. I have browsed many sites to look for examples of how to store the entry data, so that the user can go back and forward along the sign -up process. I am currently trying to use a Session object to store all these details and get them back out of session as each step is selected. My model was a HashTable of NameValueCollection, so I could go to the hash...
14
2682
by: John Coleman | last post by:
Greetings, I am currently trying to learn Python through the excellent "Learning Python" book. I wrote my first non-trivial program, which began with several comment lines. One of the comment lines began with '#hash'. IDLE doesn't colorize it as a comment line but instead colors the word 'hash' in purple as if it were a key word. Wierd. The behavior seems easy to trigger: Just open up a new window in IDLE and enter these two lines: ...
3
1852
by: nasirmajor | last post by:
dear all; my simple quetion is that if we have hashed the users passwords and stored in databases. then can we use that hash code e.g D52987198EA2730FD22A38E7976344D843A7FFA0 in query string. the reasion for asking is that: are generated hash codes are always unique? so that we can use them for page navigation like in query string.
5
2861
by: per9000 | last post by:
Hi all, I want to create an encryption program and started thinking about not storing sensitive information in the memory since I guess someone might steal my computer an scan my memory. So I wrote this method for getting a password from the console and converting it to an array of bytes for later use in the encryption algorithm. The weak point as I see it is the storage of the password - it will be
0
8642
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
8583
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
9126
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8861
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
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...
1
6500
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
4588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2283
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.