Sample code for 30+ languages & platforms
SQL Server

JWS Using RSASSA-PSS using SHA-256 and MGF1 with SHA-256

See more JSON Web Signatures (JWS) Examples

Creates and validates a JSON Web Signature (JWS) using RSASSA-PSS using SHA-256 and MGF1 with SHA-256.

Chilkat supports all of the following JWS algorithms:

   +--------------+-------------------------------+--------------------+
   | "alg" Param  | Digital Signature or MAC      | Implementation     |
   | Value        | Algorithm                     | Requirements       |
   +--------------+-------------------------------+--------------------+
   | HS256        | HMAC using SHA-256            | Required           |
   | HS384        | HMAC using SHA-384            | Optional           |
   | HS512        | HMAC using SHA-512            | Optional           |
   | RS256        | RSASSA-PKCS1-v1_5 using       | Recommended        |
   |              | SHA-256                       |                    |
   | RS384        | RSASSA-PKCS1-v1_5 using       | Optional           |
   |              | SHA-384                       |                    |
   | RS512        | RSASSA-PKCS1-v1_5 using       | Optional           |
   |              | SHA-512                       |                    |
   | ES256        | ECDSA using P-256 and SHA-256 | Recommended+       |
   | ES384        | ECDSA using P-384 and SHA-384 | Optional           |
   | ES512        | ECDSA using P-521 and SHA-512 | Optional           |
   | PS256        | RSASSA-PSS using SHA-256 and  | Optional           |
   |              | MGF1 with SHA-256             |                    |
   | PS384        | RSASSA-PSS using SHA-384 and  | Optional           |
   |              | MGF1 with SHA-384             |                    |
   | PS512        | RSASSA-PSS using SHA-512 and  | Optional           |
   |              | MGF1 with SHA-512             |                    |
   +--------------+-------------------------------+--------------------+

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- Note: This example requires Chilkat v9.5.0.66 or greater.

    -- Use the following RSA key loaded from JWK format.
    DECLARE @sbJwk int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJwk OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '{"kty":"RSA",'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '"n":"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ",'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '"e":"AQAB",'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '"d":"Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ",'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '"p":"4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc",'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '"q":"uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '-njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc",'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '"dp":"BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0",'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '"dq":"h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU",'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '"qi":"IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, 'W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U"'
    EXEC sp_OAMethod @sbJwk, 'Append', @success OUT, '}'

    DECLARE @rsaKey int
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @rsaKey OUT

    -- Note: This example loads the RSA key from JWK format.  Any format can be loaded
    -- into the private key object. (See the online reference documentation..)
    EXEC sp_OAMethod @sbJwk, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @rsaKey, 'LoadJwk', @success OUT, @sTmp0
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rsaKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sbJwk
        EXEC @hr = sp_OADestroy @rsaKey
        RETURN
      END

    -- Create the JWS Protected Header
    DECLARE @jwsProtHdr int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwsProtHdr OUT

    EXEC sp_OAMethod @jwsProtHdr, 'AppendString', @success OUT, 'alg', 'PS256'

    DECLARE @jws int
    EXEC @hr = sp_OACreate 'Chilkat.Jws', @jws OUT

    -- Set the protected header:
    DECLARE @signatureIndex int
    SELECT @signatureIndex = 0
    EXEC sp_OAMethod @jws, 'SetProtectedHeader', @success OUT, @signatureIndex, @jwsProtHdr

    -- Set the RSA key:
    EXEC sp_OAMethod @jws, 'SetPrivateKey', @success OUT, @signatureIndex, @rsaKey

    -- Set the payload.
    DECLARE @bIncludeBom int
    SELECT @bIncludeBom = 0
    DECLARE @payloadStr nvarchar(4000)
    SELECT @payloadStr = 'In our village, folks say God crumbles up the old moon into stars.'
    EXEC sp_OAMethod @jws, 'SetPayload', @success OUT, @payloadStr, 'utf-8', @bIncludeBom

    -- Create the JWS
    -- By default, the compact serialization is used.
    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 @sbJwk
        EXEC @hr = sp_OADestroy @rsaKey
        EXEC @hr = sp_OADestroy @jwsProtHdr
        EXEC @hr = sp_OADestroy @jws
        RETURN
      END


    PRINT 'JWS: ' + @jwsCompact

    -- Note: RSA PSS padding uses random values, so the output will be different each time a signature is produced.
    -- sample output:
    -- JWS: eyJhbGciOiJQUzI1NiJ9.SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu.TRWhwRo5dMv9-8OzrInfJTwmUGYgjLfHk8lqF072ND-FmLWEBnUTOpY8oJXp8FdWw2SalbdOeNlrtlJjwk4XK8Ql2iJ_2qMCtxsvLPhKBOqFoAF4aBvTOEDVJDxf0DaBSiydEEtfTVV2iwBcjWabu5J2XieR5y7QZQtuHsn7T3qKBvCcCejN3Y2oqAT3qMHvu1fTms1r_91wBn_K7Wjd9UkZ1n02qQcUHJznR_OF2BgN7_KWIDAF9ZS9keoju2NPpPelO4yxa2XUPnehY3G7dHKoCxUEQR4d2Xc5voqDASTVCDqQS4PVOZdvT3Ein6-SanAlCwbWBbkvT8g6-5PImQ

    -- Now load the JWS, validate, and recover the original text.
    DECLARE @jws2 int
    EXEC @hr = sp_OACreate 'Chilkat.Jws', @jws2 OUT

    -- Load the JWS.
    EXEC sp_OAMethod @jws2, 'LoadJws', @success OUT, @jwsCompact

    DECLARE @rsaPubKey int
    EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @rsaPubKey OUT

    EXEC sp_OAMethod @rsaKey, 'ToPublicKey', @success OUT, @rsaPubKey

    -- Set the RSA public key used for validation.
    SELECT @signatureIndex = 0
    EXEC sp_OAMethod @jws2, 'SetPublicKey', @success OUT, @signatureIndex, @rsaPubKey

    -- Validate the 1st (and only) signature at index 0..
    DECLARE @v int
    EXEC sp_OAMethod @jws2, 'Validate', @v OUT, @signatureIndex
    IF @v < 0
      BEGIN
        -- Perhaps Chilkat was not unlocked or the trial expired..

        PRINT 'Method call failed for some other reason.'
        EXEC sp_OAGetProperty @jws2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sbJwk
        EXEC @hr = sp_OADestroy @rsaKey
        EXEC @hr = sp_OADestroy @jwsProtHdr
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @jws2
        EXEC @hr = sp_OADestroy @rsaPubKey
        RETURN
      END
    IF @v = 0
      BEGIN

        PRINT 'Invalid signature.  The RSA key was incorrect, the JWS was invalid, or both.'
        EXEC @hr = sp_OADestroy @sbJwk
        EXEC @hr = sp_OADestroy @rsaKey
        EXEC @hr = sp_OADestroy @jwsProtHdr
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @jws2
        EXEC @hr = sp_OADestroy @rsaPubKey
        RETURN
      END

    -- If we get here, the signature was validated..

    PRINT 'Signature validated.'

    -- Recover the original content:
    EXEC sp_OAMethod @jws2, 'GetPayload', @sTmp0 OUT, 'utf-8'
    PRINT @sTmp0

    -- Examine the protected header:
    DECLARE @joseHeader int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @joseHeader OUT

    EXEC sp_OAMethod @jws2, 'GetProtectedH', @success OUT, @signatureIndex, @joseHeader
    EXEC sp_OASetProperty @joseHeader, 'EmitCompact', 0


    PRINT 'Protected (JOSE) header:'
    EXEC sp_OAMethod @joseHeader, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:

    -- 	Signature validated.
    -- 	In our village, folks say God crumbles up the old moon into stars.
    -- 	Protected (JOSE) header:
    -- 	{ 
    -- 	  "alg": "PS256"
    -- 	}

    EXEC @hr = sp_OADestroy @sbJwk
    EXEC @hr = sp_OADestroy @rsaKey
    EXEC @hr = sp_OADestroy @jwsProtHdr
    EXEC @hr = sp_OADestroy @jws
    EXEC @hr = sp_OADestroy @jws2
    EXEC @hr = sp_OADestroy @rsaPubKey
    EXEC @hr = sp_OADestroy @joseHeader


END
GO