How to use the Reference in an XML signature?

1

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

  • What exactly is it for?
  • How should I use the Reference? What to assign to it?
  • Do I need to use this in the signature of an XML Pq?
  • And how to use?
  • What to do here?

    Dim reference As New Reference()
    reference.Uri = ???
    
        
    asked by anonymous 02.04.2015 / 14:24

    1 answer

    1
  • The Reference represents the element of a digital signature of the XML defined by the specification of the digital signature (www.w3.org/TR/xmldsig-core /).
  • Some available properties font: MSDN:
    DigestMethod: Gets or sets the Digest URI Uniform Resource Identifier method.
    DigestValue: > Gets or sets the digest value of the current Reference . Id: . TransformChain Gets the transformation string of the current Reference . Type: Gets or sets the object type which is being signed. Uri: Gets or sets the Uri of the current Reference .

  • Yes together other classes, for example SignedXml . Because unsigned NFe is simply not accepted by SEFAZ.

  • 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
    
  • 02.04.2015 / 18:02