Sample code for 30+ languages & platforms
Objective-C

Sign Bolivia Invoice XML

See more XML Digital Signatures Examples

Demonstrates how to create an XML digital signature (XmlDSig) for a Bolivia invoice (factura).

Chilkat Objective-C Downloads

Objective-C
#import <CkoXml.h>
#import <CkoXmlDSigGen.h>
#import <CkoCert.h>
#import <CkoStringBuilder.h>
#import <CkoXmlDSig.h>

BOOL success = NO;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

success = YES;

// Create the following XML to be signed:

// <?xml version="1.0" encoding="UTF-8"?>
// <facturaElectronicaCompraVenta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="facturaElectronicaCompraVenta.xsd">
//     <cabecera>
//         <nitEmisor>99999999</nitEmisor>
//         <razonSocialEmisor>Abc Xyz</razonSocialEmisor>
//         <municipio>Cochabamba - Cochabamba</municipio>
//         <telefono>4444444</telefono>
//         <numeroFactura>417</numeroFactura>
//         <cuf>ABABABABABABABABABABABABABABABABABABABABABABABABABABABABA</cuf>
//         <cufd>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</cufd>
//         <codigoSucursal>0</codigoSucursal>
//         <direccion>aaa aaa aaa</direccion>
//         <codigoPuntoVenta>1</codigoPuntoVenta>
//         <fechaEmision>2023-04-13T22:43:22.417</fechaEmision>
//         <nombreRazonSocial>xyz</nombreRazonSocial>
//         <codigoTipoDocumentoIdentidad>1</codigoTipoDocumentoIdentidad>
//         <numeroDocumento>1234567</numeroDocumento>
//            <complemento xsi:nil="true"/>
//         <codigoCliente>1234567</codigoCliente>
//         <codigoMetodoPago>1</codigoMetodoPago>
//            <numeroTarjeta xsi:nil="true"/>
//         <montoTotal>22.00</montoTotal>
//         <montoTotalSujetoIva>22.00</montoTotalSujetoIva>
//         <codigoMoneda>1</codigoMoneda>
//         <tipoCambio>1.00</tipoCambio>
//         <montoTotalMoneda>22.00</montoTotalMoneda>
//            <montoGiftCard xsi:nil="true"/>
//         <descuentoAdicional>0.00</descuentoAdicional>
//            <codigoExcepcion>1</codigoExcepcion>
//            <cafc xsi:nil="true"/>
//         <leyenda>Ley N� 453: El proveedor de servicios debe habilitar medios e instrumentos para efectuar consultas y reclamaciones.</leyenda>
//         <usuario>nromero</usuario>
//         <codigoDocumentoSector>1</codigoDocumentoSector>
//     </cabecera>
//     <detalle>
//         <actividadEconomica>620100</actividadEconomica>
//         <codigoProductoSin>123456</codigoProductoSin>
//         <codigoProducto>1</codigoProducto>
//         <descripcion>Economicos</descripcion>
//         <cantidad>1.00</cantidad>
//         <unidadMedida>58</unidadMedida>
//         <precioUnitario>22.00</precioUnitario>
//         <montoDescuento>0.00</montoDescuento>
//         <subTotal>22.00</subTotal>
//            <numeroSerie xsi:nil="true"/>
//            <numeroImei xsi:nil="true"/>
//     </detalle>
// </facturaElectronicaCompraVenta>

// Use this online tool to generate code from sample XML: 
// Generate Code to Create XML

