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

SQL Server 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
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

 

 

 

(SQL Server) 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 ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @success int
    SELECT @success = 1

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

    -- To load XML from a file:
    DECLARE @xmlToSign int
    -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xmlToSign OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @xmlToSign, 'LoadXmlFile', @success OUT, 'c:/someDir/mx_document.xml'

    -- Or to load XML from a string
    EXEC sp_OAMethod @xmlToSign, 'LoadXml', @success OUT, '...'

    -- Or create the XML directly.
    EXEC sp_OAMethod @xmlToSign, 'Clear', NULL

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

    EXEC sp_OASetProperty @xmlToSign, 'Tag', 'DataPDU'
    EXEC sp_OAMethod @xmlToSign, 'AddAttribute', @success OUT, 'xmlns', 'urn:cma:stp:xsd:stp.1.0'
    EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'Body|AppHdr', 1, 'xmlns', 'urn:iso:std:iso:20022:tech:xsd:head.001.001.01'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|AppHdr|Fr|FIId|FinInstnId|BICFI', 'ZZZZZZZZ'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|AppHdr|To|FIId|FinInstnId|BICFI', 'YYYYYYYYYY'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|AppHdr|BizMsgIdr', 'ZZZZZZZZAXXX999999999999999999999'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|AppHdr|MsgDefIdr', 'pacs.008.001.08'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|AppHdr|BizSvc', 'IPS'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|AppHdr|CreDt', '2017-09-13T18:18:00Z'
    EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'Body|Document', 1, 'xmlns', 'urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|GrpHdr|MsgId', 'ZZZZZZZZAXXX999999999999999999999'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|GrpHdr|CreDtTm', '2017-09-13T18:18:00'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|GrpHdr|NbOfTxs', '1'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|GrpHdr|SttlmInf|SttlmMtd', 'CLRG'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|EndToEndId', 'NOTPROVIDED'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtId|TxId', 'ZZZZZZZZAXXX999999999999999999999'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|ClrChanl', 'RTNS'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|LclInstrm|Prtry', 'CSCT'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|PmtTpInf|CtgyPurp|Prtry', '001'
    EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt', 1, 'Ccy', 'JOD'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmAmt', '71.12'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|IntrBkSttlmDt', '2018-01-14'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|ChrgBr', 'SLEV'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstgAgt|FinInstnId|BICFI', 'ZZZZZZZZ'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstdAgt|FinInstnId|BICFI', 'UBSIJOA0'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Dbtr|Nm', 'John Johnson'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAcct|Id|IBAN', 'JO22CITI00000000000555555555'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|BICFI', 'ZZZZZZZZ'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|Id', '200004'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgt|FinInstnId|Othr|SchmeNm|Prtry', '1700099999'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|DbtrAgtAcct|Id|IBAN', 'JO66CITI22222222222222222222'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|BICFI', 'UBSIJOA0'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|Id', '210027'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgt|FinInstnId|Othr|SchmeNm|Prtry', '1400199999'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAgtAcct|Id|IBAN', 'JO44UBSI33333333333333333333'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Cdtr|Nm', 'Omega Jones'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|CdtrAcct|Id|IBAN', 'JO95UBSI00000000000777777777'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|InstrForNxtAgt|InstrInf', '/BNF/Details'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Purp|Prtry', '5814'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf', 'SOMEINFORMATIONABOUTPAYMENT-1'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[1]', 'SOMEINFORMATIONABOUTPAYMENT-2'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RgltryRptg|Dtls|Inf[2]', 'SOMEINFORMATIONABOUTPAYMENT-3'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Cdtr|TaxId', '9900083901'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|Tax|Dbtr|TaxId', '1000387561'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'Body|Document|FIToFICstmrCdtTrf|CdtTrfTxInf|RmtInf|Ustrd', 'EDV UCUN ODENIR'
    EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, '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>

    DECLARE @gen int
    -- Use "Chilkat_9_5_0.XmlDSigGen" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.XmlDSigGen', @gen OUT

    EXEC sp_OASetProperty @gen, 'SigLocation', 'DataPDU|Body|AppHdr|Sgntr'
    EXEC sp_OASetProperty @gen, 'SigLocationMod', 0
    EXEC sp_OASetProperty @gen, 'SigNamespacePrefix', 'ds'
    EXEC sp_OASetProperty @gen, 'SigNamespaceUri', 'http://www.w3.org/2000/09/xmldsig#'
    EXEC sp_OASetProperty @gen, 'SignedInfoCanonAlg', 'EXCL_C14N'
    EXEC sp_OASetProperty @gen, 'SignedInfoDigestMethod', 'sha256'

    -- Set the KeyInfoId before adding references..
    EXEC sp_OASetProperty @gen, 'KeyInfoId', '_f9f2c543-e50a-4a50-bd91-50155d27f7e2'

    -- Create an Object to be added to the Signature.
    DECLARE @object1 int
    -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @object1 OUT

    EXEC sp_OASetProperty @object1, 'Tag', 'xades:QualifyingProperties'
    EXEC sp_OAMethod @object1, 'AddAttribute', @success OUT, 'xmlns:xades', 'http://uri.etsi.org/01903/v1.3.2#'
    EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties', 1, 'Id', '_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops'
    EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime', 'TO BE GENERATED BY CHILKAT'

    EXEC sp_OAMethod @object1, 'GetXml', @sTmp0 OUT
    EXEC sp_OAMethod @gen, 'AddObject', @success OUT, '', @sTmp0, '', ''

    -- -------- Reference 1 --------
    EXEC sp_OAMethod @gen, 'AddSameDocRef', @success OUT, '_f9f2c543-e50a-4a50-bd91-50155d27f7e2', 'sha256', 'EXCL_C14N', '', ''

    -- -------- Reference 2 --------
    EXEC sp_OAMethod @gen, 'AddObjectRef', @success OUT, '_4ed8e0ed-f47c-4262-909b-0458532ce7aa-signedprops', 'sha256', 'EXCL_C14N', '', 'http://uri.etsi.org/01903/v1.3.2#SignedProperties'

    -- -------- Reference 3 --------
    EXEC sp_OAMethod @gen, 'AddSameDocRef', @success OUT, '', 'sha256', 'EXCL_C14N', '', ''

    -- Provide a certificate + private key. (PFX password is test123)
    DECLARE @cert int
    -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/pfx/cert_test123.pfx', 'test123'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmlToSign
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @object1
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END
    EXEC sp_OAMethod @gen, 'SetX509Cert', @success OUT, @cert, 1

    EXEC sp_OASetProperty @gen, 'KeyInfoType', 'X509Data'
    EXEC sp_OASetProperty @gen, 'X509Type', 'IssuerSerial'

    -- Load XML to be signed...
    DECLARE @sbXml int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbXml OUT

    EXEC sp_OAMethod @xmlToSign, 'GetXmlSb', @success OUT, @sbXml

    -- Can alternatively use "CompactSignedXml"
    EXEC sp_OASetProperty @gen, 'Behaviors', 'IndentedSignature,LocalSigningTime'

    -- Sign the XML...
    EXEC sp_OAMethod @gen, 'CreateXmlDSigSb', @success OUT, @sbXml
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @gen, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmlToSign
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @object1
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @sbXml
        RETURN
      END
    -- -----------------------------------------------

    -- Save the signed XML to a file.
    EXEC sp_OAMethod @sbXml, 'WriteFile', @success OUT, 'qa_output/mx_signed.xml', 'utf-8', 0

    EXEC sp_OAMethod @sbXml, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- ----------------------------------------
    -- Verify the signatures we just produced...
    DECLARE @verifier int
    -- Use "Chilkat_9_5_0.XmlDSig" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.XmlDSig', @verifier OUT

    EXEC sp_OAMethod @verifier, 'LoadSignatureSb', @success OUT, @sbXml
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @verifier, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmlToSign
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @object1
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @verifier
        RETURN
      END

    -- 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.

    DECLARE @verifyCert int
    -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @verifyCert OUT

    EXEC sp_OAMethod @verifyCert, 'LoadFromFile', @success OUT, 'qa_data/certs/cert_test123.cer'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @verifyCert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmlToSign
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @object1
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @verifier
        EXEC @hr = sp_OADestroy @verifyCert
        RETURN
      END
    DECLARE @pubKey int
    EXEC sp_OAMethod @verifyCert, 'ExportPublicKey', @pubKey OUT
    EXEC sp_OAMethod @verifier, 'SetPublicKey', @success OUT, @pubKey
    EXEC @hr = sp_OADestroy @pubKey

    DECLARE @numSigs int
    EXEC sp_OAGetProperty @verifier, 'NumSignatures', @numSigs OUT
    DECLARE @verifyIdx int
    SELECT @verifyIdx = 0
    WHILE @verifyIdx < @numSigs
      BEGIN
        EXEC sp_OASetProperty @verifier, 'Selector', @verifyIdx
        DECLARE @verified int
        EXEC sp_OAMethod @verifier, 'VerifySignature', @verified OUT, 1
        IF @verified <> 1
          BEGIN
            EXEC sp_OAGetProperty @verifier, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @xmlToSign
            EXEC @hr = sp_OADestroy @gen
            EXEC @hr = sp_OADestroy @object1
            EXEC @hr = sp_OADestroy @cert
            EXEC @hr = sp_OADestroy @sbXml
            EXEC @hr = sp_OADestroy @verifier
            EXEC @hr = sp_OADestroy @verifyCert
            RETURN
          END
        SELECT @verifyIdx = @verifyIdx + 1
      END

    PRINT 'All signatures were successfully verified.'

    EXEC @hr = sp_OADestroy @xmlToSign
    EXEC @hr = sp_OADestroy @gen
    EXEC @hr = sp_OADestroy @object1
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @sbXml
    EXEC @hr = sp_OADestroy @verifier
    EXEC @hr = sp_OADestroy @verifyCert


END
GO

 

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