Sample code for 30+ languages & platforms
SQL Server

Sign a Byte Array to Create an Opaque Signature in a Byte Array

See more Digital Signatures Examples

Signs data contained in a byte array to produce an opaque signature (also in a byte array). An opaque signature is a PKCS7 signature (also known as CAdES) that embeds the signed data. Also shows how to verify the signature and extract the original data.

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 @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/pfx/cert_test123.pfx', 'test123'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    DECLARE @fac int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT

    EXEC sp_OAMethod @fac, 'ReadEntireFile', @fileBytes OUT, 'qa_data/pdf/sample.pdf'
    EXEC sp_OAGetProperty @fac, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @fac, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @fac
        RETURN
      END

    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT

    EXEC sp_OAMethod @crypt, 'SetSigningCert', @success OUT, @cert

    -- We can sign any type of file.

    EXEC sp_OAMethod @crypt, 'OpaqueSignBytes', @sigBytes OUT, @fileBytes
    EXEC sp_OAGetProperty @crypt, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @crypt
        RETURN
      END

    EXEC sp_OAMethod @fac, 'WriteEntireFile', @success OUT, 'qa_output/sample.pdf.p7m', @sigBytes
    EXEC sp_OAGetProperty @fac, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @fac, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @crypt
        RETURN
      END

    -- We can verify the opaque signature and extract the original data like this

    EXEC sp_OAMethod @crypt, 'OpaqueVerifyBytes', @originalData OUT, @sigBytes
    EXEC sp_OAGetProperty @crypt, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @crypt
        RETURN
      END

    EXEC sp_OAMethod @fac, 'WriteEntireFile', @success OUT, 'qa_output/sample.pdf', @originalData
    EXEC sp_OAGetProperty @fac, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @fac, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @crypt
        RETURN
      END


    PRINT 'Signature is verified and the original data was extracted.'

    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @fac
    EXEC @hr = sp_OADestroy @crypt


END
GO