Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Paraguay E-Invoicing Signed XML (SIFEN)See more XML Digital Signatures ExamplesDemonstrates how to sign XML for e-invoicing -- SISTEMA INTEGRADO DE FACTURACIÓN ELECTRÓNICA NACIONAL (SIFEN). This is for signing XML files that need to be submitted to a government agency (Country Paraguay, SIFEN / EKUATIA, Sistema Nacional de Facturacion Electrónica).
IncludeFile "CkCert.pb" IncludeFile "CkXml.pb" IncludeFile "CkXmlDSigGen.pb" IncludeFile "CkXmlDSig.pb" IncludeFile "CkStringBuilder.pb" Procedure ChilkatExample() success.i = 1 ; Create the XML to be signed... ; Use this online tool to generate code from sample XML: ; Generate Code to Create XML xmlToSign.i = CkXml::ckCreate() If xmlToSign.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xmlToSign, "rDE") CkXml::ckAddAttribute(xmlToSign,"xmlns","http://ekuatia.set.gov.py/sifen/xsd") CkXml::ckAddAttribute(xmlToSign,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance") CkXml::ckAddAttribute(xmlToSign,"xsi:schemaLocation","http://ekuatia.set.gov.py/sifen/xsd siRecepDE_v150.xsd") CkXml::ckUpdateChildContent(xmlToSign,"dVerFor","150") CkXml::ckUpdateAttrAt(xmlToSign,"DE",1,"Id","01800228774059001001501012022091519999999990") CkXml::ckUpdateChildContent(xmlToSign,"DE|dDVId","0") CkXml::ckUpdateChildContent(xmlToSign,"DE|dFecFirma","2022-09-15T10:23:05") CkXml::ckUpdateChildContent(xmlToSign,"DE|dSisFact","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gOpeDE|iTipEmi","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gOpeDE|dDesTipEmi","Normal") CkXml::ckUpdateChildContent(xmlToSign,"DE|gOpeDE|dCodSeg","999999999") CkXml::ckUpdateChildContent(xmlToSign,"DE|gOpeDE|dInfoEmi","GENERADA DE PEDIDO 131043202") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTimb|iTiDE","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTimb|dDesTiDE","Factura electrnica") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTimb|dNumTim","18889014") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTimb|dEst","059") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTimb|dPunExp","001") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTimb|dNumDoc","0015010") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTimb|dFeIniT","2022-05-25") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|dFeEmiDE","2022-09-15T10:22:57") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gOpeCom|iTipTra","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gOpeCom|dDesTipTra","Venta de mercadera") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gOpeCom|iTImp","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gOpeCom|dDesTImp","IVA") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gOpeCom|cMoneOpe","PYG") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gOpeCom|dDesMoneOpe","Guarani") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dRucEm","80022877") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dDVEmi","4") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|iTipCont","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|cTipReg","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dNomEmi","ABCDEF SA") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dNomFanEmi","ABCDEF SA") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dDirEmi","Jos Abcde Xyz N 666 e/Abxyz123") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dNumCas","0") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dCompDir1","N/A") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dCompDir2","N/A") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|cDepEmi","12") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dDesDepEmi","CENTRAL") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|cDisEmi","153") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dDesDisEmi","CAPIATA") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|cCiuEmi","1234") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dDesCiuEmi","CAPIATA") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dTelEmi","(022) 1111 000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dEmailE","somebody@example.com.py") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|dDenSuc","PUNTO 99") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|gActEco|cActEco","47721") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gEmis|gActEco|dDesActEco","COMERCIO AL POR MENOR DE PRODUCTOS") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|iNatRec","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|iTiOpe","2") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|cPaisRec","PRY") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|dDesPaisRe","Paraguay") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|iTiContRec","2") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|dRucRec","80082971") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|dDVRec","9") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|dNomRec","XXXXXXX") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|dNomFanRec","XXXXXXX") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|dNumCasRec","0") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDatGralOpe|gDatRec|dCodCliente","9999999") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamFE|iIndPres","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamFE|dDesIndPres","Operacin presencial") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|iCondOpe","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|dDCondOpe","Contado") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni|iTiPago","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni|dDesTiPag","Efectivo") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni|dMonTiPag","100000.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni|cMoneTiPag","PYG") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni|dDMoneTiPag","Guarani") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni[1]|iTiPago","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni[1]|dDesTiPag","Efectivo") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni[1]|dMonTiPag","73600.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni[1]|cMoneTiPag","PYG") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamCond|gPaConEIni[1]|dDMoneTiPag","Guarani") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|dCodInt","818181") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|dDesProSer","Widget Abc Xyz") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|cUniMed","77") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|dDesUniMed","UNI") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|dCantProSer","2.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gValorItem|dPUniProSer","16500.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gValorItem|dTotBruOpeItem","33000.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gValorItem|gValorRestaItem|dDescItem","3300.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gValorItem|gValorRestaItem|dPorcDesIt","20.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gValorItem|gValorRestaItem|dTotOpeItem","26400.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gCamIVA|iAfecIVA","1") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gCamIVA|dDesAfecIVA","Gravado IVA") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gCamIVA|dPropIVA","100") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gCamIVA|dTasaIVA","5") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gCamIVA|dBasGravIVA","25142.8571") CkXml::ckUpdateChildContent(xmlToSign,"DE|gDtipDE|gCamItem|gCamIVA|dLiqIVAItem","1257.1429") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dSubExe","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dSub5","26400.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dSub10","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dTotOpe","26400.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dTotDesc","6600.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dTotDescGlotem","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dTotAntItem","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dTotAnt","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dPorcDescTotal","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dDescTotal","6600.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dAnticipo","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dRedon","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dTotGralOpe","26400.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dIVA5","1257.1429") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dIVA10","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dTotIVA","1257.1429") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dBaseGrav5","25142.8571") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dBaseGrav10","0.0000") CkXml::ckUpdateChildContent(xmlToSign,"DE|gTotSub|dTBasGraIVA","25142.8571") gen.i = CkXmlDSigGen::ckCreate() If gen.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXmlDSigGen::setCkSigLocation(gen, "rDE") CkXmlDSigGen::setCkSigLocationMod(gen, 0) CkXmlDSigGen::setCkSigNamespacePrefix(gen, "") CkXmlDSigGen::setCkSigNamespaceUri(gen, "http://www.w3.org/2000/09/xmldsig#") CkXmlDSigGen::setCkSignedInfoCanonAlg(gen, "C14N") CkXmlDSigGen::setCkSignedInfoDigestMethod(gen, "sha256") ; -------- Reference 1 -------- xml1.i = CkXml::ckCreate() If xml1.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xml1, "Transforms") CkXml::ckUpdateAttrAt(xml1,"Transform",1,"Algorithm","http://www.w3.org/2000/09/xmldsig#enveloped-signature") CkXml::ckUpdateAttrAt(xml1,"Transform[1]",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#") CkXmlDSigGen::ckAddSameDocRef2(gen,"01800228774059001001501012022091516941989060","sha256",xml1,"") ; Provide a certificate + private key. (PFX password is test123) cert.i = CkCert::ckCreate() If cert.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123") If success <> 1 Debug CkCert::ckLastErrorText(cert) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(xml1) CkCert::ckDispose(cert) ProcedureReturn EndIf CkXmlDSigGen::ckSetX509Cert(gen,cert,1) CkXmlDSigGen::setCkKeyInfoType(gen, "X509Data") CkXmlDSigGen::setCkX509Type(gen, "Certificate") ; Load XML to be signed... sbXml.i = CkStringBuilder::ckCreate() If sbXml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::ckGetXmlSb(xmlToSign,sbXml) CkXmlDSigGen::setCkBehaviors(gen, "IndentedSignature") ; Sign the XML... success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml) If success <> 1 Debug CkXmlDSigGen::ckLastErrorText(gen) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(xml1) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndIf ; ----------------------------------------------- ; Save the signed XML to a file. success = CkStringBuilder::ckWriteFile(sbXml,"c:/temp/qa_output/signedXml.xml","utf-8",0) Debug CkStringBuilder::ckGetAsString(sbXml) ; ---------------------------------------- ; Verify the signatures we just produced... verifier.i = CkXmlDSig::ckCreate() If verifier.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkXmlDSig::ckLoadSignatureSb(verifier,sbXml) If success <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(xml1) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf numSigs.i = CkXmlDSig::ckNumSignatures(verifier) verifyIdx.i = 0 While verifyIdx < numSigs CkXmlDSig::setCkSelector(verifier, verifyIdx) verified.i = CkXmlDSig::ckVerifySignature(verifier,1) If verified <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(xml1) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf verifyIdx = verifyIdx + 1 Wend Debug "All signatures were successfully verified." CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(xml1) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.