Sample code for 30+ languages & platforms
Swift

Create sii.cl Factura Electrónica (Chile Servicio de Impuestos Internos)

See more XAdES Examples

Demonstrates how to sign an XML invoice according to Chilean Internal Revenue Service regulations.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    // See: Apply 2nd Signature to sii.cl Factura Electr�nica (Chile Servicio de Impuestos Internos)
    // for an example showing how to apply a 2nd signature.

    // We want to sign XML that looks like the following:

    // <?xml version="1.0" encoding="iso-8859-1"?>
    // <DTE version="1.0" xmlns="http://www.sii.cl/SiiDte">
    //     <Documento ID="F13T34">
    //         <Encabezado>
    //             <IdDoc>
    //                 <TipoDTE>34</TipoDTE>
    //                 <Folio>13</Folio>
    //                 <FchEmis>2020-07-17</FchEmis>
    //                 <FmaPago>1</FmaPago>
    //                 <FchVenc>2020-07-17</FchVenc>
    //             </IdDoc>
    //             <Emisor>
    //                 <RUTEmisor>...</RUTEmisor>
    //                 <RznSoc>...</RznSoc>
    //                 <GiroEmis>...</GiroEmis>
    //                 <Acteco>...</Acteco>
    //                 <DirOrigen>...</DirOrigen>
    //                 <CmnaOrigen>...</CmnaOrigen>
    //                 <CiudadOrigen>...</CiudadOrigen>
    //             </Emisor>
    //             <Receptor>
    //                 <RUTRecep>...</RUTRecep>
    //                 <RznSocRecep>...</RznSocRecep>
    //                 <GiroRecep>...</GiroRecep>
    //                 <Contacto/>
    //                 <DirRecep>...</DirRecep>
    //                 <CmnaRecep>...</CmnaRecep>
    //                 <CiudadRecep>...</CiudadRecep>
    //             </Receptor>
    //             <Totales>
    //                 <MntExe>14999</MntExe>
    //                 <MntTotal>14999</MntTotal>
    //             </Totales>
    //         </Encabezado>
    //         <Detalle>
    //             <NroLinDet>1</NroLinDet>
    //             <CdgItem>
    //                 <TpoCodigo>INT</TpoCodigo>
    //                 <VlrCodigo>1</VlrCodigo>
    //             </CdgItem>
    //             <NmbItem>Atenci�n profesional mes de Junio 2020</NmbItem>
    //             <QtyItem>1</QtyItem>
    //             <UnmdItem>UNI</UnmdItem>
    //             <PrcItem>14999</PrcItem>
    //             <MontoItem>14999</MontoItem>
    //         </Detalle>
    //         <TED version="1.0">
    //             <DD>
    //                 <RE>99972220-K</RE>
    //                 <TD>34</TD>
    //                 <F>13</F>
    //                 <FE>2020-07-17</FE>
    //                 <RR>99942999-2</RR>
    //                 <RSR>...</RSR>
    //                 <MNT>14999</MNT>
    //                 <IT1>Atencion profesional mes de Junio 2020</IT1>
    //                 <CAF version="1.0">
    //                     <DA>
    //                         <RE>99972220-K</RE>
    //                         <RS>...</RS>
    //                         <TD>34</TD>
    //                         <RNG>
    //                             <D>3</D>
    //                             <H>12</H>
    //                         </RNG>
    //                         <FA>2019-10-10</FA>
    //                         <RSAPK>
    //                             <M>2zHVYpcVNQRvS2yFuqdrh...TEQZx/m0t9HVTgWKZvlc6LSQ==</M>
    //                             <E>Aw==</E>
    //                         </RSAPK>
    //                         <IDK>300</IDK>
    //                     </DA>
    //                     <FRMA algoritmo="SHA1withRSA">LaVkjISGu...sBtsQL1jR9lw==</FRMA>
    //                 </CAF>
    //                 <TSTED>2020-07-17T13:19:10</TSTED>
    //             </DD>
    //             <FRMT algoritmo="SHA1withRSA">LxZr6zmXRZIfTz7...IXS6sp4vfz2fIsA==</FRMT>
    //         </TED>
    //         <TmstFirma>2020-07-17T13:19:10</TmstFirma>
    //     </Documento>
    // </DTE>

    success = true

    let gen = CkoXmlDSigGen()!

    gen.sigLocation = "DTE"
    gen.sigLocationMod = 0
    gen.sigNamespacePrefix = ""
    gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
    gen.signedInfoCanonAlg = "C14N"
    gen.signedInfoDigestMethod = "sha1"

    // -------- Reference 1 --------
    gen.addSameDocRef(id: "F13T34", digestMethod: "sha1", canonMethod: "", prefixList: "", refType: "")

    // Provide a certificate + private key. (PFX password is test123)
    let cert = CkoCert()!
    success = cert.loadPfxFile(path: "qa_data/pfx/cert_test123.pfx", password: "test123")
    if success != true {
        print("\(cert.lastErrorText!)")
        return
    }

    gen.setX509Cert(cert: cert, usePrivateKey: true)

    gen.keyInfoType = "X509Data+KeyValue"
    gen.x509Type = "Certificate"

    // Load XML to be signed...
    let sbXml = CkoStringBuilder()!
    success = sbXml.loadFile(path: "qa_data/xml_dsig/sii_cl/xmlToSign.xml", charset: "iso-8859-1")
    if success == false {
        print("Failed to load XML file.")
        return
    }

    gen.behaviors = "IndentedSignature"

    // Sign the XML...
    success = gen.createXmlDSigSb(sbXml: sbXml)
    if success != true {
        print("\(gen.lastErrorText!)")
        return
    }

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

    // Save the signed XML to a file.
    success = sbXml.writeFile(path: "qa_data/xml_dsig/sii_cl/signed1.xml", charset: "iso-8859-1", emitBom: false)

    print("\(sbXml.getAsString()!)")

    // ----------------------------------------
    // Verify the signatures we just produced...
    let verifier = CkoXmlDSig()!
    success = verifier.loadSignatureSb(sbXmlSig: sbXml)
    if success != true {
        print("\(verifier.lastErrorText!)")
        return
    }

    var numSigs: Int = verifier.numSignatures.intValue
    var verifyIdx: Int = 0
    while verifyIdx < numSigs {
        verifier.selector = verifyIdx
        var verified: Bool = verifier.verifySignature(verifyReferenceDigests: true)
        if verified != true {
            print("\(verifier.lastErrorText!)")
            return
        }

        verifyIdx = verifyIdx + 1
    }

    print("All signatures were successfully verified.")

}