What's the best way to go about hashing an XML document in C#? I'd like to hash an XML document so that I can tell if it was manually changed from when it was generated. I'm not using this for security--it's OK if someone changes the XML, and changes the hash to match.
For example, I'd hash the child nodes of the root and store the hash as an attribute of the root:
<RootNode Hash="abc123">
<!-- Content to hash here -->
</RootNode>
Add a .NET reference to System.Security, and use XmlDsigC14NTransform. Here's an example...
I recently had to implement a hash "checksum" for partial XML documents at work (we use XElement). Rudimentary performance tests showed ~3x runtime speedup on my machine when using a lookup table for creating the hex string hash, compared to without.
Here is my implementation:
And heres a couple of unit tests for it (using the NUnit framework):
.NET has classes that implement the XML digital signature spec. The signature can be added inside the original XML document (i.e. an "enveloped signature"), or stored/transferred separately.
It may be a bit overkill since you don't need the security, but it has the advantage of being already implemented, and being a standard which does not depend on a language or platform.
You can use the cryptography name space:
You just need to use a key to create the hashing cryptographer and then create a hash with the string reqpresentation of your xml.