How to create a hash code in C # on an object graph provided by WCF

I currently have a WCF service that provides an object graph on demand. I want to have a mechanism in which a client can compute a hash on a graph of cached objects that it has, and can then pass that value to the hash function to the WCF service to make sure that it matches the data that the service has.

I tried this using a standard cryptographic algorithm to calculate the hash on objects, but since the definitions of objects are stored by the service when it is passed to the client, additional properties can be added and the order of the properties can change, both of which will affect the generated hash.

Is there any mechanism other than overriding GetHashCode for each object in the WCF service definition and then repeating the implementation of the same hash generation as a utility on the client?

+3
source share
2 answers

Now I managed to figure it out. Instead of using the XMLSerialiser on the client and server to create a memory stream that I could calculate the hash, I changed it to use the DataContractSerializer, which the WCF service uses to serialize the object graph to the client. This means that graphic objects have the same structure and layout. The hash is now calculated on both serialized forms.

- , , , [], , :

        private static byte[] CalculateHashCode(SomeComplexTypeDefinedAsDataContract objectGraph)
    {
        using (RIPEMD160 crypto = new RIPEMD160Managed())
        {
            using (MemoryStream memStream = new MemoryStream())
            {
                DataContractSerializer x = new DataContractSerializer(typeof(SomeComplexTypeDefinedAsDataContract ));
                x.WriteObject(memStream, objectGraph);
                memStream.Position = 0;
                return crypto.ComputeHash(memStream);
            }
        }
    }
+1

(md5?) . - XML , whitespace. .

, /md5?

+2

Source: https://habr.com/ru/post/1767885/


All Articles