Sample code for 30+ languages & platforms
SQL Server

Validate a JWS with a Public Key

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetPublicKey, which sets the public key used to validate a signature of a loaded JWS.

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 public key verifies a signature created with the corresponding private key. Validate returns 1 when valid, 0 when not, or a negative value on error.

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

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

    --  Load the JWS compact serialization.
    DECLARE @jwsCompact nvarchar(4000)
    SELECT @jwsCompact = 'eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>'
    EXEC sp_OAMethod @jws, 'LoadJws', @success OUT, @jwsCompact
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        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.
    --  Load the signer's public key and set it for validating signature 0.
    DECLARE @pubKey int
    EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @pubKey OUT

    EXEC sp_OAMethod @pubKey, 'LoadFromFile', @success OUT, 'qa_data/signer_pubkey.pem'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pubKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @pubKey
        RETURN
      END
    EXEC sp_OAMethod @jws, 'SetPublicKey', @success OUT, 0, @pubKey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @pubKey
        RETURN
      END

    --  Validate the signature.  Validate returns 1 when valid, 0 when not, or a negative value on error.
    DECLARE @v int
    EXEC sp_OAMethod @jws, 'Validate', @v OUT, 0
    IF @v < 0
      BEGIN
        EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @pubKey
        RETURN
      END
    IF @v <> 1
      BEGIN

        PRINT 'The signature is not valid.'
        EXEC @hr = sp_OADestroy @jws
        EXEC @hr = sp_OADestroy @pubKey
        RETURN
      END

    PRINT 'The signature is valid.'

    EXEC @hr = sp_OADestroy @jws
    EXEC @hr = sp_OADestroy @pubKey


END
GO