segunda-feira, 27 de junho de 2016

Curso WinDev - Certificado - 007 - Assinatura Digital nfe - Parte 3/... - Assinar Documento



Video youtube


Nessa aula vou Mostrar como Assinar um Documento Xml

This lesson will show how Subscribe to a XML Document

Cette leçon montrera comment Abonnez-vous à un document XML



doc.windev.com/en-US/?1000019302&name=functions_for_managing_the_certificates
http://doc.pcsoft.fr/fr-FR/?1000019302&name=fonctions_gestion_des_certificats

Blog - WinDev - Curso Certificado - 001/... - Seleciona Certificado - Certificate Select 
Blog - WinDev - Curso Certificado - 002/... - Certificate Type - Pegar Dados Certificado
Blog - WinDev - Curso Certificado - 003/... - Ver se Certificado é Válido
Blog - WinDev - Curso Certificado - 004/... - CertificateLoad - Chamar pfx/arquivo
Blog - WinDev - Curso Certificado - 005/... - Assinatura Digital Nfe - Parte 1/...
Blog - WinDev - Curso Certificado - 006/... - Assinatura nfe - Parte 2/... - System.xml.xmlDocument
Blog - WinDev - Curso Certificado - 007/... - Assinatura nfe - Parte 3/... - Assinar Documento
Blog - Windev - Curso Certificado - 008/... HttpListCertificate
Blog - WinDev - Curso Soap - 009/... Consulta Cadastro Sefaz - WebService
Blog - WinDev - Curso Soap - 010 - Status Nfe Sefaz - WebService



PROCEDURE assinar_nfe(_recebe_xml,_refUri,_ChNfe)
//_refUri= (sRefUri IS STRING = "infNFe")
clX509certi is X509Certificate2("D:\certificado_2016\certificado.pfx","senha")

clDoc is System.Xml.XmlDocument;
clDoc.LoadXml(_recebe_xml) // Ler um arquivo Xml // read an xml file // lire un fichir xml //

//certificado com endereco e senha //certificate with address and password //le certificat avec adresse et mot de pass
//ver se Refuri existe  //Refers to see there //fait référence à y voir
qtderefuri is int= clDoc.GetElementsByTagName(_refUri).Count
IF (qtderefuri) =0 THEN
    //nao existe // does not exists // ne pas exister
ELSE
    //cria um objeto assinado // creat a signed object // créer un objet signé
    VrSignedXml is SignedXml(clDoc)
    // adiciona a chave do certificado // add the key certificate // ajouter le certificat de clé
    VrSignedXml.SigningKey = clX509certi.PrivateKey
    // Cria a referencia para assinatura // creates a reference to signature // crée une référence à la signature
    Reference is Reference //= new Reference();
    Reference.Uri = "#" + _ChNfe
    // adiciona um XmlDsigEnvelopedSignatureTransform para a assinatura // add a XmlDsigEnvelopedSignatureTransform for signature
    // ajouter un XmlDsigEnvelopedSignatureTransform à la signature
    Env is XmlDsigEnvelopedSignatureTransform    
    Reference.AddTransform(Env)   
    c14 is XmlDsigC14NTransform
    Reference.AddTransform(c14)
    // adiciona a referencia no xml assinado // add the reference in xlm signed // ajouter la référence en xml singé   
    VrSignedXml.AddReference(Reference)       
    // Cria a chave // creates the key // crée la clé
    VrkeyInfo is KeyInfo
    // carrega o certificado em um keyinfox509 e adiciona ao keyinfo // load the certificate in a keyinfo x509 and add to keyinfo
    // chager le certificat dans un x509 keyinfo et ajouter à kyinfo
    VrkeyInfo.AddClause(new KeyInfoX509Data(clX509certi))
    // adiciona o keyinfo ao xml assinado // add keyinfo the xml signed // ajouter keyinfo le xml signé
    VrSignedXml.KeyInfo = VrkeyInfo
    VrSignedXml.ComputeSignature()   
    // busca a representacao XML da assinatura e salva no XML // search the xml representantion of the signature and saved in xml
    // rechercher la représentation xml de la signature et enregistré en xml
    xmlDigitalSignature is System.Xml.XmlElement dynamic = VrSignedXml.GetXml()       
    // adiciona a assinatura no documento // add the signature in the document // ajouter la signature dans le document
    clDoc.DocumentElement.AppendChild(clDoc.ImportNode(xmlDigitalSignature, True))   
    //salva o documento assinado // save the signed document // enregistrer le document signé
    RESULT clDoc.get_InnerXml()
   
END

RESULT -1




//Buscar arquivo xml a assinar // search xml file to sign // rechercher ujn fichier xml pourr signer
_xml_assinar is string=fLoadText("D:\arquivo_xml\43160603850874000126550010000035551000142204-nfe.xml",foUnicode)
EDT_xml=_xml_assinar
sRefuri is string="infNFe"
chavenfe is string="NFe43160603850874000126550010000035551000142204"
s_retorno is string=assinar_nfe(_xml_assinar,sRefuri,chavenfe)
IF s_retorno=-1 THEN
    EDT_xml_assinado="Erro assinar"
