Sample code for 30+ languages & platforms
SQL Server

Sign a JWS with a Certificate

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetSigningCert, which sets the certificate whose associated private key is used to create a signature.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The certificate must have an accessible private key. This is convenient when the signing identity is held as a certificate (for example loaded from a PFX).

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 @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    --  Protected header specifying RSASSA-PKCS1-v1_5 with SHA-256 (RS256).
    DECLARE @header int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @header OUT

    EXEC sp_OAMethod @header, 'UpdateString', @success OUT, 'alg', 'RS256'
    EXEC sp_OAMethod @jws, 'SetProtectedHeader', @success OUT, 0, @header
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @header
        RETURN
      END

    DECLARE @bIncludeBom int
    SELECT @bIncludeBom = 0
    EXEC sp_OAMethod @jws, 'SetPayload', @success OUT, 'This is the content to sign.', 'utf-8', @bIncludeBom
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @header
        RETURN
      END

    --  The file path is relative to the application's current working directory.  An absolute path
    --  may also be used.  Supply the path appropriate to your own environment.
    --  The PFX password should come from a secure source rather than being hard-coded.
    DECLARE @pfxPassword nvarchar(4000)
    SELECT @pfxPassword = 'myPfxPassword'

    --  Load a certificate that has an associated private key, then set it as the signing certificate for
    --  signature 0.  The certificate's private key is used to create the signature.
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/signer.pfx', @pfxPassword
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @header
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END
    EXEC sp_OAMethod @jws, 'SetSigningCert', @success OUT, 0, @cert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @header
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    DECLARE @jwsCompact nvarchar(4000)
    EXEC sp_OAMethod @jws, 'CreateJws', @jwsCompact OUT
    EXEC sp_OAGetProperty @jws, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @header
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    PRINT @jwsCompact

    EXEC @hr = sp_OADestroy @jws
    EXEC @hr = sp_OADestroy @header
    EXEC @hr = sp_OADestroy @cert


END
GO