Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BinaryFormatter ignore assembly version

I have the following method to generate a hash of an object. It works pretty good! But when I change the version of the assembly, the hash is changing even when the object is the same.

public static string GetHash(Object item)
{
    MemoryStream memoryStream = new MemoryStream();
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    binaryFormatter.Serialize(memoryStream, item);
    binaryFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;

    HashAlgorithm hashAlgorithm = new MD5CryptoServiceProvider();
    memoryStream.Seek(0, SeekOrigin.Begin);

    return Convert.ToBase64String(hashAlgorithm.ComputeHash(memoryStream));
}

How is it possible to ignore the assembly version?

like image 886
David Avatar asked Oct 10 '13 07:10

David


2 Answers

But when I change the version of the assembly, the hash is changing even when the object is the same.

yes, that is expected behaviour when using BinaryFormatter... it does not guarantee to create the same output - and especially since it includes full type information (including version) it is pretty much guaranteed to change between versions.

I would consider using a serializer that doesn't include type information; XmlSerializer, Json.NET or protobuf-net would leap to mind.

like image 187
Marc Gravell Avatar answered Nov 04 '22 06:11

Marc Gravell


BinaryFormatter.AssemblyFormat is documented as:

Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.

There's no indication that it has an impact on the serializing path.

Personally I would avoid this method of hashing - it seems terribly fragile to me. Do you have no control over the object being hashed, or any way of hashing in a more stable way?

like image 20
Jon Skeet Avatar answered Nov 04 '22 08:11

Jon Skeet