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