Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

SQL Server Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(SQL Server) Extract PKCS7 Signature Digest

Demonstrates how to extract a signature digest from a PKCS7 signature.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    --  This example requires the Chilkat Crypt API to have been previously unlocked.
    --  See Unlock Chilkat Crypt for sample code.

    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Crypt2', @crypt OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @p7mFile nvarchar(4000)
    SELECT @p7mFile = 'qa_data/p7m/test.pdf.p7m'
    DECLARE @outPath nvarchar(4000)
    SELECT @outPath = 'qa_output/test.pdf'

    --  First let's see if this .p7m signature can be verified, and the original file extracted.
    DECLARE @verified int
    EXEC sp_OAMethod @crypt, 'VerifyP7M', @verified OUT, @p7mFile, @outPath
    IF @verified <> 1
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @crypt
        RETURN
      END

    --  How many signers?
    --  (The NumSignerCerts property is set whenever a signature is verified.)

    EXEC sp_OAGetProperty @crypt, 'NumSignerCerts', @iTmp0 OUT
    PRINT 'Num Signers: ' + @iTmp0

    --  Load the .p7m into memory...
    DECLARE @pkcs7Data int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.BinData', @pkcs7Data OUT

    EXEC sp_OAMethod @pkcs7Data, 'LoadFile', @success OUT, @p7mFile

    --  Check to see if this .p7m contains the binary bytes, or if it's
    --  already base64 encoded.  Get the 1st two bytes.  If the first two
    --  bytes are the us-ascii values "MI", then we have base64.
    DECLARE @sbBase64 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbBase64 OUT

    DECLARE @hexStr nvarchar(4000)
    EXEC sp_OAMethod @pkcs7Data, 'GetEncodedChunk', @hexStr OUT, 0, 2, 'hex'
    EXEC sp_OAMethod @sbBase64, 'Append', @success OUT, @hexStr

    DECLARE @bHaveBase64 int
    SELECT @bHaveBase64 = 0
    EXEC sp_OAMethod @sbBase64, 'ContentsEqual', @iTmp0 OUT, '4D49', 1
    IF @iTmp0 = 1
      BEGIN
        SELECT @bHaveBase64 = 1
        EXEC sp_OAMethod @sbBase64, 'Clear', NULL
        EXEC sp_OAMethod @sbBase64, 'AppendBd', @success OUT, @pkcs7Data, 'utf-8', 0, 0
      END

    --  Get each signer's signature digest.
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @digest nvarchar(4000)

    EXEC sp_OAGetProperty @crypt, 'NumSignerCerts', @iTmp0 OUT
    WHILE @i < @iTmp0
      BEGIN
        IF @bHaveBase64
          BEGIN
            EXEC sp_OAMethod @sbBase64, 'GetAsString', @sTmp0 OUT
            EXEC sp_OAMethod @crypt, 'Pkcs7ExtractDigest', @digest OUT, @i, @sTmp0
          END
        ELSE
          BEGIN
            EXEC sp_OAMethod @pkcs7Data, 'GetEncoded', @sTmp0 OUT, 'base64'
            EXEC sp_OAMethod @crypt, 'Pkcs7ExtractDigest', @digest OUT, @i, @sTmp0
          END
        EXEC sp_OAGetProperty @crypt, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 <> 1
          BEGIN
            EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @crypt
            EXEC @hr = sp_OADestroy @pkcs7Data
            EXEC @hr = sp_OADestroy @sbBase64
            RETURN
          END


        PRINT 'Signer ' + @i + 1 + ' digest = ' + @digest
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @pkcs7Data
    EXEC @hr = sp_OADestroy @sbBase64


END
GO

 

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