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=2.0.0.0, 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, 
        null, 
        new Type[] { typeof(XmlDocument), typeof(XmlResolver) }, 
        null); 
 
    //construct the object
    object cx = c.Invoke(new object[] { xd, null }); 
 
    //calculate the Hash 
    byte[] result = (byte[])t.InvokeMember(
        "GetDigestedBytes",
        BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, 
        null, 
        cx, 
        new object[] { hash }); 
 
    return result; 
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.