Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Extract PKCS7 Signature DigestDemonstrates how to extract a signature digest from a PKCS7 signature.
-- 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) -- This example requires the Chilkat Crypt API to have been previously unlocked. -- See Unlock Chilkat Crypt for sample code. DECLARE @crypt int -- Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @p7mFile nvarchar(4000) SELECT @p7mFile = 'qa_data/p7m/test.pdf.p7m' DECLARE @outPath nvarchar(4000) SELECT @outPath = 'qa_output/test.pdf' -- First let's see if this .p7m signature can be verified, and the original file extracted. DECLARE @verified int EXEC sp_OAMethod @crypt, 'VerifyP7M', @verified OUT, @p7mFile, @outPath IF @verified <> 1 BEGIN EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @crypt RETURN END -- How many signers? -- (The NumSignerCerts property is set whenever a signature is verified.) EXEC sp_OAGetProperty @crypt, 'NumSignerCerts', @iTmp0 OUT PRINT 'Num Signers: ' + @iTmp0 -- Load the .p7m into memory... DECLARE @pkcs7Data int -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.BinData', @pkcs7Data OUT DECLARE @success int EXEC sp_OAMethod @pkcs7Data, 'LoadFile', @success OUT, @p7mFile -- Check to see if this .p7m contains the binary bytes, or if it's -- already base64 encoded. Get the 1st two bytes. If the first two -- bytes are the us-ascii values "MI", then we have base64. DECLARE @sbBase64 int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbBase64 OUT DECLARE @hexStr nvarchar(4000) EXEC sp_OAMethod @pkcs7Data, 'GetEncodedChunk', @hexStr OUT, 0, 2, 'hex' EXEC sp_OAMethod @sbBase64, 'Append', @success OUT, @hexStr DECLARE @bHaveBase64 int SELECT @bHaveBase64 = 0 EXEC sp_OAMethod @sbBase64, 'ContentsEqual', @iTmp0 OUT, '4D49', 1 IF @iTmp0 = 1 BEGIN SELECT @bHaveBase64 = 1 EXEC sp_OAMethod @sbBase64, 'Clear', NULL EXEC sp_OAMethod @sbBase64, 'AppendBd', @success OUT, @pkcs7Data, 'utf-8', 0, 0 END -- Get each signer's signature digest. EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64' DECLARE @i int SELECT @i = 0 DECLARE @digest nvarchar(4000) EXEC sp_OAGetProperty @crypt, 'NumSignerCerts', @iTmp0 OUT WHILE @i < @iTmp0 BEGIN IF @bHaveBase64 BEGIN EXEC sp_OAMethod @sbBase64, 'GetAsString', @sTmp0 OUT EXEC sp_OAMethod @crypt, 'Pkcs7ExtractDigest', @digest OUT, @i, @sTmp0 END ELSE BEGIN EXEC sp_OAMethod @pkcs7Data, 'GetEncoded', @sTmp0 OUT, 'base64' EXEC sp_OAMethod @crypt, 'Pkcs7ExtractDigest', @digest OUT, @i, @sTmp0 END EXEC sp_OAGetProperty @crypt, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @pkcs7Data EXEC @hr = sp_OADestroy @sbBase64 RETURN END PRINT 'Signer ' + @i + 1 + ' digest = ' + @digest SELECT @i = @i + 1 END EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @pkcs7Data EXEC @hr = sp_OADestroy @sbBase64 END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.