Sample code for 30+ languages & platforms
SQL Server

Load a Private Key from a JWK

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadJwk method, which loads a private key from JWK (JSON Web Key) JSON. The only argument is the JWK JSON string. Supported key types are RSA, EC, and Ed25519 (kty=OKP).

Background: JWK is the JSON key format used throughout modern web security — JWT signing, OAuth, OpenID Connect — so this loader is what you use to consume a key from a JWK Set or an identity provider. A private JWK includes the private parameters (d and, for RSA, the CRT values); a public-only JWK omits them. As JSON in the clear, a private JWK must be handled as sensitive material.

Chilkat SQL Server Downloads

SQL Server
-- 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.LoadJwk method, which loads a private key from JWK (JSON Web Key)
    --  JSON.  The only argument is the JWK JSON string.  Supported key types are RSA, EC, and
    --  Ed25519 (kty=OKP).

    DECLARE @privKey int
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  A private JWK.  For a real key, "d" (and the other private parameters) would be present.
    DECLARE @jwk nvarchar(4000)
    SELECT @jwk = '{"kty":"EC","crv":"P-256","x":"...","y":"...","d":"..."}'

    EXEC sp_OAMethod @privKey, 'LoadJwk', @success OUT, @jwk
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END


    EXEC sp_OAGetProperty @privKey, 'KeyType', @sTmp0 OUT

    PRINT 'Loaded a ' + @sTmp0 + ' private key.'

    EXEC @hr = sp_OADestroy @privKey


END
GO