SQL Server
SQL Server
RSA Signature using Private Key from .snk File
See more RSA Examples
A .snk file (Strong Name Key file) is a file format used in the .NET ecosystem to store an RSA public/private key pair. This key pair is typically used for strong-naming assemblies, which is a process of signing .NET assemblies to ensure their integrity and to uniquely identify them.This example loads a private key from a .snk file and creates an RSA 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
DECLARE @rsa int
EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Load the .snk and return the private key as XML.
DECLARE @xmlStr nvarchar(4000)
EXEC sp_OAMethod @rsa, 'SnkToXml', @xmlStr OUT, './test.snk'
EXEC sp_OAGetProperty @rsa, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rsa
RETURN
END
DECLARE @privKey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT
EXEC sp_OAMethod @privKey, 'LoadXml', @success OUT, @xmlStr
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @privKey
RETURN
END
EXEC sp_OAMethod @rsa, 'UsePrivateKey', @success OUT, @privKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @privKey
RETURN
END
-- Sign the SHA-256 hash of the utf-8 byte representation of the contents of sb
-- Return the signature in base64 format.
EXEC sp_OASetProperty @rsa, 'EncodingMode', 'base64'
EXEC sp_OASetProperty @rsa, 'Charset', 'utf-8'
DECLARE @sigBase64 nvarchar(4000)
EXEC sp_OAMethod @rsa, 'SignStringENC', @sigBase64 OUT, 'This is the text to be hashed and signed.', 'sha256'
EXEC sp_OAGetProperty @rsa, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @privKey
RETURN
END
PRINT 'RSA signature as base64: ' + @sigBase64
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @privKey
END
GO