How to use Reference in an XML signature?

Asked

Viewed 574 times

1

Doubts about System.Security.Cryptography.Xml.Reference:

  1. What exactly is it for?
  2. How should I use Reference? What to assign to it?
  3. I need to use this in signing an XML Pq?
  4. And how to use?

What to do here?

Dim reference As New Reference()
reference.Uri = ???

1 answer

1


  1. Reference represents the element of a digital XML signature defined by the digital signature specification (www.W3.org/TR/xmldsig-core/).
  2. Some properties available font: MSDN:
    Digestmethod: Obtain or define the method Digest URI Uniform Resource Identifier of Reference.
    Digestvalue: Get or set the value of Digest of the current Reference.
    Id: Obtains or defines the identification of the current Reference.
    Transformchain Get the current transformation chain Reference.
    Type: Get or define the type of object being signed.
    Uri: Obtains or defines the Uri of the current Reference.

  3. Yes together other class, for example SignedXml. Because unsigned Nfe is simply not accepted by SEFAZ.

  4. Example:

    ' Create a new XML document.
    Dim doc As New XmlDocument()
    
    ' Format the document to ignore white spaces.
    doc.PreserveWhitespace = False
    
    ' Load the passed XML file using it’s name.
    doc.LoadXml(xmlString)
    
    Dim tagAss = "infNFe"
    Dim ref As New Reference()
    Dim _Uri As XmlAttributeCollection = doc.GetElementsByTagName(tagAss).Item(0).Attributes
    For Each atributo As XmlAttribute In _Uri
      If atributo.Name = "Id" Then
          ref.Uri = "#" & atributo.InnerText
      End If
    Next
    
    ' Add an enveloped transformation to the reference.
    Dim Envelope As New XmlDsigEnvelopedSignatureTransform()
    Referencia.AddTransform(Envelope)
    
    Dim c14 As New XmlDsigC14NTransform()
    ref.AddTransform(c14)
    
    ' Add the reference to the SignedXml object.
    signedXml.AddReference(ref)
    
    ' Create a new KeyInfo object
    Dim keyInfo As New KeyInfo()
    
    ' Load the certificate into a KeyInfoX509Data object
    ' and add it to the KeyInfo object.
    keyInfo.AddClause(New KeyInfoX509Data(_X509Cert))
    
    ' Add the KeyInfo object to the SignedXml object.
    signedXml.KeyInfo = keyInfo
    signedXml.ComputeSignature()
    
    ' Get the XML representation of the signature and save
    ' it to an XmlElement object.
    Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
    
    ' Gravar assinatura no documento XML
    doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature,True))
    XMLDoc = New XmlDocument()
    XMLDoc.PreserveWhitespace = False
    XMLDoc = doc 
    dim xmlAssinado = XMLDoc.OuterXml
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.