Create an XML Digital Signature

Problem

You need to sign part of an XML document according the XML Signatures standard.

Solution

Use the classes in the System.Security.Cryptography.Xml namespace.

Discussion

You can apply the hashing and signing techniques in recipe Encrypt an Object and Verify That Data Hasn’t Changed to encrypt XML content in a file or a byte array, just as you would create a hash for any other type of data. However, this approach introduces several limitations:

  • There’s no easy way to create a hash code and then make the resulting document (XML source and hash code) into a valid XML document.

  • Minor differences in XML documents (such as the addition of white space) will change the hash code, even though they have no effect on how the ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.