Swift
Swift
Cape Verde Fatura Eletrónica XML Digital Signature
See more XML Digital Signatures Examples
Demonstrates how to sign XML to create a signed invoice compatible with the Cape Verde Fatura Eletrónica requirements.Note: This example requires Chilkat v9.5.0.89 or greater for a small feature to provide the ability produce mult-line base64 signature ending with
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First create the following XML to be signed. This can be created programmatically as shown in the code below, or
// you can simply load the XML to be signed from a file..
// <?xml version="1.0" encoding="utf-8"?>
// <Dfe xmlns="urn:cv:efatura:xsd:v1.0" DocumentTypeCode="1" Id="CV2210818123456789000010100000000546735014601" Version="1.0">
// <Invoice>
// <LedCode>1</LedCode>
// <Serie>SL</Serie>
// <DocumentNumber>5</DocumentNumber>
// <IssueDate>2021-08-30</IssueDate>
// <IssueTime>00:01:00</IssueTime>
// <TaxPointDate>2021-08-18</TaxPointDate>
// <EmitterParty>
// <TaxId CountryCode="CV">123456789</TaxId>
// <Name>Maria</Name>
// <Address CountryCode="CV">
// <State>Estado do Emissor</State>
// <City>Cidade do Emissor</City>
// <Region>Regi�o do Emissor</Region>
// <Street>Rua do Emissor</Street>
// <StreetDetail>Detalhe da rua do Emissor</StreetDetail>
// <BuildingName>Pr�dio do Emissor</BuildingName>
// <BuildingNumber>1</BuildingNumber>
// <BuildingFloor>2</BuildingFloor>
// <PostalCode>7600</PostalCode>
// <AddressDetail>Detalhe de endere�o do Emissor</AddressDetail>
// <AddressCode>CV774999999999999319</AddressCode>
// </Address>
// <Contacts>
// <Telephone>1234567</Telephone>
// <Mobilephone>1234568</Mobilephone>
// <Telefax>1234569</Telefax>
// <Email>somebody@example.com</Email>
// <Website>emissor.efatura.cv</Website>
// </Contacts>
// </EmitterParty>
// <ReceiverParty>
// <TaxId CountryCode="CV">123456789</TaxId>
// <Name>Nome do Recetor</Name>
// <Address CountryCode="CV">
// <State>Estado do Recetor</State>
// <City>Cidade do Recetor</City>
// <Region>Regi�o do Recetor</Region>
// <Street>Rua do Recetor</Street>
// <StreetDetail>Detalhe da rua do Recetor</StreetDetail>
// <BuildingName>Pr�dio do Recetor</BuildingName>
// <BuildingNumber>2</BuildingNumber>
// <BuildingFloor>3</BuildingFloor>
// <PostalCode>7601</PostalCode>
// <AddressDetail>Detalhe de endere�o do Recetor</AddressDetail>
// <AddressCode>CV774999999999999319</AddressCode>
// </Address>
// <Contacts>
// <Telephone>2234567</Telephone>
// <Mobilephone>2234568</Mobilephone>
// <Telefax>2234569</Telefax>
// <Email>rector@efatura.cv</Email>
// <Website>rector.efatura.cv</Website>
// </Contacts>
// </ReceiverParty>
// <Lines>
// <Line LineTypeCode="N">
// <Id>01</Id>
// <OrderLineReference>1</OrderLineReference>
// <Quantity UnitCode="EA">3.15</Quantity>
// <Price>123456</Price>
// <PriceExtension>388886</PriceExtension>
// <Discount>15</Discount>
// <NetTotal>252776</NetTotal>
// <Tax TaxTypeCode="IVA">
// <TaxPercentage>15</TaxPercentage>
// </Tax>
// <Item>
// <Description>Telem�vel</Description>
// <Name>Telem�vel</Name>
// <BrandName>Apple</BrandName>
// <ModelName>iPhone X</ModelName>
// <EmitterIdentification>TLMV</EmitterIdentification>
// <StandardIdentification>
// <GTIN>12345</GTIN>
// </StandardIdentification>
// <HazardousRiskIndicator>false</HazardousRiskIndicator>
// </Item>
// </Line>
// </Lines>
// <Totals>
// <PriceExtensionTotalAmount>388886</PriceExtensionTotalAmount>
// <ChargeTotalAmount>0</ChargeTotalAmount>
// <DiscountTotalAmount>136110</DiscountTotalAmount>
// <NetTotalAmount>252776</NetTotalAmount>
// <Discount ValueType="P">20</Discount>
// <TaxTotalAmount>37916</TaxTotalAmount>
// <PayableAmount>290692</PayableAmount>
// </Totals>
// <References>
// <Reference>
// <FiscalDocument IsOldDocument="false">CV1200520123456789000112345678901112345678901</FiscalDocument>
// <PaymentAmount>12345</PaymentAmount>
// <Tax TaxTypeCode="IR">
// <TaxPercentage>10</TaxPercentage>
// </Tax>
// </Reference>
// <Reference>
// <FiscalDocument IsOldDocument="false">CV1200520123456789000112345678901112345678903</FiscalDocument>
// <PaymentAmount>98765</PaymentAmount>
// <Tax TaxTypeCode="IVA">
// <TaxPercentage>15</TaxPercentage>
// </Tax>
// </Reference>
// </References>
// <Payments>
// <PaymentDueDate>2021-12-31</PaymentDueDate>
// <PaymentTerms>
// <Note>Juros de 12% a partir da data de vencimento</Note>
// </PaymentTerms>
// <PayeeFinancialAccount>
// <NIB>123456789012345678901</NIB>
// <Name>Nome da Conta 1</Name>
// </PayeeFinancialAccount>
// <PayeeFinancialAccount>
// <NIB>123456789012345678902</NIB>
// <Name>Nome da Conta 2</Name>
// </PayeeFinancialAccount>
// </Payments>
// <Note>Texto livre</Note>
// </Invoice>
// <Transmission>
// <IssueMode>2</IssueMode>
// <TransmitterTaxId CountryCode="CV">123456789</TransmitterTaxId>
// <Software>
// <Code>EMIPUB</Code>
// <Name>DNRE Emissor P�blico</Name>
// <Version>Build #53 12-07-2021 14:00:47 CVT - master</Version>
// </Software>
// <Contingency>
// <LedCode>1</LedCode>
// <IssueDate>2020-01-01</IssueDate>
// <IssueTime>01:02:03</IssueTime>
// <ReasonTypeCode>1</ReasonTypeCode>
// </Contingency>
// </Transmission>
// <RepositoryCode>2</RepositoryCode>
// </Dfe>
success = true
// Create the XML to be signed...
// Use this online tool to generate code from sample XML:
// Generate Code to Create XML
let xmlToSign = CkoXml()!
xmlToSign.tag = "Dfe"
xmlToSign.addAttribute(name: "xmlns", value: "urn:cv:efatura:xsd:v1.0")
xmlToSign.addAttribute(name: "DocumentTypeCode", value: "1")
xmlToSign.addAttribute(name: "Id", value: "CV2210818123456789000010100000000546735014601")
xmlToSign.addAttribute(name: "Version", value: "1.0")
xmlToSign.updateChildContent(tagPath: "Invoice|LedCode", value: "1")
xmlToSign.updateChildContent(tagPath: "Invoice|Serie", value: "SL")
xmlToSign.updateChildContent(tagPath: "Invoice|DocumentNumber", value: "5")
xmlToSign.updateChildContent(tagPath: "Invoice|IssueDate", value: "2021-08-30")
xmlToSign.updateChildContent(tagPath: "Invoice|IssueTime", value: "00:01:00")
xmlToSign.updateChildContent(tagPath: "Invoice|TaxPointDate", value: "2021-08-18")
xmlToSign.updateAttrAt(tagPath: "Invoice|EmitterParty|TaxId", autoCreate: true, attrName: "CountryCode", attrValue: "CV")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|TaxId", value: "123456789")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Name", value: "Maria")
xmlToSign.updateAttrAt(tagPath: "Invoice|EmitterParty|Address", autoCreate: true, attrName: "CountryCode", attrValue: "CV")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|State", value: "Estado do Emissor")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|City", value: "Cidade do Emissor")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|Region", value: "Regi�o do Emissor")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|Street", value: "Rua do Emissor")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|StreetDetail", value: "Detalhe da rua do Emissor")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|BuildingName", value: "Pr�dio do Emissor")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|BuildingNumber", value: "1")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|BuildingFloor", value: "2")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|PostalCode", value: "7600")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|AddressDetail", value: "Detalhe de endere�o do Emissor")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Address|AddressCode", value: "CV774999999999999319")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Contacts|Telephone", value: "1234567")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Contacts|Mobilephone", value: "1234568")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Contacts|Telefax", value: "1234569")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Contacts|Email", value: "somebody@example.com")
xmlToSign.updateChildContent(tagPath: "Invoice|EmitterParty|Contacts|Website", value: "emissor.efatura.cv")
xmlToSign.updateAttrAt(tagPath: "Invoice|ReceiverParty|TaxId", autoCreate: true, attrName: "CountryCode", attrValue: "CV")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|TaxId", value: "123456789")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Name", value: "Nome do Recetor")
xmlToSign.updateAttrAt(tagPath: "Invoice|ReceiverParty|Address", autoCreate: true, attrName: "CountryCode", attrValue: "CV")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|State", value: "Estado do Recetor")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|City", value: "Cidade do Recetor")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|Region", value: "Regi�o do Recetor")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|Street", value: "Rua do Recetor")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|StreetDetail", value: "Detalhe da rua do Recetor")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|BuildingName", value: "Pr�dio do Recetor")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|BuildingNumber", value: "2")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|BuildingFloor", value: "3")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|PostalCode", value: "7601")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|AddressDetail", value: "Detalhe de endere�o do Recetor")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Address|AddressCode", value: "CV774999999999999319")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Contacts|Telephone", value: "2234567")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Contacts|Mobilephone", value: "2234568")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Contacts|Telefax", value: "2234569")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Contacts|Email", value: "rector@efatura.cv")
xmlToSign.updateChildContent(tagPath: "Invoice|ReceiverParty|Contacts|Website", value: "rector.efatura.cv")
xmlToSign.updateAttrAt(tagPath: "Invoice|Lines|Line", autoCreate: true, attrName: "LineTypeCode", attrValue: "N")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Id", value: "01")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|OrderLineReference", value: "1")
xmlToSign.updateAttrAt(tagPath: "Invoice|Lines|Line|Quantity", autoCreate: true, attrName: "UnitCode", attrValue: "EA")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Quantity", value: "3.15")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Price", value: "123456")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|PriceExtension", value: "388886")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Discount", value: "15")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|NetTotal", value: "252776")
xmlToSign.updateAttrAt(tagPath: "Invoice|Lines|Line|Tax", autoCreate: true, attrName: "TaxTypeCode", attrValue: "IVA")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Tax|TaxPercentage", value: "15")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Item|Description", value: "Telem�vel")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Item|Name", value: "Telem�vel")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Item|BrandName", value: "Apple")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Item|ModelName", value: "iPhone X")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Item|EmitterIdentification", value: "TLMV")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Item|StandardIdentification|GTIN", value: "12345")
xmlToSign.updateChildContent(tagPath: "Invoice|Lines|Line|Item|HazardousRiskIndicator", value: "false")
xmlToSign.updateChildContent(tagPath: "Invoice|Totals|PriceExtensionTotalAmount", value: "388886")
xmlToSign.updateChildContent(tagPath: "Invoice|Totals|ChargeTotalAmount", value: "0")
xmlToSign.updateChildContent(tagPath: "Invoice|Totals|DiscountTotalAmount", value: "136110")
xmlToSign.updateChildContent(tagPath: "Invoice|Totals|NetTotalAmount", value: "252776")
xmlToSign.updateAttrAt(tagPath: "Invoice|Totals|Discount", autoCreate: true, attrName: "ValueType", attrValue: "P")
xmlToSign.updateChildContent(tagPath: "Invoice|Totals|Discount", value: "20")
xmlToSign.updateChildContent(tagPath: "Invoice|Totals|TaxTotalAmount", value: "37916")
xmlToSign.updateChildContent(tagPath: "Invoice|Totals|PayableAmount", value: "290692")
xmlToSign.updateAttrAt(tagPath: "Invoice|References|Reference|FiscalDocument", autoCreate: true, attrName: "IsOldDocument", attrValue: "false")
xmlToSign.updateChildContent(tagPath: "Invoice|References|Reference|FiscalDocument", value: "CV1200520123456789000112345678901112345678901")
xmlToSign.updateChildContent(tagPath: "Invoice|References|Reference|PaymentAmount", value: "12345")
xmlToSign.updateAttrAt(tagPath: "Invoice|References|Reference|Tax", autoCreate: true, attrName: "TaxTypeCode", attrValue: "IR")
xmlToSign.updateChildContent(tagPath: "Invoice|References|Reference|Tax|TaxPercentage", value: "10")
xmlToSign.updateAttrAt(tagPath: "Invoice|References|Reference[1]|FiscalDocument", autoCreate: true, attrName: "IsOldDocument", attrValue: "false")
xmlToSign.updateChildContent(tagPath: "Invoice|References|Reference[1]|FiscalDocument", value: "CV1200520123456789000112345678901112345678903")
xmlToSign.updateChildContent(tagPath: "Invoice|References|Reference[1]|PaymentAmount", value: "98765")
xmlToSign.updateAttrAt(tagPath: "Invoice|References|Reference[1]|Tax", autoCreate: true, attrName: "TaxTypeCode", attrValue: "IVA")
xmlToSign.updateChildContent(tagPath: "Invoice|References|Reference[1]|Tax|TaxPercentage", value: "15")
xmlToSign.updateChildContent(tagPath: "Invoice|Payments|PaymentDueDate", value: "2021-12-31")
xmlToSign.updateChildContent(tagPath: "Invoice|Payments|PaymentTerms|Note", value: "Juros de 12% a partir da data de vencimento")
xmlToSign.updateChildContent(tagPath: "Invoice|Payments|PayeeFinancialAccount|NIB", value: "123456789012345678901")
xmlToSign.updateChildContent(tagPath: "Invoice|Payments|PayeeFinancialAccount|Name", value: "Nome da Conta 1")
xmlToSign.updateChildContent(tagPath: "Invoice|Payments|PayeeFinancialAccount[1]|NIB", value: "123456789012345678902")
xmlToSign.updateChildContent(tagPath: "Invoice|Payments|PayeeFinancialAccount[1]|Name", value: "Nome da Conta 2")
xmlToSign.updateChildContent(tagPath: "Invoice|Note", value: "Texto livre")
xmlToSign.updateChildContent(tagPath: "Transmission|IssueMode", value: "2")
xmlToSign.updateAttrAt(tagPath: "Transmission|TransmitterTaxId", autoCreate: true, attrName: "CountryCode", attrValue: "CV")
xmlToSign.updateChildContent(tagPath: "Transmission|TransmitterTaxId", value: "123456789")
xmlToSign.updateChildContent(tagPath: "Transmission|Software|Code", value: "EMIPUB")
xmlToSign.updateChildContent(tagPath: "Transmission|Software|Name", value: "DNRE Emissor P�blico")
xmlToSign.updateChildContent(tagPath: "Transmission|Software|Version", value: "Build #53 12-07-2021 14:00:47 CVT - master")
xmlToSign.updateChildContent(tagPath: "Transmission|Contingency|LedCode", value: "1")
xmlToSign.updateChildContent(tagPath: "Transmission|Contingency|IssueDate", value: "2020-01-01")
xmlToSign.updateChildContent(tagPath: "Transmission|Contingency|IssueTime", value: "01:02:03")
xmlToSign.updateChildContent(tagPath: "Transmission|Contingency|ReasonTypeCode", value: "1")
xmlToSign.updateChildContent(tagPath: "RepositoryCode", value: "2")
let gen = CkoXmlDSigGen()!
gen.sigLocation = "Dfe"
gen.sigLocationMod = 0
gen.sigId = "xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac"
gen.sigNamespacePrefix = "ds"
gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
gen.sigValueId = "xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-sigvalue"
gen.signedInfoCanonAlg = "C14N"
gen.signedInfoDigestMethod = "sha256"
// Set the KeyInfoId before adding references..
gen.keyInfoId = "xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-keyinfo"
// Create an Object to be added to the Signature.
let object1 = CkoXml()!
object1.tag = "xades:QualifyingProperties"
object1.addAttribute(name: "xmlns:xades", value: "http://uri.etsi.org/01903/v1.3.2#")
object1.addAttribute(name: "xmlns:xades141", value: "http://uri.etsi.org/01903/v1.4.1#")
object1.addAttribute(name: "Target", value: "#xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac")
object1.updateAttrAt(tagPath: "xades:SignedProperties", autoCreate: true, attrName: "Id", attrValue: "xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-signedprops")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", value: "TO BE GENERATED BY CHILKAT")
object1.updateAttrAt(tagPath: "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod", autoCreate: true, attrName: "Algorithm", attrValue: "http://www.w3.org/2001/04/xmlenc#sha256")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue", value: "TO BE GENERATED BY CHILKAT")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2", value: "TO BE GENERATED BY CHILKAT")
object1.updateAttrAt(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat", autoCreate: true, attrName: "ObjectReference", attrValue: "#xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-ref0")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description", value: "DFE Signature")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier", value: "efatura-uri")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:DocumentationReferences|xades:DocumentationReference", value: "https://efatura.cv")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType", value: "text/xml")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding", value: "UTF-8")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:CommitmentTypeId|xades:Identifier", value: "http://uri.etsi.org/01903/v1.2.2#ProofOfApproval")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:CommitmentTypeId|xades:Description", value: "Indicates that the signer has approved the content of the signed data object")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:AllSignedDataObjects", value: "")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication[1]|xades:CommitmentTypeId|xades:Identifier", value: "http://uri.etsi.org/01903/v1.2.2#ProofOfOrigin")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication[1]|xades:CommitmentTypeId|xades:Description", value: "Indicates that the signer recognizes to have created, approved and sent the signed data object")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication[1]|xades:ObjectReference", value: "#xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-ref0")
object1.updateChildContent(tagPath: "xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication[1]|xades:CommitmentTypeQualifiers|xades:CommitmentTypeQualifier", value: "CV-FE-123456789")
gen.add(id: "", content: object1.getXml(), mimeType: "", encoding: "")
// -------- Reference 1 --------
gen.addSameDocRef(id: "CV2210818123456789000010100000000546735014601", digestMethod: "sha256", canonMethod: "", prefixList: "", refType: "")
gen.setRefIdAttr(uri_or_id: "CV2210818123456789000010100000000546735014601", value: "xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-ref0")
// -------- Reference 2 --------
gen.addSameDocRef(id: "xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-keyinfo", digestMethod: "sha256", canonMethod: "C14N", prefixList: "", refType: "")
// -------- Reference 3 --------
gen.addObjectRef(id: "xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-signedprops", digestMethod: "sha256", canonMethod: "C14N", prefixList: "", refType: "http://uri.etsi.org/01903#SignedProperties")
// 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"
gen.x509Type = "IssuerSerial,SubjectName,Certificate"
// Load XML to be signed...
let sbXml = CkoStringBuilder()!
xmlToSign.getSb(sb: sbXml)
// Note: The last keyword (Base64Cr13Entity) requires Chilkat v9.5.0.89 or later.
gen.behaviors = "IndentedSignature,Base64Cr13Entity,ForceAddEnvelopedSignatureTransform,Base64Cr13Entity"
// 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_output/signedXml.xml", charset: "utf-8", 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.")
}