Sample code for 30+ languages & platforms
SQL Server

RSA Signature with Certificate's Private Key from PFX

See more RSA Examples

Demonstrates how to use a certificate's private key from a PFX file to create an RSA signature.

Chilkat SQL Server Downloads

SQL Server
-- 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)
    DECLARE @success int
    SELECT @success = 0

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

    -- Create an instance of a certificate store object, load a PFX file,
    -- locate the certificate we need, and use it for signing.
    -- (a PFX file may contain more than one certificate.)
    DECLARE @certStore int
    EXEC @hr = sp_OACreate 'Chilkat.CertStore', @certStore OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- The 1st argument is the filename, the 2nd arg is the 
    -- PFX file's password:
    EXEC sp_OAMethod @certStore, 'LoadPfxFile', @success OUT, 'chilkat.pfx', 'test'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @certStore, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @certStore
        RETURN
      END

    -- Find the certificate by the subject common name:
    DECLARE @jsonCN int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonCN OUT

    EXEC sp_OAMethod @jsonCN, 'UpdateString', @success OUT, 'CN', 'cert common name'

    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @certStore, 'FindCert', @success OUT, @jsonCN, @cert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @certStore, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @certStore
        EXEC @hr = sp_OADestroy @jsonCN
        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 @certStore
        EXEC @hr = sp_OADestroy @jsonCN
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    DECLARE @rsa int
    EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa OUT

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

    -- Encode the signature as a hex string
    EXEC sp_OASetProperty @rsa, 'EncodingMode', 'hex'

    DECLARE @strData nvarchar(4000)
    SELECT @strData = 'This is the string to be signed.'

    -- Sign the string using the sha-1 hash algorithm.
    -- Other valid choices are "sha-256", "md2" and "md5".
    DECLARE @hexSig nvarchar(4000)
    EXEC sp_OAMethod @rsa, 'SignStringENC', @hexSig OUT, @strData, 'sha-1'


    PRINT @hexSig


    PRINT 'Success!'

    EXEC @hr = sp_OADestroy @certStore
    EXEC @hr = sp_OADestroy @jsonCN
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @rsa


END
GO