SQL Server
SQL Server
Sign a JWS with an HMAC Key from BinData
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetMacKeyBd, which sets the symmetric MAC key for a signature from the raw bytes in a BinData.
Background. This is the in-memory equivalent of SetMacKey, for when the key material is already available as bytes.
Chilkat SQL Server Downloads
-- 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 HMAC-SHA256.
DECLARE @header int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @header OUT
EXEC sp_OAMethod @header, 'UpdateString', @success OUT, 'alg', 'HS256'
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
-- Set the symmetric MAC key for signature 0 from the raw bytes in a BinData. In production, obtain
-- the key material from a secure source.
DECLARE @bdKey int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdKey OUT
EXEC sp_OAMethod @bdKey, 'AppendEncoded', @success OUT, 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=', 'base64'
EXEC sp_OAMethod @jws, 'SetMacKeyBd', @success OUT, 0, @bdKey
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 @bdKey
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 @bdKey
RETURN
END
PRINT @jwsCompact
EXEC @hr = sp_OADestroy @jws
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @bdKey
END
GO