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) Sign Manifest File to Generate a Passbook .pkpass in Memory

Demonstrates how to create a Passbook .pkpass archive by creating a signature of a manifest file and then zipping to a .pkpass archive in memory

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 requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- ---------------------------------------------------------------------------------------------
    -- This example is the same as Sign Manifest File to Generate a Passbook .pkpass file
    -- except everything happens in memory (no input files, no output files)
    -- ---------------------------------------------------------------------------------------------

    -- First create the manifest.json
    DECLARE @success int

    DECLARE @manifest int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @manifest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

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

    EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'notUsedAndNeverCreated.zip'

    EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'sha1'
    -- Return hashes as lowercase hex.
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hexlower'

    DECLARE @digestStr nvarchar(4000)

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

    -- Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
    DECLARE @entry int
    EXEC sp_OAMethod @zip, 'AppendBd', @entry OUT, 'icon.png', @pngData
    EXEC @hr = sp_OADestroy @entry

    EXEC sp_OAMethod @crypt, 'HashBdENC', @digestStr OUT, @pngData
    EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"icon.png"', @digestStr

    EXEC sp_OAMethod @pngData, 'Clear', @success OUT
    -- Assume we load the pngData with bytes for "icon@2x.png" from somewhere...
    EXEC sp_OAMethod @zip, 'AppendBd', @entry OUT, 'icon@2x.png', @pngData
    EXEC @hr = sp_OADestroy @entry

    EXEC sp_OAMethod @crypt, 'HashBdENC', @digestStr OUT, @pngData
    EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"icon@2x.png"', @digestStr

    EXEC sp_OAMethod @pngData, 'Clear', @success OUT
    -- Assume we load the pngData with bytes for "logo.png" from somewhere...
    EXEC sp_OAMethod @zip, 'AppendBd', @entry OUT, 'logo.png', @pngData
    EXEC @hr = sp_OADestroy @entry

    EXEC sp_OAMethod @crypt, 'HashBdENC', @digestStr OUT, @pngData
    EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"logo.png"', @digestStr

    EXEC sp_OAMethod @pngData, 'Clear', @success OUT
    -- Assume we load the pngData with bytes for "logo@2x.png" from somewhere...
    EXEC sp_OAMethod @zip, 'AppendBd', @entry OUT, 'logo@2x.png', @pngData
    EXEC @hr = sp_OADestroy @entry

    EXEC sp_OAMethod @crypt, 'HashBdENC', @digestStr OUT, @pngData
    EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"logo@2x.png"', @digestStr

    DECLARE @passJson nvarchar(4000)
    SELECT @passJson = '{ .... }'    --  Contains the contents of pass.json
    EXEC sp_OAMethod @zip, 'AppendString', @entry OUT, 'pass.json', @passJson
    EXEC @hr = sp_OADestroy @entry

    EXEC sp_OAMethod @crypt, 'HashStringENC', @digestStr OUT, @passJson
    EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"pass.json"', @digestStr

    EXEC sp_OAMethod @manifest, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @zip, 'AppendString', @entry OUT, 'manifest.json', @sTmp0
    EXEC @hr = sp_OADestroy @entry

    -- Make sure we have the Apple WWDR intermediate certificate available for 
    -- the cert chain in the signature.
    DECLARE @certVault int
    -- Use "Chilkat_9_5_0.XmlCertVault" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.XmlCertVault', @certVault OUT

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

    EXEC sp_OAMethod @appleWwdrCert, 'LoadByCommonName', @success OUT, 'Apple Worldwide Developer Relations Certification Authority'
    IF @success <> 1
      BEGIN

        PRINT 'The Apple WWDR intermediate certificate is not installed.'

        PRINT 'It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer'

        PRINT 'You may alternatively load the .cer like this...'
        EXEC sp_OAMethod @appleWwdrCert, 'LoadFromFile', @success OUT, 'qa_data/certs/AppleWWDRCA.cer'
        IF @success <> 1
          BEGIN
            EXEC sp_OAGetProperty @appleWwdrCert, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @manifest
            EXEC @hr = sp_OADestroy @crypt
            EXEC @hr = sp_OADestroy @zip
            EXEC @hr = sp_OADestroy @pngData
            EXEC @hr = sp_OADestroy @certVault
            EXEC @hr = sp_OADestroy @appleWwdrCert
            RETURN
          END
      END
    EXEC sp_OAMethod @certVault, 'AddCert', @success OUT, @appleWwdrCert
    EXEC sp_OAMethod @crypt, 'UseCertVault', @success OUT, @certVault

    -- Use a digital certificate and private key from a PFX
    DECLARE @bdPfx int
    -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdPfx OUT

    -- Assume we loaded a PFX into bdPfx....
    DECLARE @pfxPassword nvarchar(4000)
    SELECT @pfxPassword = '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, 'LoadPfxBd', @success OUT, @bdPfx, @pfxPassword
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @manifest
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @pngData
        EXEC @hr = sp_OADestroy @certVault
        EXEC @hr = sp_OADestroy @appleWwdrCert
        EXEC @hr = sp_OADestroy @bdPfx
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- Provide the signing cert (with associated private key).
    EXEC sp_OAMethod @crypt, 'SetSigningCert', @success OUT, @cert
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @manifest
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @pngData
        EXEC @hr = sp_OADestroy @certVault
        EXEC @hr = sp_OADestroy @appleWwdrCert
        EXEC @hr = sp_OADestroy @bdPfx
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- Specify the signed attributes to be included.
    -- (These attributes appear to not be necessary, but we're including
    -- them just in case they become necessary in the future.)
    DECLARE @jsonSignedAttrs int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonSignedAttrs OUT

    EXEC sp_OAMethod @jsonSignedAttrs, 'UpdateInt', @success OUT, 'contentType', 1
    EXEC sp_OAMethod @jsonSignedAttrs, 'UpdateInt', @success OUT, 'signingTime', 1
    EXEC sp_OAMethod @jsonSignedAttrs, 'Emit', @sTmp0 OUT
    EXEC sp_OASetProperty @crypt, 'SigningAttributes', @sTmp0

    -- Sign the manifest JSON to produce a signature
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'
    DECLARE @sig nvarchar(4000)
    EXEC sp_OAMethod @manifest, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @crypt, 'SignStringENC', @sig OUT, @sTmp0
    DECLARE @bdSig int
    -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdSig OUT

    EXEC sp_OAMethod @bdSig, 'AppendEncoded', @success OUT, @sig, 'base64'
    EXEC sp_OAMethod @zip, 'AppendBd', @entry OUT, 'signature', @bdSig
    EXEC @hr = sp_OADestroy @entry

    -- ---------------------------------------------------------------------------------------------
    -- Note: Chilkat also has the capability to do everything in-memory (no files would be involved).  
    -- If this is of interest, please send email to support@chilkatsoft.com
    -- ---------------------------------------------------------------------------------------------

    -- Create the .pkipass archive (which is a .zip archive containing the required files).
    -- the .zip is written to bdZip
    DECLARE @bdZip int
    -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdZip OUT

    EXEC sp_OAMethod @zip, 'WriteBd', @success OUT, @bdZip
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @manifest
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @pngData
        EXEC @hr = sp_OADestroy @certVault
        EXEC @hr = sp_OADestroy @appleWwdrCert
        EXEC @hr = sp_OADestroy @bdPfx
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @jsonSignedAttrs
        EXEC @hr = sp_OADestroy @bdSig
        EXEC @hr = sp_OADestroy @bdZip
        RETURN
      END


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @manifest
    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @zip
    EXEC @hr = sp_OADestroy @pngData
    EXEC @hr = sp_OADestroy @certVault
    EXEC @hr = sp_OADestroy @appleWwdrCert
    EXEC @hr = sp_OADestroy @bdPfx
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @jsonSignedAttrs
    EXEC @hr = sp_OADestroy @bdSig
    EXEC @hr = sp_OADestroy @bdZip


END
GO

 

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