SQL Server
SQL Server
Sign a Byte Array to Create a Detached Signature in a Byte Array
See more Digital Signatures Examples
Signs data contained in a byte array to produce a detached signature (also in a byte array). Also shows how to verify the signature.Chilkat SQL Server Downloads
-- 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.
-- The result is a detached signature (a signature that does not contain the data being signed).
EXEC sp_OAMethod @crypt, 'SignBytes', @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.p7s', @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 detached signature like this
DECLARE @verified int
EXEC sp_OAMethod @crypt, 'VerifyBytes', @verified OUT, @fileBytes, @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
PRINT 'Verified = ' + @verified
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @crypt
END
GO