I need to a way to consistently generate a unique ID given an object or a subset of properties from the object across .NET frameworks / environments / executions with minimal collisions.
- Using the same properties over and over again should always generate the same result
- Collisions should be minimal
- The properties are a mix of strings, ints, and decimals
- The values of the properties are not sensitive data
- Properties won't be added or removed
- The application that uses this is only internal (not open to the public)
- I don't want to rely on GetHashCode which could vary across .NET frameworks / environments / executions as described in this great article at andrewlock.net
- He suggests a possible method but doesn't provide much information on real world use
For example, given a fixed object like
public class ExampleObject
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public int Prop3 { get; set; }
public decimal Prop4 { get; set; }
}
I was thinking about converting all the values to a string, concatenating them, and then using some deterministic algorithm to get the desired result
// Convert all values to strings and concat
string str1 = "a";
string str2 = "b";
int int1 = 1;
decimal dec1 = 1.2m;
string concatStr = $"{str1}_{str2}_{int1.ToString()}_{dec1.ToString()}";
// Use the string to generate the desired value
I have seen people generate a MD5 hash from the string or but I am not sure about the real world usage for millions of possible combinations of the strings / ints / decimals.
The decimal to string also worries me. The value shouldn't change, but could the .ToString()
handle the precision differently in different environments?