Generating an XML document hash in C# Generating an XML document hash in C# xml xml

Generating an XML document hash in C#


.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:

System.Security.Cryptography.MACTripleDES hash = new System.Security.Cryptography.MACTripleDES(Encoding.Default.GetBytes("mykey"));string hashString = Convert.ToBase64String(hash.ComputeHash(Encoding.Default.GetBytes(myXMLString)));

You just need to use a key to create the hashing cryptographer and then create a hash with the string reqpresentation of your xml.


Add a .NET reference to System.Security, and use XmlDsigC14NTransform. Here's an example...

/* http://www.w3.org/TR/xml-c14n    Of course is cannot detect these are the same...       <color>black</color>    vs.   <color>rgb(0,0,0)</color>    ...because that's dependent on app logic's interpretation of XML data.    But otherwise it gets the following right...    •Normalization of whitespace in start and end tags    •Lexicographic ordering of namespace and attribute    •Empty element conversion to start-end tag pair     •Retain all whitespace between tags    And more. */public static string XmlHash(XmlDocument myDoc){    var t = new System.Security.Cryptography.Xml.XmlDsigC14NTransform();    t.LoadInput(myDoc);    var s = (Stream)t.GetOutput(typeof(Stream));    var sha1 = SHA1.Create();    var hash = sha1.ComputeHash(s);    var base64String = Convert.ToBase64String(hash);    s.Close();    return base64String;}