Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

DKIM Signature using Windows Current User Certificate Store

See more DKIM / DomainKey Examples

This is a Windows-specific example to load a certificate from the Current User (registry-based) certificate store, and then use the certificate's associated private key for a DKIM signature.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  This example requires the Chilkat API to have been previously unlocked.
    --  See Global Unlock Sample for sample code.

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

    --  The LoadByCommonName method searches the Windows Local Machine and Current User 
    --  registry-based certificate stores for a certificate having the common name specified. 
    --  If found, the certificate is loaded and ready for use.
    EXEC sp_OAMethod @cert, 'LoadByCommonName', @success OUT, 'My Certificate ABC'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    --  The certificate must have an associated private key installed, and it must be a
    --  private key that has been marked "exportable" when it was originally installed.
    EXEC sp_OAMethod @cert, 'HasPrivateKey', @iTmp0 OUT
    IF Not @iTmp0
      BEGIN

        PRINT 'This certificate does not have a private key available.'
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    DECLARE @privKey int
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT

    EXEC sp_OAMethod @cert, 'GetPrivateKey', @success OUT, @privKey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    DECLARE @dkim int
    EXEC @hr = sp_OACreate 'Chilkat.Dkim', @dkim OUT

    --  Load the private key into the DKIM object:
    EXEC sp_OAMethod @dkim, 'SetDkimPrivateKey', @success OUT, @privKey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @dkim, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @dkim
        RETURN
      END

    --  The private key has been loaded into the DKIM object.  See the other DKIM
    --  examples for guidance on how to create a DKIM signature...

    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @dkim


END
GO