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
(Go) IPS MX Signature - Digitally Sign MX DocumentSee more XML Digital Signatures ExamplesDemonstrates 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.
// This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. success := true // First create the XML to be signed, or load it from a file, or a string, // To load XML from a file: xmlToSign := chilkat.NewXml() success = xmlToSign.LoadXmlFile("c:/someDir/mx_document.xml") // Or to load XML from a string success = xmlToSign.LoadXml("...") // Or create the XML directly. xmlToSign.Clear() // Use this online tool to generate code from sample XML: // Generate Code to Create XML xmlToSign.SetTag("DataPDU") xmlToSign.AddAttribute("xmlns","urn:cma:stp:xsd:stp.1.0") xmlToSign.UpdateAttrAt("Body|AppHdr",true,"xmlns","urn:iso:std:iso:20022:tech:xsd:head.001.001.01") xmlToSign.UpdateChildContent("Body|AppHdr|Fr|FIId|FinInstnId|BICFI","ZZZZZZZZ") xmlToSign.UpdateChildContent("Body|AppHdr|To|FIId|FinInstnId|BICFI","YYYYYYYYYY") xmlToSign.UpdateChildContent("Body|AppHdr|BizMsgIdr","ZZZZZZZZAXXX999999999999999999999") xmlToSign.UpdateChildContent("Body|AppHdr|MsgDefIdr","pacs.008.001.08") xmlToSign.UpdateChildContent("Body|AppHdr|BizSvc","IPS") xmlToSign.UpdateChildContent("Body|AppHdr|CreDt","2017-09-13T18:18:00Z") xmlToSign.UpdateAttrAt("Body|Document",true,"xmlns","urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|MsgId","ZZZZZZZZAXXX999999999999999999999") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|CreDtTm","2017-09-13T18:18:00") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|NbOfTxs","1") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|GrpHdr|SttlmInf|SttlmMtd","CLRG") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|EndToEndId","NOTPROVIDED") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|TxId","ZZZZZZZZAXXX999999999999999999999") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|ClrChanl","RTNS") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|LclInstrm|Prtry","CSCT") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|CtgyPurp|Prtry","001") xmlToSign.UpdateAttrAt("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt",true,"Ccy","JOD") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt","71.12") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmDt","2018-01-14") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|ChrgBr","SLEV") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstgAgt|FinInstnId|BICFI","ZZZZZZZZ") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstdAgt|FinInstnId|BICFI","UBSIJOA0") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Dbtr|Nm","John Johnson") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAcct|Id|IBAN","JO22CITI00000000000555555555") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|BICFI","ZZZZZZZZ") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|Id","200004") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|SchmeNm|Prtry","1700099999") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgtAcct|Id|IBAN","JO66CITI22222222222222222222") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|BICFI","UBSIJOA0") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|Id","210027") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|SchmeNm|Prtry","1400199999") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgtAcct|Id|IBAN","JO44UBSI33333333333333333333") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Cdtr|Nm","Omega Jones") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAcct|Id|IBAN","JO95UBSI00000000000777777777") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstrForNxtAgt|InstrInf","/BNF/Details") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Purp|Prtry","5814") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf","SOMEINFORMATIONABOUTPAYMENT-1") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[1]","SOMEINFORMATIONABOUTPAYMENT-2") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[2]","SOMEINFORMATIONABOUTPAYMENT-3") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Cdtr|TaxId","9900083901") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Dbtr|TaxId","1000387561") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RmtInf|Ustrd","EDV UCUN ODENIR") xmlToSign.UpdateChildContent("Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RmtInf|Ustrd[1]","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> gen := chilkat.NewXmlDSigGen() gen.SetSigLocation("DataPDU|Body|AppHdr|Sgntr") gen.SetSigLocationMod(0) gen.SetSigNamespacePrefix("ds") gen.SetSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#") gen.SetSignedInfoCanonAlg("EXCL_C14N") gen.SetSignedInfoDigestMethod("sha256") // Set the KeyInfoId before adding references.. gen.SetKeyInfoId("_f9f2c543-e50a-4a50-bd91-50155d27f7e2") // Create an Object to be added to the Signature. object1 := chilkat.NewXml() object1.SetTag("xades:QualifyingProperties") object1.AddAttribute("xmlns:xades","http://uri.etsi.org/01903/v1.3.2#") object1.UpdateAttrAt("xades:SignedProperties",true,"Id","_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops") object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT") gen.AddObject("",*object1.GetXml(),"","") // -------- Reference 1 -------- gen.AddSameDocRef("_f9f2c543-e50a-4a50-bd91-50155d27f7e2","sha256","EXCL_C14N","","") // -------- Reference 2 -------- gen.AddObjectRef("_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops","sha256","EXCL_C14N","","http://uri.etsi.org/01903/v1.3.2#SignedProperties") // -------- Reference 3 -------- gen.AddSameDocRef("","sha256","EXCL_C14N","","") // Provide a certificate + private key. (PFX password is test123) cert := chilkat.NewCert() success = cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123") if success != true { fmt.Println(cert.LastErrorText()) xmlToSign.DisposeXml() gen.DisposeXmlDSigGen() object1.DisposeXml() cert.DisposeCert() return } gen.SetX509Cert(cert,true) gen.SetKeyInfoType("X509Data") gen.SetX509Type("IssuerSerial") // Load XML to be signed... sbXml := chilkat.NewStringBuilder() xmlToSign.GetXmlSb(sbXml) // Can alternatively use "CompactSignedXml" gen.SetBehaviors("IndentedSignature,LocalSigningTime") // Sign the XML... success = gen.CreateXmlDSigSb(sbXml) if success != true { fmt.Println(gen.LastErrorText()) xmlToSign.DisposeXml() gen.DisposeXmlDSigGen() object1.DisposeXml() cert.DisposeCert() sbXml.DisposeStringBuilder() return } // ----------------------------------------------- // Save the signed XML to a file. success = sbXml.WriteFile("qa_output/mx_signed.xml","utf-8",false) fmt.Println(*sbXml.GetAsString()) // ---------------------------------------- // Verify the signatures we just produced... verifier := chilkat.NewXmlDSig() success = verifier.LoadSignatureSb(sbXml) if success != true { fmt.Println(verifier.LastErrorText()) xmlToSign.DisposeXml() gen.DisposeXmlDSigGen() object1.DisposeXml() cert.DisposeCert() sbXml.DisposeStringBuilder() verifier.DisposeXmlDSig() 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. verifyCert := chilkat.NewCert() success = verifyCert.LoadFromFile("qa_data/certs/cert_test123.cer") if success != true { fmt.Println(verifyCert.LastErrorText()) xmlToSign.DisposeXml() gen.DisposeXmlDSigGen() object1.DisposeXml() cert.DisposeCert() sbXml.DisposeStringBuilder() verifier.DisposeXmlDSig() verifyCert.DisposeCert() return } pubKey := verifyCert.ExportPublicKey() verifier.SetPublicKey(pubKey) pubKey.DisposePublicKey() numSigs := verifier.NumSignatures() verifyIdx := 0 for verifyIdx < numSigs { verifier.SetSelector(verifyIdx) verified := verifier.VerifySignature(true) if verified != true { fmt.Println(verifier.LastErrorText()) xmlToSign.DisposeXml() gen.DisposeXmlDSigGen() object1.DisposeXml() cert.DisposeCert() sbXml.DisposeStringBuilder() verifier.DisposeXmlDSig() verifyCert.DisposeCert() return } verifyIdx = verifyIdx + 1 } fmt.Println("All signatures were successfully verified.") xmlToSign.DisposeXml() gen.DisposeXmlDSigGen() object1.DisposeXml() cert.DisposeCert() sbXml.DisposeStringBuilder() verifier.DisposeXmlDSig() verifyCert.DisposeCert() |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.