In order to identify an object in our database and make lookup easier, I have decided to hash the values that identify the object and store them in our database.
These values could be strings, guids, decimals, booleans or datetimes. So for example, we could have
object 1: "test 1", 1, Guid.NewGuid() -> combined hash of these values = 1
object 2: "test 2", 2, DateTime.Now -> combined hash of these values = 2
In this case object 1
and object 2
are different because their hashes are different.
In my first attempt, I used the HashCode of the values and combined them in an order independent way (see here). However I quickly came across a number of issues with this:
- I was getting collisions with sequential guids
- Hashes may not be unique across app domains
I was thinking of using SHA256
to generate the hashcodes, but how can I combine them in an order-independent way?
Essentially my requirements for my hashing function H
are
- Commutative:
H(1, "test") = H("test", 1)
- Injective: So the hash of a tuple should not equal the hash of another tuple unless the tuples themselves are equal (i.e. the tuple components are equal)
- Cryptographically secure hashes are not needed
- Making the process reversible is not needed
What could I use for this?