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
(Unicode C++) 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).
#include <CkXmlW.h> #include <CkXmlDSigGenW.h> #include <CkCertW.h> #include <CkStringBuilderW.h> #include <CkXmlDSigW.h> void ChilkatSample(void) { bool success = true; // Create the XML to be signed... // Use this online tool to generate code from sample XML: // Generate Code to Create XML CkXmlW xmlToSign; xmlToSign.put_Tag(L"rDE"); xmlToSign.AddAttribute(L"xmlns",L"http://ekuatia.set.gov.py/sifen/xsd"); xmlToSign.AddAttribute(L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance"); xmlToSign.AddAttribute(L"xsi:schemaLocation",L"http://ekuatia.set.gov.py/sifen/xsd siRecepDE_v150.xsd"); xmlToSign.UpdateChildContent(L"dVerFor",L"150"); xmlToSign.UpdateAttrAt(L"DE",true,L"Id",L"01800228774059001001501012022091519999999990"); xmlToSign.UpdateChildContent(L"DE|dDVId",L"0"); xmlToSign.UpdateChildContent(L"DE|dFecFirma",L"2022-09-15T10:23:05"); xmlToSign.UpdateChildContent(L"DE|dSisFact",L"1"); xmlToSign.UpdateChildContent(L"DE|gOpeDE|iTipEmi",L"1"); xmlToSign.UpdateChildContent(L"DE|gOpeDE|dDesTipEmi",L"Normal"); xmlToSign.UpdateChildContent(L"DE|gOpeDE|dCodSeg",L"999999999"); xmlToSign.UpdateChildContent(L"DE|gOpeDE|dInfoEmi",L"GENERADA DE PEDIDO 131043202"); xmlToSign.UpdateChildContent(L"DE|gTimb|iTiDE",L"1"); xmlToSign.UpdateChildContent(L"DE|gTimb|dDesTiDE",L"Factura electrnica"); xmlToSign.UpdateChildContent(L"DE|gTimb|dNumTim",L"18889014"); xmlToSign.UpdateChildContent(L"DE|gTimb|dEst",L"059"); xmlToSign.UpdateChildContent(L"DE|gTimb|dPunExp",L"001"); xmlToSign.UpdateChildContent(L"DE|gTimb|dNumDoc",L"0015010"); xmlToSign.UpdateChildContent(L"DE|gTimb|dFeIniT",L"2022-05-25"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|dFeEmiDE",L"2022-09-15T10:22:57"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gOpeCom|iTipTra",L"1"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gOpeCom|dDesTipTra",L"Venta de mercadera"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gOpeCom|iTImp",L"1"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gOpeCom|dDesTImp",L"IVA"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gOpeCom|cMoneOpe",L"PYG"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gOpeCom|dDesMoneOpe",L"Guarani"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dRucEm",L"80022877"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dDVEmi",L"4"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|iTipCont",L"1"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|cTipReg",L"1"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dNomEmi",L"ABCDEF SA"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dNomFanEmi",L"ABCDEF SA"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dDirEmi",L"Jos Abcde Xyz N 666 e/Abxyz123"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dNumCas",L"0"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dCompDir1",L"N/A"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dCompDir2",L"N/A"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|cDepEmi",L"12"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dDesDepEmi",L"CENTRAL"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|cDisEmi",L"153"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dDesDisEmi",L"CAPIATA"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|cCiuEmi",L"1234"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dDesCiuEmi",L"CAPIATA"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dTelEmi",L"(022) 1111 000"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dEmailE",L"somebody@example.com.py"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|dDenSuc",L"PUNTO 99"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|gActEco|cActEco",L"47721"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gEmis|gActEco|dDesActEco",L"COMERCIO AL POR MENOR DE PRODUCTOS"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|iNatRec",L"1"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|iTiOpe",L"2"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|cPaisRec",L"PRY"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|dDesPaisRe",L"Paraguay"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|iTiContRec",L"2"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|dRucRec",L"80082971"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|dDVRec",L"9"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|dNomRec",L"XXXXXXX"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|dNomFanRec",L"XXXXXXX"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|dNumCasRec",L"0"); xmlToSign.UpdateChildContent(L"DE|gDatGralOpe|gDatRec|dCodCliente",L"9999999"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamFE|iIndPres",L"1"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamFE|dDesIndPres",L"Operacin presencial"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|iCondOpe",L"1"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|dDCondOpe",L"Contado"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni|iTiPago",L"1"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni|dDesTiPag",L"Efectivo"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni|dMonTiPag",L"100000.0000"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni|cMoneTiPag",L"PYG"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni|dDMoneTiPag",L"Guarani"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni[1]|iTiPago",L"1"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni[1]|dDesTiPag",L"Efectivo"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni[1]|dMonTiPag",L"73600.0000"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni[1]|cMoneTiPag",L"PYG"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamCond|gPaConEIni[1]|dDMoneTiPag",L"Guarani"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|dCodInt",L"818181"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|dDesProSer",L"Widget Abc Xyz"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|cUniMed",L"77"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|dDesUniMed",L"UNI"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|dCantProSer",L"2.0000"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gValorItem|dPUniProSer",L"16500.0000"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gValorItem|dTotBruOpeItem",L"33000.0000"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gValorItem|gValorRestaItem|dDescItem",L"3300.0000"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gValorItem|gValorRestaItem|dPorcDesIt",L"20.0000"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gValorItem|gValorRestaItem|dTotOpeItem",L"26400.0000"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gCamIVA|iAfecIVA",L"1"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gCamIVA|dDesAfecIVA",L"Gravado IVA"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gCamIVA|dPropIVA",L"100"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gCamIVA|dTasaIVA",L"5"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gCamIVA|dBasGravIVA",L"25142.8571"); xmlToSign.UpdateChildContent(L"DE|gDtipDE|gCamItem|gCamIVA|dLiqIVAItem",L"1257.1429"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dSubExe",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dSub5",L"26400.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dSub10",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dTotOpe",L"26400.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dTotDesc",L"6600.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dTotDescGlotem",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dTotAntItem",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dTotAnt",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dPorcDescTotal",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dDescTotal",L"6600.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dAnticipo",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dRedon",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dTotGralOpe",L"26400.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dIVA5",L"1257.1429"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dIVA10",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dTotIVA",L"1257.1429"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dBaseGrav5",L"25142.8571"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dBaseGrav10",L"0.0000"); xmlToSign.UpdateChildContent(L"DE|gTotSub|dTBasGraIVA",L"25142.8571"); CkXmlDSigGenW gen; gen.put_SigLocation(L"rDE"); gen.put_SigLocationMod(0); gen.put_SigNamespacePrefix(L""); gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#"); gen.put_SignedInfoCanonAlg(L"C14N"); gen.put_SignedInfoDigestMethod(L"sha256"); // -------- Reference 1 -------- CkXmlW xml1; xml1.put_Tag(L"Transforms"); xml1.UpdateAttrAt(L"Transform",true,L"Algorithm",L"http://www.w3.org/2000/09/xmldsig#enveloped-signature"); xml1.UpdateAttrAt(L"Transform[1]",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#"); gen.AddSameDocRef2(L"01800228774059001001501012022091516941989060",L"sha256",xml1,L""); // Provide a certificate + private key. (PFX password is test123) CkCertW cert; success = cert.LoadPfxFile(L"qa_data/pfx/cert_test123.pfx",L"test123"); if (success != true) { wprintf(L"%s\n",cert.lastErrorText()); return; } gen.SetX509Cert(cert,true); gen.put_KeyInfoType(L"X509Data"); gen.put_X509Type(L"Certificate"); // Load XML to be signed... CkStringBuilderW sbXml; xmlToSign.GetXmlSb(sbXml); gen.put_Behaviors(L"IndentedSignature"); // Sign the XML... success = gen.CreateXmlDSigSb(sbXml); if (success != true) { wprintf(L"%s\n",gen.lastErrorText()); return; } // ----------------------------------------------- // Save the signed XML to a file. success = sbXml.WriteFile(L"c:/temp/qa_output/signedXml.xml",L"utf-8",false); wprintf(L"%s\n",sbXml.getAsString()); // ---------------------------------------- // Verify the signatures we just produced... CkXmlDSigW verifier; success = verifier.LoadSignatureSb(sbXml); if (success != true) { wprintf(L"%s\n",verifier.lastErrorText()); return; } int numSigs = verifier.get_NumSignatures(); int verifyIdx = 0; while (verifyIdx < numSigs) { verifier.put_Selector(verifyIdx); bool verified = verifier.VerifySignature(true); if (verified != true) { wprintf(L"%s\n",verifier.lastErrorText()); return; } verifyIdx = verifyIdx + 1; } wprintf(L"All signatures were successfully verified.\n"); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.