CkoXml *xml = [[CkoXml alloc] init];
xml.Tag = @"facturaElectronicaCompraVenta";
[xml AddAttribute: @"xmlns:xsi" value: @"http://www.w3.org/2001/XMLSchema-instance"];
[xml AddAttribute: @"xsi:noNamespaceSchemaLocation" value: @"facturaElectronicaCompraVenta.xsd"];
[xml UpdateChildContent: @"cabecera|nitEmisor" value: @"99999999"];
[xml UpdateChildContent: @"cabecera|razonSocialEmisor" value: @"Abc Xyz"];
[xml UpdateChildContent: @"cabecera|municipio" value: @"Cochabamba - Cochabamba"];
[xml UpdateChildContent: @"cabecera|telefono" value: @"4444444"];
[xml UpdateChildContent: @"cabecera|numeroFactura" value: @"417"];
[xml UpdateChildContent: @"cabecera|cuf" value: @"ABABABABABABABABABABABABABABABABABABABABABABABABABABABABA"];
[xml UpdateChildContent: @"cabecera|cufd" value: @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"];
[xml UpdateChildContent: @"cabecera|codigoSucursal" value: @"0"];
[xml UpdateChildContent: @"cabecera|direccion" value: @"aaa aaa aaa"];
[xml UpdateChildContent: @"cabecera|codigoPuntoVenta" value: @"1"];
[xml UpdateChildContent: @"cabecera|fechaEmision" value: @"2023-04-13T22:43:22.417"];
[xml UpdateChildContent: @"cabecera|nombreRazonSocial" value: @"xyz"];
[xml UpdateChildContent: @"cabecera|codigoTipoDocumentoIdentidad" value: @"1"];
[xml UpdateChildContent: @"cabecera|numeroDocumento" value: @"1234567"];
[xml UpdateAttrAt: @"cabecera|complemento" autoCreate: YES attrName: @"xsi:nil" attrValue: @"true"];
[xml UpdateChildContent: @"cabecera|codigoCliente" value: @"1234567"];
[xml UpdateChildContent: @"cabecera|codigoMetodoPago" value: @"1"];
[xml UpdateAttrAt: @"cabecera|numeroTarjeta" autoCreate: YES attrName: @"xsi:nil" attrValue: @"true"];
[xml UpdateChildContent: @"cabecera|montoTotal" value: @"22.00"];
[xml UpdateChildContent: @"cabecera|montoTotalSujetoIva" value: @"22.00"];
[xml UpdateChildContent: @"cabecera|codigoMoneda" value: @"1"];
[xml UpdateChildContent: @"cabecera|tipoCambio" value: @"1.00"];
[xml UpdateChildContent: @"cabecera|montoTotalMoneda" value: @"22.00"];
[xml UpdateAttrAt: @"cabecera|montoGiftCard" autoCreate: YES attrName: @"xsi:nil" attrValue: @"true"];
[xml UpdateChildContent: @"cabecera|descuentoAdicional" value: @"0.00"];
[xml UpdateChildContent: @"cabecera|codigoExcepcion" value: @"1"];
[xml UpdateAttrAt: @"cabecera|cafc" autoCreate: YES attrName: @"xsi:nil" attrValue: @"true"];
[xml UpdateChildContent: @"cabecera|leyenda" value: @"Ley N� 453: El proveedor de servicios debe habilitar medios e instrumentos para efectuar consultas y reclamaciones."];
[xml UpdateChildContent: @"cabecera|usuario" value: @"nromero"];
[xml UpdateChildContent: @"cabecera|codigoDocumentoSector" value: @"1"];
[xml UpdateChildContent: @"detalle|actividadEconomica" value: @"620100"];
[xml UpdateChildContent: @"detalle|codigoProductoSin" value: @"123456"];
[xml UpdateChildContent: @"detalle|codigoProducto" value: @"1"];
[xml UpdateChildContent: @"detalle|descripcion" value: @"Economicos"];
[xml UpdateChildContent: @"detalle|cantidad" value: @"1.00"];
[xml UpdateChildContent: @"detalle|unidadMedida" value: @"58"];
[xml UpdateChildContent: @"detalle|precioUnitario" value: @"22.00"];
[xml UpdateChildContent: @"detalle|montoDescuento" value: @"0.00"];
[xml UpdateChildContent: @"detalle|subTotal" value: @"22.00"];
[xml UpdateAttrAt: @"detalle|numeroSerie" autoCreate: YES attrName: @"xsi:nil" attrValue: @"true"];
[xml UpdateAttrAt: @"detalle|numeroImei" autoCreate: YES attrName: @"xsi:nil" attrValue: @"true"];

CkoXmlDSigGen *gen = [[CkoXmlDSigGen alloc] init];

gen.SigLocation = @"facturaElectronicaCompraVenta";
gen.SigLocationMod = [NSNumber numberWithInt:0];
gen.SigNamespacePrefix = @"";
gen.SigNamespaceUri = @"http://www.w3.org/2000/09/xmldsig#";
gen.SignedInfoCanonAlg = @"C14N";
gen.SignedInfoDigestMethod = @"sha256";

[gen AddSameDocRef: @"" digestMethod: @"sha256" canonMethod: @"C14N_WithComments" prefixList: @"" refType: @""];

// Provide your certificate + private key. (PFX password is test123)
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadPfxFile: @"qa_data/pfx/cert_test123.pfx" password: @"test123"];
if (success != YES) {
    NSLog(@"%@",cert.LastErrorText);
    return;
}

[gen SetX509Cert: cert usePrivateKey: YES];

gen.KeyInfoType = @"X509Data";
gen.X509Type = @"Certificate";

gen.Behaviors = @"EnvelopedTransformFirst";

// Load XML to be signed...
CkoStringBuilder *sbXml = [[CkoStringBuilder alloc] init];
xml.EmitCompact = YES;
[xml GetXmlSb: sbXml];

// Sign the XML...
success = [gen CreateXmlDSigSb: sbXml];
if (success != YES) {
    NSLog(@"%@",gen.LastErrorText);
    return;
}

// -----------------------------------------------

// Save the signed XML to a file.
success = [sbXml WriteFile: @"qa_output/signedXml.xml" charset: @"utf-8" emitBom: YES];

NSLog(@"%@",[sbXml GetAsString]);

// ----------------------------------------
// Verify the signatures we just produced...
CkoXmlDSig *verifier = [[CkoXmlDSig alloc] init];
success = [verifier LoadSignatureSb: sbXml];
if (success != YES) {
    NSLog(@"%@",verifier.LastErrorText);
    return;
}

int numSigs = [verifier.NumSignatures intValue];
int verifyIdx = 0;
while (verifyIdx < numSigs) {
    verifier.Selector = [NSNumber numberWithInt: verifyIdx];
    BOOL verified = [verifier VerifySignature: YES];
    if (verified != YES) {
        NSLog(@"%@",verifier.LastErrorText);
        return;
    }

    verifyIdx = verifyIdx + 1;
}

NSLog(@"%@",@"All signatures were successfully verified.");