Calculating the hash value of an XML in C# ASP.NET

static byte[] HashXml ( XmlElement xe, HashAlgorithm hash ) 
    XmlDocument xd = new XmlDocument(); 
    xd.LoadXml (xe.OuterXml); 
    return HashXml(xd, hash); 
static byte[] HashXml ( XmlDocument xd, HashAlgorithm hash ) 
   //Get the assembly
    Assembly a = Assembly.Load("System.Security, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); 
    //Get the hashing class 
    Type t = a.GetType("System.Security.Cryptography.Xml.CanonicalXml"); 
    ConstructorInfo c = t.GetConstructor(
        BindingFlags.Instance | BindingFlags.NonPublic, 
        new Type[] { typeof(XmlDocument), typeof(XmlResolver) }, 
    //construct the object
    object cx = c.Invoke(new object[] { xd, null }); 
    //calculate the Hash 
    byte[] result = (byte[])t.InvokeMember(
        BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, 
        new object[] { hash }); 
    return result; 

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.