Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Swift 3,4,5... Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Swift 3,4,5...) IPS MX Signature - Digitally Sign MX Document

See more XML Digital Signatures Examples

Demonstrates how to digitally sign ISO 20022 SWIFT MX messages.

Note: This example requires Chilkat v9.5.0.89 or later. Additional internal functionality was added to auto-recognize SWIFT MX messages and do what is appropriate to satisfy the SWIFT MX specifications.

Chilkat Downloads for the Swift Programming Language

MAC OS X (Cocoa) Objective-C/Swift Libs

iOS Objective-C/Swift Libs

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

    var success: Bool = true

    // First create the XML to be signed, or load it from a file, or a string,

    // To load XML from a file:
    let xmlToSign = CkoXml()!
    success = xmlToSign.loadFile("c:/someDir/mx_document.xml")

    // Or to load XML from a string
    success = xmlToSign.load("...")

    // Or create the XML directly.
    xmlToSign.clear()

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

    xmlToSign.tag = "DataPDU"
    xmlToSign.addAttribute("xmlns", value: "urn:cma:stp:xsd:stp.1.0")
    xmlToSign.updateAttr(at: "Body|AppHdr", autoCreate: true, attrName: "xmlns", attrValue: "urn:iso:std:iso:20022:tech:xsd:head.001.001.01")
    xmlToSign.updateChildContent("Body|AppHdr|Fr|FIId|FinInstnId|BICFI", value: "ZZZZZZZZ")
    xmlToSign.updateChildContent("Body|AppHdr|To|FIId|FinInstnId|BICFI", value: "YYYYYYYYYY")
    xmlToSign.updateChildContent("Body|AppHdr|BizMsgIdr", value: "ZZZZZZZZAXXX999999999999999999999")
    xmlToSign.updateChildContent("Body|AppHdr|MsgDefIdr", value: "pacs.008.001.08")
    xmlToSign.updateChildContent("Body|AppHdr|BizSvc", value: "IPS")
    xmlToSign.updateChildContent("Body|AppHdr|CreDt", value: "2017-09-13T18:18:00Z")
    xmlToSign.updateAttr(at: "Body|Document", autoCreate: true, attrName: "xmlns", attrValue: "urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|MsgId", value: "ZZZZZZZZAXXX999999999999999999999")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|CreDtTm", value: "2017-09-13T18:18:00")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|NbOfTxs", value: "1")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|SttlmInf|SttlmMtd", value: "CLRG")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|EndToEndId", value: "NOTPROVIDED")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|TxId", value: "ZZZZZZZZAXXX999999999999999999999")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|ClrChanl", value: "RTNS")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|LclInstrm|Prtry", value: "CSCT")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|CtgyPurp|Prtry", value: "001")
    xmlToSign.updateAttr(at: "Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt", autoCreate: true, attrName: "Ccy", attrValue: "JOD")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt", value: "71.12")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmDt", value: "2018-01-14")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|ChrgBr", value: "SLEV")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstgAgt|FinInstnId|BICFI", value: "ZZZZZZZZ")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstdAgt|FinInstnId|BICFI", value: "UBSIJOA0")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Dbtr|Nm", value: "John Johnson")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAcct|Id|IBAN", value: "JO22CITI00000000000555555555")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|BICFI", value: "ZZZZZZZZ")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|Id", value: "200004")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|SchmeNm|Prtry", value: "1700099999")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgtAcct|Id|IBAN", value: "JO66CITI22222222222222222222")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|BICFI", value: "UBSIJOA0")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|Id", value: "210027")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|SchmeNm|Prtry", value: "1400199999")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgtAcct|Id|IBAN", value: "JO44UBSI33333333333333333333")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Cdtr|Nm", value: "Omega Jones")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAcct|Id|IBAN", value: "JO95UBSI00000000000777777777")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstrForNxtAgt|InstrInf", value: "/BNF/Details")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Purp|Prtry", value: "5814")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf", value: "SOMEINFORMATIONABOUTPAYMENT-1")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[1]", value: "SOMEINFORMATIONABOUTPAYMENT-2")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[2]", value: "SOMEINFORMATIONABOUTPAYMENT-3")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Cdtr|TaxId", value: "9900083901")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Dbtr|TaxId", value: "1000387561")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RmtInf|Ustrd", value: "EDV UCUN ODENIR")
    xmlToSign.updateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RmtInf|Ustrd[1]", value: "EXTRA INFO")

    // The following XML is to be signed:

    // <?xml version="1.0" encoding="UTF-8"?>
    // <DataPDU xmlns="urn:cma:stp:xsd:stp.1.0">
    // 	<Body>
    // 		<AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
    // 			<Fr>
    // 				<FIId>
    // 					<FinInstnId>
    // 						<BICFI>ZZZZZZZZ</BICFI>
    // 					</FinInstnId>
    // 				</FIId>
    // 			</Fr>
    // 			<To>
    // 				<FIId>
    // 					<FinInstnId>
    // 						<BICFI>YYYYYYYYYY</BICFI>
    // 					</FinInstnId>
    // 				</FIId>
    // 			</To>
    // 			<BizMsgIdr>ZZZZZZZZAXXX999999999999999999999</BizMsgIdr>
    // 			<MsgDefIdr>pacs.008.001.08</MsgDefIdr>
    // 			<BizSvc>IPS</BizSvc>
    // 			<CreDt>2017-09-13T18:18:00Z</CreDt>
    // 		</AppHdr>
    // 		<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08">
    // 			<FIToFICstmrCdtTrf>
    // 				<GrpHdr>
    // 					<MsgId>ZZZZZZZZAXXX999999999999999999999</MsgId>
    // 					<CreDtTm>2017-09-13T18:18:00</CreDtTm>
    // 					<NbOfTxs>1</NbOfTxs>
    // 					<SttlmInf>
    // 						<SttlmMtd>CLRG</SttlmMtd>
    // 					</SttlmInf>
    // 				</GrpHdr>
    // 				<CdtTrfTxInf>
    // 					<PmtId>
    // 						<EndToEndId>NOTPROVIDED</EndToEndId>
    // 						<TxId>ZZZZZZZZAXXX999999999999999999999</TxId>
    // 					</PmtId>
    // 					<PmtTpInf>
    // 						<ClrChanl>RTNS</ClrChanl>
    // 						<LclInstrm>
    // 							<Prtry>CSCT</Prtry>
    // 						</LclInstrm>
    // 						<CtgyPurp>
    // 							<Prtry>001</Prtry>
    // 						</CtgyPurp>
    // 					</PmtTpInf>
    // 					<IntrBkSttlmAmt Ccy="JOD">71.12</IntrBkSttlmAmt>
    // 					<IntrBkSttlmDt>2018-01-14</IntrBkSttlmDt>
    // 					<ChrgBr>SLEV</ChrgBr>
    // 					<InstgAgt>
    // 						<FinInstnId>
    // 							<BICFI>ZZZZZZZZ</BICFI>
    // 						</FinInstnId>
    // 					</InstgAgt>
    // 					<InstdAgt>
    // 						<FinInstnId>
    // 							<BICFI>UBSIJOA0</BICFI>
    // 						</FinInstnId>
    // 					</InstdAgt>
    // 					<Dbtr>
    // 						<Nm>John Johnson</Nm>
    // 					</Dbtr>
    // 					<DbtrAcct>
    // 						<Id>
    // 							<IBAN>JO22CITI00000000000555555555</IBAN>
    // 						</Id>
    // 					</DbtrAcct>
    // 					<DbtrAgt>
    // 						<FinInstnId>
    // 							<BICFI>ZZZZZZZZ</BICFI>
    // 							<Othr>
    // 								<Id>200004</Id>
    // 								<SchmeNm>
    // 									<Prtry>1700089999</Prtry>
    // 								</SchmeNm>
    // 							</Othr>
    // 						</FinInstnId>
    // 					</DbtrAgt>
    // 					<DbtrAgtAcct>
    // 						<Id>
    // 							<IBAN>JO66CITI22222222222222222222</IBAN>
    // 						</Id>
    // 					</DbtrAgtAcct>
    // 					<CdtrAgt>
    // 						<FinInstnId>
    // 							<BICFI>UBSIJOA0</BICFI>
    // 							<Othr>
    // 								<Id>210027</Id>
    // 								<SchmeNm>
    // 									<Prtry>1400199999</Prtry>
    // 								</SchmeNm>
    // 							</Othr>
    // 						</FinInstnId>
    // 					</CdtrAgt>
    // 					<CdtrAgtAcct>
    // 						<Id>
    // 							<IBAN>JO44UBSI33333333333333333333</IBAN>
    // 						</Id>
    // 					</CdtrAgtAcct>
    // 					<Cdtr>
    // 						<Nm>Omega Jones</Nm>
    // 					</Cdtr>
    // 					<CdtrAcct>
    // 						<Id>
    // 							<IBAN>JO95UBSI00000000000777777777</IBAN>
    // 						</Id>
    // 					</CdtrAcct>
    // 					<InstrForNxtAgt>
    // 						<InstrInf>/BNF/Details</InstrInf>
    // 					</InstrForNxtAgt>
    // 					<Purp>
    // 						<Prtry>5814</Prtry>
    // 					</Purp>
    // 					<RgltryRptg>
    // 						<Dtls>
    // 							<Inf>SOMEINFORMATIONABOUTPAYMENT-1</Inf>
    // 							<Inf>SOMEINFORMATIONABOUTPAYMENT-2</Inf>
    // 							<Inf>SOMEINFORMATIONABOUTPAYMENT-3</Inf>
    // 						</Dtls>
    // 					</RgltryRptg>
    // 					<Tax>
    // 						<Cdtr>
    // 							<TaxId>9900083901</TaxId>
    // 						</Cdtr>
    // 						<Dbtr>
    // 							<TaxId>1000387561</TaxId>
    // 						</Dbtr>
    // 					</Tax>
    // 					<RmtInf>
    // 						<Ustrd>EDV UCUN ODENIR</Ustrd>
    // 						<Ustrd>EXTRA INFO</Ustrd>
    // 					</RmtInf>
    // 				</CdtTrfTxInf>
    // 			</FIToFICstmrCdtTrf>
    // 		</Document>
    // 	</Body>
    // </DataPDU>

    let gen = CkoXmlDSigGen()!

    gen.sigLocation = "DataPDU|Body|AppHdr|Sgntr"
    gen.sigLocationMod = 0
    gen.sigNamespacePrefix = "ds"
    gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
    gen.signedInfoCanonAlg = "EXCL_C14N"
    gen.signedInfoDigestMethod = "sha256"

    // Set the KeyInfoId before adding references..
    gen.keyInfoId = "_f9f2c543-e50a-4a50-bd91-50155d27f7e2"

    // Create an Object to be added to the Signature.
    let object1 = CkoXml()!
    object1.tag = "xades:QualifyingProperties"
    object1.addAttribute("xmlns:xades", value: "http://uri.etsi.org/01903/v1.3.2#")
    object1.updateAttr(at: "xades:SignedProperties", autoCreate: true, attrName: "Id", attrValue: "_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops")
    object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", value: "TO BE GENERATED BY CHILKAT")

    gen.addObject("", content: object1.getXml(), mimeType: "", encoding: "")

    // -------- Reference 1 --------
    gen.addSameDocRef("_f9f2c543-e50a-4a50-bd91-50155d27f7e2", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "", refType: "")

    // -------- Reference 2 --------
    gen.addObjectRef("_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "", refType: "http://uri.etsi.org/01903/v1.3.2#SignedProperties")

    // -------- Reference 3 --------
    gen.addSameDocRef("", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "", refType: "")

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

    gen.setX509Cert(cert, usePrivateKey: true)

    gen.keyInfoType = "X509Data"
    gen.x509Type = "IssuerSerial"

    // Load XML to be signed...
    let sbXml = CkoStringBuilder()!
    xmlToSign.getSb(sbXml)

    // Can alternatively use "CompactSignedXml"
    gen.behaviors = "IndentedSignature,LocalSigningTime"

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

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

    // Save the signed XML to a file.
    success = sbXml.writeFile("qa_output/mx_signed.xml", charset: "utf-8", emitBom: false)

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

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

    // Important: The above signature did not include the full X.509 certificate.
    // You must call verifier.SetPublicKey to provide the public key of the certificate required for validation.

    let verifyCert = CkoCert()!
    success = verifyCert.load(fromFile: "qa_data/certs/cert_test123.cer")
    if success != true {
        print("\(verifyCert.lastErrorText!)")
        return
    }

    var pubKey: CkoPublicKey? = verifyCert.exportPublicKey()
    verifier.setPublicKey(pubKey)
    pubKey = nil

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

        verifyIdx = verifyIdx + 1
    }

    print("All signatures were successfully verified.")

}

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.