开发者

How to create a hash code in C# on object graph supplied by a WCF service

开发者 https://www.devze.com 2023-01-18 20:52 出处:网络
I currently have a WCF service which provides an object graph of data on request. I want to have a mechanism where the client can compute a hash on the cached object grap开发者_如何转开发h it posses a

I currently have a WCF service which provides an object graph of data on request. I want to have a mechanism where the client can compute a hash on the cached object grap开发者_如何转开发h it posses and can then supply this hash value to the WCF service to see if it matches the data the service possesses.

I tried this using a standard cryptographic algorithm for computing a hash on the objects but as the object definitions are held by the service, when this is transmitted to the client extra properties can be added and the order of the properties may change, both of which will affect the hash produced.

Is there any mechanism other than say overriding GetHashCode on each object on the WCF service definition and then re-implementing the same kind of hash generation as a utility on the client?


You could simply serialize it to a known layout and take a known hash (md5?) of the serialised form. This requires a predictable serialization though - so XML attributes would be a pain as would whitespace. But not insurmountable.

Alternatively, a reflection / md5 based approach?


Managed to sort this out now. Rather than using an XMLSerialiser on the client and server to create a memory stream that I could then compute a hash on I changed it to use the DataContractSerializer which the WCF service is using to serialise the object graph to the client. This then means that the object graphs are of the same structure and layout. Computing the hash on both serialised forms now matches.

Just in case anyones interested this is how its working, I'm calling this then comparing the byte[] returned with that supplied by the client:

        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);
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号