0

I'm setting ID of database entry's by creating and using a hash value. The problem is that when I start the application again, the hash value of the same origin values are different and I get a doubling (same values, different ID). Below you find my example code. Start the CLI, remember the given hash value, start again --> different value.

How can I reproduce the same hash value with each instance?

static void Main(string[] args)
{
    int drid = 3081;
    DateTime dt = DateTime.ParseExact("2019-04-11 00:23:10", "yyyy-MM-dd HH:mm:ss", null);
    string idAsString = drid.ToString() + dt.ToString();
    Console.WriteLine(idAsString.GetHashCode().ToString());
    Console.ReadKey();
}
4

1 Answer 1

0

Do not do this. Never ever ever. Hash codes are far not unique and semantically different objects at some point will result to a collision - i.e. both will produce exactly the same hashcode, which, in turn, will break down your database.

Next thing to notice is that hashing objects is complicated. It is good to keep hashcode the same as long as your object lives, which implies that you compute it once - when the object is created - or compute it on the fly ignoring mutable fields (since generally each mutation will change the hashcode).

Even more than that: it isn't easy to come up with good enough hashing algorithm: the one which is unpredictable enought and yet collision-rare.

Sign up to request clarification or add additional context in comments.

7 Comments

While this is correct, using something like SHA2-256 or SHA2-512 (.NET does't have SHA3 yet) would make collisions far less likely
@CamiloTerevinto, it does not change the thing. Id is unique, hashes are not.
"Even more than that: it isn't easy to come up with good enough hashing algorithm: the one which is unpredictable enought and yet collision-rare." It's not that hard. Avoiding collisions mostly comes down to just increasing the size of the hash, as long as the hashing algorithm isn't truly bad. Hashing to an int is never going to have a low collision rate for any non-trivial collection size, but it doesn't need to be that big for the odds of a collision to be low enough that the sun is more likely to explode before a collision happens, which tends to be good enough for most purposes.
@Servy, we are not discussing hashing algorithm itself, rathether the way you implement GetHashCode() on a particular (complicated?) object.
@SerejaBogolubov No, you do not in fact need to inevitable call GetHashCode to compute a hash of a string. You can absolutely write code to compute a hash without using it. That you think it's impossible to write any hashing algorithm besides string.GetHashCode makes no sense. Why do you think that's the only possible way to hash anything?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.