![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Create JWS using the Flattened JSON Serialization FormatCreates a JSON Web Signature (JWS) containing 1 signature and output using the flattened JSON serialization format. Note: This example requires Chilkat v9.5.0.66 or greater.
-- 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) -- 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. -- The JWS to be created will contain one signature using an ECDSA key. DECLARE @sbEccJwk int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbEccJwk OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '{"kty":"EC",' EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '"crv":"P-256",' EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '"x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",' EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '"y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0",' EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '"d":"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI"' EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '}' DECLARE @eccKey int -- Use "Chilkat_9_5_0.PrivateKey" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @eccKey OUT -- Note: This example loads the ECDSA key from JWK format. Any format can be loaded -- into the private key object. (See the online reference documentation..) DECLARE @success int EXEC sp_OAMethod @sbEccJwk, 'GetAsString', @sTmp0 OUT EXEC sp_OAMethod @eccKey, 'LoadJwk', @success OUT, @sTmp0 IF @success <> 1 BEGIN EXEC sp_OAGetProperty @eccKey, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sbEccJwk EXEC @hr = sp_OADestroy @eccKey RETURN END -- --------------------------------------------------- -- Prepare both a protected and unprotected header. DECLARE @jwsProtHdr int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwsProtHdr OUT EXEC sp_OAMethod @jwsProtHdr, 'AppendString', @success OUT, 'alg', 'ES256' DECLARE @jwsUnprotHdr int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwsUnprotHdr OUT EXEC sp_OAMethod @jwsUnprotHdr, 'AppendString', @success OUT, 'kid', 'myEcKey' -- --------------------------------------------------- DECLARE @jws int -- Use "Chilkat_9_5_0.Jws" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Jws', @jws OUT EXEC sp_OAMethod @jws, 'SetProtectedHeader', @success OUT, 0, @jwsProtHdr EXEC sp_OAMethod @jws, 'SetUnprotectedHeader', @success OUT, 0, @jwsUnprotHdr EXEC sp_OAMethod @jws, 'SetPrivateKey', @success OUT, 0, @eccKey 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. -- Indicate we prefer the non-compact, flattened serialization. EXEC sp_OASetProperty @jws, 'PreferCompact', 0 EXEC sp_OASetProperty @jws, 'PreferFlattened', 1 DECLARE @jwsStr nvarchar(4000) EXEC sp_OAMethod @jws, 'CreateJws', @jwsStr OUT EXEC sp_OAGetProperty @jws, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sbEccJwk EXEC @hr = sp_OADestroy @eccKey EXEC @hr = sp_OADestroy @jwsProtHdr EXEC @hr = sp_OADestroy @jwsUnprotHdr EXEC @hr = sp_OADestroy @jws RETURN END -- The jwsStr is contains the flattened JSON in the smallest possible size, which is a single line. -- To get in human-readable format, load into a Chilkat JSON object and emit.. DECLARE @json int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT EXEC sp_OAMethod @json, 'Load', @success OUT, @jwsStr EXEC sp_OASetProperty @json, 'EmitCompact', 0 EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT PRINT @sTmp0 -- Sample output: -- { -- "payload": "SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu", -- "protected": "eyJhbGciOiJFUzI1NiJ9", -- "header": { -- "kid": "myEcKey" -- }, -- "signature": "b7V2UpDPytr-kMnM_YjiQ3E0J2ucOI9LYA7mt57vccrK1rb84j9areqgQcJwOA00aWGoz4hf6sMTBfobdcJEGg" -- } EXEC @hr = sp_OADestroy @sbEccJwk EXEC @hr = sp_OADestroy @eccKey EXEC @hr = sp_OADestroy @jwsProtHdr EXEC @hr = sp_OADestroy @jwsUnprotHdr EXEC @hr = sp_OADestroy @jws EXEC @hr = sp_OADestroy @json END GO |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.