Serialize object in xml and hash result

I am trying to serialize an object in xml and then the hash result, but whenever I create a hash, it is always the same for different objects, and not for confidence in what I am doing wrong or missed. Help will be appreciated.

Here is the code I'm using:

private static byte[] CreateHash<T>(T value) { using (MemoryStream stream = new MemoryStream()) using (SHA512Managed hash = new SHA512Managed()) { XmlSerializer serialize = new XmlSerializer(typeof(T)); serialize.Serialize(stream, value); return hash.ComputeHash(stream); } } 
+4
source share
2 answers

Rewind Stream:

 serialize.Serialize(stream, value); stream.Position = 0; return hash.ComputeHash(stream); 

After Serialize stream is at the end, and the data is not readable.

+8
source

Since a hash will never be the same hash, the original idea would be to set the stream position back to the first byte after writing it:

 stream.Position = 0; 
+1
source

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


All Articles