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) Generate RSA Key and Sign a StringSee more RSA ExamplesDemonstrates how to generate a new RSA public/private key pair and use it to generate a signature for a string. The (binary) digital signature is returned as a hexidecimalized string.
-- 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) -- This example assumes the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @rsa int -- Use "Chilkat_9_5_0.Rsa" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END -- Generate a 2048-bit RSA key. DECLARE @success int EXEC sp_OAMethod @rsa, 'GenerateKey', @success OUT, 2048 -- Return the signature in hex. EXEC sp_OASetProperty @rsa, 'EncodingMode', 'hex' DECLARE @strData nvarchar(4000) SELECT @strData = 'This is the string to be signed.' -- Sign the SHA256 hash of the string. DECLARE @hexSig nvarchar(4000) EXEC sp_OAMethod @rsa, 'SignStringENC', @hexSig OUT, @strData, 'sha256' PRINT @hexSig EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 -- Now verify the signature: EXEC sp_OAMethod @rsa, 'VerifyStringENC', @success OUT, @strData, 'sha256', @hexSig IF @success = 1 BEGIN PRINT 'Signature verified!' END ELSE BEGIN EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END -- Try it with an invalid signature: EXEC sp_OAMethod @rsa, 'VerifyStringENC', @success OUT, @strData, 'sha256', 'not a valid sig' IF @success = 1 BEGIN PRINT 'Signature verified!' END ELSE BEGIN EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END -- Try it with invalid data: EXEC sp_OAMethod @rsa, 'VerifyStringENC', @success OUT, 'Not the original data', 'sha256', @hexSig IF @success = 1 BEGIN PRINT 'Signature verified!' END ELSE BEGIN EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END -- Try it with the wrong hash algorithm: EXEC sp_OAMethod @rsa, 'VerifyStringENC', @success OUT, @strData, 'sha-1', @hexSig IF @success = 1 BEGIN PRINT 'Signature verified!' END ELSE BEGIN EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END EXEC @hr = sp_OADestroy @rsa END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.