Sample code for 30+ languages & platforms
SQL Server

Validate a JWS Signature

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.Validate, which validates exactly one signature, identified by a zero-based index, using the key configured at that same index.

Background. Validate returns 1 when the signature or MAC is cryptographically valid, 0 when it is not, or a negative value on error. The verifying key must be provided before validating.

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

    --  Provide the key for signature 0 (here an HMAC key).
    DECLARE @base64Key nvarchar(4000)
    SELECT @base64Key = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU='
    EXEC sp_OAMethod @jws, 'SetMacKey', @success OUT, 0, @base64Key, 'base64'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @jws
        RETURN
      END

    --  Validate the signature identified by the zero-based index, using the key configured at that index.
    --  Validate returns 1 when the signature or MAC is cryptographically valid, 0 when it is 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
        RETURN
      END
    IF @v <> 1
      BEGIN

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

    PRINT 'The signature is valid.'

    EXEC @hr = sp_OADestroy @jws


END
GO