Sample code for 30+ languages & platforms
SQL Server

RSASSA-PSS Algorithm with SHA256 Hashing

See more RSA Examples

RSA encrypt a SHA256 hash with OAEP padding.

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
    DECLARE @iTmp0 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.

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

    -- Load the private key object from a PEM file.
    -- (To load from a PEM string, call LoadPem instead.)
    EXEC sp_OAMethod @privkey, 'LoadPemFile', @success OUT, 'somePath/myPrivateKey.pem'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @privkey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privkey
        RETURN
      END

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

    -- Use RSA-PSS by setting PkcsPadding = 0
    EXEC sp_OASetProperty @rsa, 'PkcsPadding', 0
    -- Use SHA256
    EXEC sp_OASetProperty @rsa, 'OaepHash', 'SHA-256'

    EXEC sp_OAMethod @rsa, 'UsePrivateKey', @success OUT, @privkey

    -- Generate a base64 signature.
    EXEC sp_OASetProperty @rsa, 'EncodingMode', 'base64'

    DECLARE @sigStr nvarchar(4000)
    EXEC sp_OAMethod @rsa, 'SignStringENC', @sigStr OUT, 'String to be signed', 'SHA-256'
    EXEC sp_OAGetProperty @rsa, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privkey
        EXEC @hr = sp_OADestroy @rsa
        RETURN
      END


    PRINT 'Signature: ' + @sigStr

    EXEC @hr = sp_OADestroy @privkey
    EXEC @hr = sp_OADestroy @rsa


END
GO