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

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    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
    -- Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.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
    -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @pkcs7Data OUT

    DECLARE @success int
    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
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.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-2024 Chilkat Software, Inc. All Rights Reserved.