Pular para o conteúdo principal

XML signatures

PKI SDK supports the XmlDSig and XAdES signature standards. An XML signature can cover a whole document, a specific element inside the document, or external content. As basic examples, the sections below show the signer signing a whole XML document and a specific element of an XML document.

Signing an XML document​

The code below shows a basic XAdES signature of a whole XML document.

// ICP-Brasil AD-RB XAdES signature policy
var policy = BrazilXmlPolicySpec.GetAdrBasicaV22(true);

// Instantiate the signer object, which creates the signature
var signer = new FullXmlSigner();

signer.SetSigningCertificate(cert); // the signer's certificate, with its private key
signer.SetXml(xmlDocument); // bytes or XmlDocument object of the XML document to sign
signer.SetPolicy(policy); // AD-RB signature policy
signer.ComputeSignature(); // creates the signature element
var signedXml = signer.GetSignedXml(); // returns the bytes of the signed XML document

In this signature mode, by default, the transformation of the signed element is enveloped, and the resulting signature element is included as a child of the root element in the finished document. For more information on where the signature element goes and on the XML signature modes, see Insertion modes.

Signing an element of an XML document​

The code below shows a basic XAdES signature of a specific element of an XML document.

note

To sign an element of an XML document, the element must have an ID attribute that is unique in the XML.

// ICP-Brasil AD-RB XAdES signature policy
var policy = BrazilXmlPolicySpec.GetAdrBasicaV22(true);

// Instantiate the signer object, which creates the signature
var signer = new XmlElementSigner();

signer.SetSigningCertificate(cert); // the signer's certificate, with its private key
signer.SetXml(xmlDocument); // bytes or XmlDocument object of the XML document that has the element to sign
signer.SetToSignElementId(id); // sets the ID of the element to sign
signer.SetPolicy(policy);
signer.ComputeSignature(); // creates the signature element
var signedXml = signer.GetSignedXml(); // returns the bytes of the XML document with the signature element

In this signature mode, by default, a detached signature is created, and the signature element is included as a sibling of the signed element. For more information on where the signature element goes and on the XML signature modes, see Insertion modes.

Signing a Brazilian electronic invoice (NFe)​

The code below signs a Brazilian electronic invoice (Nota Fiscal eletrônica, NFe) in the national standard:

// signature policy for NFe
var policy = BrazilXmlPolicySpec.GetNFePadraoNacional();

// Instantiate the signer object, which creates the signature
var signer = new XmlElementSigner();

signer.SetSigningCertificate(cert); // the signer's certificate, with its private key
signer.SetXml(xmlNFe); // bytes or XmlDocument object of the NFe to sign
signer.SetToSignElementId("NFe0000001"); // sets the ID of the NFe to sign
signer.SetPolicy(policy);
signer.ComputeSignature(); // creates the signature element
var signedNFe = signer.GetSignedXml(); // returns the bytes of the signed NFe

See also​

  • @Lacuna.Pki.Xml.XmlElementSigner
  • @Lacuna.Pki.Xml.FullXmlSigner
View as Markdown