ELSE
    EDT_xml_assinado=s_retorno
       
END




















Codigo do dia 19 07 2021
---------------
exemplo esta
E:\_exemplos_windev_web_mob\windev_amarildo_aula
assinar
e copia do d:\_copia


=========
instanciar 
mscorlib
system
system.security
system.xml
==================== Codigo para chamar assinar
EDT_Xml=Replace(EDT_Xml,CR,"")
//nfe
//EDT_Xml_assinado=assinar_nfe(EDT_Xml,"infNFe","NFe43210717229165000132550010000000061509881447")
EDT_Xml_assinado=assinar_nfe(EDT_Xml,"infEvento","ID2102104321071722916500013255001000000004133116576901")
fSaveText("c:\temp\assinado.xml",EDT_Xml_assinado)
ShellExecute("c:\temp\assinado.xml")
=============================
// INTERNAL PROCEDURE assinar_nfe(_recebe_xml,_refUri,_ChNfe)
  //_refUri= (sRefUri IS STRING = "infNFe")

clX509certi is X509Certificate2("D:\BACKUP_HD\AMARILDO\infinity\certificado_rm2020.pfx","rm2020")

clDoc is System.Xml.XmlDocument;
clDoc.LoadXml(_recebe_xml) // Ler um arquivo Xml // read an xml file // lire un fichir xml //
fSaveText("c:\temp\a.xml",_recebe_xml)
ShellExecute("c:\temp\a.xml")
//certificado com endereco e senha //certificate with address and password //le certificat avec adresse et mot de pass
//ver se Refuri existe  //Refers to see there //fait référence à y voir
qtdeRefUri is int= clDoc.GetElementsByTagName(_refUri).Count
IF (qtdeRefUri) =0 THEN
//nao existe // does not exists // ne pas exister
ELSE
//cria um objeto assinado // creat a signed object // créer un objet signé
VrSignedXml is SignedXml(clDoc)
// adiciona a chave do certificado // add the key certificate // ajouter le certificat de clé
VrSignedXml.SigningKey = clX509certi.PrivateKey
// Cria a referencia para assinatura // creates a reference to signature // crée une référence à la signature
Reference is Reference //= new Reference();
Reference.URI = "#" + _ChNfe
Trace("#" + _ChNfe)
// adiciona um XmlDsigEnvelopedSignatureTransform para a assinatura // add a XmlDsigEnvelopedSignatureTransform for signature
// ajouter un XmlDsigEnvelopedSignatureTransform à la signature
Env is XmlDsigEnvelopedSignatureTransform    
Reference.AddTransform(Env)    
c14 is XmlDsigC14NTransform
Reference.AddTransform(c14)
// adiciona a referencia no xml assinado // add the reference in xlm signed // ajouter la référence en xml singé    
VrSignedXml.AddReference(Reference)        
// Cria a chave // creates the key // crée la clé
VrkeyInfo is KeyInfo
// carrega o certificado em um keyinfox509 e adiciona ao keyinfo // load the certificate in a keyinfo x509 and add to keyinfo
// chager le certificat dans un x509 keyinfo et ajouter à kyinfo
VrkeyInfo.AddClause(new KeyInfoX509Data(clX509certi))
// adiciona o keyinfo ao xml assinado // add keyinfo the xml signed // ajouter keyinfo le xml signé
VrSignedXml.KeyInfo = VrkeyInfo
VrSignedXml.ComputeSignature()    
// busca a representacao XML da assinatura e salva no XML // search the xml representantion of the signature and saved in xml
// rechercher la représentation xml de la signature et enregistré en xml
xmlDigitalSignature is System.Xml.XmlElement dynamic = VrSignedXml.GetXml()        
// adiciona a assinatura no documento // add the signature in the document // ajouter la signature dans le document
clDoc.DocumentElement.AppendChild(clDoc.ImportNode(xmlDigitalSignature, True))    
//salva o documento assinado // save the signed document // enregistrer le document signé
RESULT clDoc.get_InnerXml()

END
END

