So I have an object that will ether be stored in a database table or found in a file; used for updating that table. We need to do comparisons between the table and the update file to avoid duplicates when we update.
My first attempt to solve the problem is to do a string.join
on the fields then convert that to bytes, and finally md5 hash that byte array. The problem is that we get sometimes we would get an empty string when some (but not all) of the fields were null.
So the second way we decided to do it was to just serialize the object into bytes and md5 hash the string of that. So far that's worked fine but it was brought to my attention that it could be unstable (if someone updates .net versions for example).
Is this something i need to worry about?
Example code for those who want it:
public void GenerateHash()
{
md5 = returnHash();
}
public byte[] returnHash()
{
if (this == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, this);
string str = System.Text.Encoding.Default.GetString(ms.ToArray());
return SensitiveNamespace.Hashing.MD5(str).ToBytes();
}