SQL Server
SQL Server
Load a Private Key from BinData (Any Format)
See more Private Key Examples
Demonstrates the Chilkat PrivateKey.LoadAnyFormat method, which loads a private key by inspecting the bytes in a BinData. The first argument is the BinData and the second is the password (used if the detected key is encrypted).
Note: The file path is relative to the application's current working directory. Absolute paths may also be used. Supply the path to your own key file.
Background: When you have key bytes in memory but do not know their exact format — downloaded, decrypted, or pulled from a database — this figures it out for you, recognizing PKCS #1/#8 DER, encrypted PKCS #8, PEM, JWK, Chilkat XML, Microsoft formats, and more. It removes the guesswork of choosing a specific loader, at the small cost of the detection step. Supply the password so it can decrypt an encrypted key without a second call.
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 @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the PrivateKey.LoadAnyFormat method, which loads a private key by inspecting the
-- bytes in a BinData. The 1st argument is the BinData and the 2nd is the password (used if the
-- detected key is encrypted).
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, 'qa_data/some_private_key'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @bd, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @bd
RETURN
END
DECLARE @privKey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT
-- The key password is not hard-coded in a real application; obtain it from an interactive
-- prompt, environment variable, or a secrets vault.
DECLARE @password nvarchar(4000)
SELECT @password = 'myKeyPassword'
-- Chilkat auto-detects the representation: PKCS #1/#8 DER, encrypted PKCS #8, PEM, JWK,
-- Chilkat XML, Microsoft formats, and more.
EXEC sp_OAMethod @privKey, 'LoadAnyFormat', @success OUT, @bd, @password
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @privKey
RETURN
END
EXEC sp_OAGetProperty @privKey, 'KeyType', @sTmp0 OUT
PRINT 'Loaded a ' + @sTmp0 + ' private key.'
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @privKey
END
GO