================ Exemplos
EDT_Xml=[
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeRecepcaoEvento4">
<envEvento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00">
<idLote>0</idLote>
<evento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00">
<infEvento Id="ID2102104321071722916500013255001000000004133116576901">
<cOrgao>43</cOrgao>
<tpAmb>2</tpAmb>
<CNPJ>17229165000132</CNPJ>
<chNFe>43210709402019000139550010000008431131416823</chNFe>
<dhEvento>2021-07-18T10:39:05-03:00</dhEvento>
<tpEvento>210210</tpEvento>
<nSeqEvento>1</nSeqEvento>
<verEvento>1.00</verEvento>
<detEvento versao="1.00">
<descEvento>Ciencia da Operacao</descEvento>
</detEvento>
</infEvento>
</evento>
</envEvento>
</nfeDadosMsg>
</soap12:Body>
</soap12:Envelope>
]
==================================
EDT_Xml=[
<?xml version="1.0" encoding="UTF-8"?><NFe xmlns="http://www.portalfiscal.inf.br/nfe"><infNFe Id="NFe43210717229165000132550010000000061509881447" versao="4.00"><ide><cUF>43</cUF><cNF>50988144</cNF><natOp>5.101 Venda of producao do estabelecimento</natOp><mod>55</mod><serie>1</serie><nNF>6</nNF><dhEmi>2021-07-18T18:57:48-03:00</dhEmi><dhSaiEnt>2021-07-18T18:57:00-03:00</dhSaiEnt><tpNF>1</tpNF><idDest>1</idDest><cMunFG>4313409</cMunFG><tpImp>1</tpImp><tpEmis>1</tpEmis><cDV>7</cDV><tpAmb>2</tpAmb><finNFe>1</finNFe><indFinal>0</indFinal><indPres>1</indPres><procEmi>0</procEmi><verProc>TGS Sistemas 0.8b</verProc></ide><emit><CNPJ>17229165000132</CNPJ><xNome>INFINITY FABRICACAO E COM of CALCADOS EIRELI</xNome><enderEmit><xLgr>RUA CAMPO BOM</xLgr><nro>1045</nro><xBairro>CANUDOS</xBairro><cMun>4313409</cMun><xMun>NOVO HAMBURGO</xMun><UF>RS</UF><CEP>93542210</CEP><cPais>1058</cPais><xPais>BRASIL</xPais><fone>30673503</fone></enderEmit><IE>0860541673</IE><CRT>3</CRT></emit><dest><CNPJ>18044828
000107</CNPJ><xNome>NF-E EMITIDA EM AMBIENTE of HOMOLOGACAO - SEM VALOR FISCAL</xNome><enderDest><xLgr>AV JOSE CORREA DA SILVA</xLgr><nro>327</nro><xBairro>CAVALHADA</xBairro><cMun>4314902</cMun><xMun>PORTO ALEGRE</xMun><UF>RS</UF><CEP>91740380</CEP><cPais>1058</cPais><xPais>BRASIL</xPais><fone>5181215338</fone></enderDest><indIEDest>1</indIEDest><IE>963535455</IE><email>acessoriogaucho@gmail.com</email></dest><det nItem="1"><prod><cProd>1</cProd><cEAN>SEM GTIN</cEAN><xProd>TINTA Abs/ACR PRETO to/B</xProd><NCM>32089010</NCM><CEST>0000000</CEST><CFOP>5101</CFOP><uCom>KG</uCom><qCom>1.0000</qCom><vUnCom>1.0000000000</vUnCom><vProd>1.00</vProd><cEANTrib>SEM GTIN</cEANTrib><uTrib>KG</uTrib><qTrib>1.0000</qTrib><vUnTrib>1.0000000000</vUnTrib><indTot>1</indTot></prod><imposto><vTotTrib>0.35</vTotTrib><ICMS><ICMS00><orig>0</orig><CST>00</CST><modBC>3</modBC><vBC>1.00</vBC><pICMS>17.50</pICMS><vICMS>0.18</vICMS></ICMS00></ICMS><IPI><cEnq>999</cEnq><IPITrib><CST>99</CST><vBC>0.00</vBC><pIPI>0.
0
0</pIPI><vIPI>0.00</vIPI></IPITrib></IPI><PIS><PISOutr><CST>99</CST><qBCProd>0.00</qBCProd><vAliqProd>0.00</vAliqProd><vPIS>0.00</vPIS></PISOutr></PIS><COFINS><COFINSOutr><CST>99</CST><qBCProd>0.00</qBCProd><vAliqProd>0.00</vAliqProd><vCOFINS>0.00</vCOFINS></COFINSOutr></COFINS></imposto></det><total><ICMSTot><vBC>1.00</vBC><vICMS>0.18</vICMS><vICMSDeson>0.00</vICMSDeson><vFCPUFDest>0.00</vFCPUFDest><vICMSUFDest>0.00</vICMSUFDest><vICMSUFRemet>0.00</vICMSUFRemet><vFCP>0.00</vFCP><vBCST>0.00</vBCST><vST>0.00</vST><vFCPST>0.00</vFCPST><vFCPSTRet>0.00</vFCPSTRet><vProd>1.00</vProd><vFrete>0.00</vFrete><vSeg>0.00</vSeg><vDesc>0.00</vDesc><vII>0.00</vII><vIPI>0.00</vIPI><vIPIDevol>0.00</vIPIDevol><vPIS>0.00</vPIS><vCOFINS>0.00</vCOFINS><vOutro>0.00</vOutro><vNF>1.00</vNF><vTotTrib>0.35</vTotTrib></ICMSTot></total><transp><modFrete>1</modFrete><vol/></transp><pag><detPag><indPag>0</indPag><tPag>01</tPag><vPag>1.00</vPag></detPag></pag></infNFe></NFe>
]
==========================



Teste

Teste
teste