Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Create JWS with Multiple Signatures using the General JSON Serialization FormatCreates a JSON Web Signature (JWS) containing 3 signatures and output using the general JSON serialization format. 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 | | +--------------+-------------------------------+--------------------+ 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 three signatures. -- The 1st will use an ECC key, the 2nd an RSA key, and the 3rd an HMAC key. -- Prepare each key.. -- --------------------------------------------------- -- Use the following RSA key loaded from JWK format. DECLARE @sbRsaJwk int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRsaJwk OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '{"kty":"RSA",' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"n":"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ",' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"e":"AQAB",' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"d":"Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ",' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"p":"4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc",' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"q":"uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '-njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc",' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"dp":"BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0",' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"dq":"h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU",' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"qi":"IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U"' EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '}' DECLARE @rsaKey int -- Use "Chilkat_9_5_0.PrivateKey" for versions of Chilkat < 10.0.0 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..) DECLARE @success int EXEC sp_OAMethod @sbRsaJwk, 'GetAsString', @sTmp0 OUT EXEC sp_OAMethod @rsaKey, 'LoadJwk', @success OUT, @sTmp0 IF @success <> 1 BEGIN EXEC sp_OAGetProperty @rsaKey, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sbRsaJwk EXEC @hr = sp_OADestroy @rsaKey RETURN END -- --------------------------------------------------- -- Use the following ECC key loaded from JWK format. DECLARE @sbEccJwk int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbEccJwk OUT 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..) 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 @sbRsaJwk EXEC @hr = sp_OADestroy @rsaKey EXEC @hr = sp_OADestroy @sbEccJwk EXEC @hr = sp_OADestroy @eccKey RETURN END -- --------------------------------------------------- -- The HMAC key (in base64url format) DECLARE @hmacKey nvarchar(4000) SELECT @hmacKey = 'AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow' -- --------------------------------------------------- -- Prepare the headers.. -- RSASSA-PKCS1-v1_5 SHA-256 DECLARE @jwsProtHdr0 int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwsProtHdr0 OUT EXEC sp_OAMethod @jwsProtHdr0, 'AppendString', @success OUT, 'alg', 'RS256' DECLARE @jwsUnprotHdr0 int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwsUnprotHdr0 OUT EXEC sp_OAMethod @jwsUnprotHdr0, 'AppendString', @success OUT, 'kid', 'myRsaKey' -- ECDSA using P-256 and SHA-256 DECLARE @jwsProtHdr1 int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwsProtHdr1 OUT EXEC sp_OAMethod @jwsProtHdr1, 'AppendString', @success OUT, 'alg', 'ES256' DECLARE @jwsUnprotHdr1 int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwsUnprotHdr1 OUT EXEC sp_OAMethod @jwsUnprotHdr1, 'AppendString', @success OUT, 'kid', 'myEcKey' -- HMAC using SHA-256 DECLARE @jwsProtHdr2 int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwsProtHdr2 OUT EXEC sp_OAMethod @jwsProtHdr2, 'AppendString', @success OUT, 'alg', 'HS256' DECLARE @jwsUnprotHdr2 int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jwsUnprotHdr2 OUT EXEC sp_OAMethod @jwsUnprotHdr2, 'AppendString', @success OUT, 'kid', 'myMacKey' -- --------------------------------------------------- -- First set the JWS headers, keys, and payload, then we'll create the JWS... 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, @jwsProtHdr0 EXEC sp_OAMethod @jws, 'SetUnprotectedHeader', @success OUT, 0, @jwsUnprotHdr0 EXEC sp_OAMethod @jws, 'SetProtectedHeader', @success OUT, 1, @jwsProtHdr1 EXEC sp_OAMethod @jws, 'SetUnprotectedHeader', @success OUT, 1, @jwsUnprotHdr1 EXEC sp_OAMethod @jws, 'SetProtectedHeader', @success OUT, 2, @jwsProtHdr2 EXEC sp_OAMethod @jws, 'SetUnprotectedHeader', @success OUT, 2, @jwsUnprotHdr2 EXEC sp_OAMethod @jws, 'SetPrivateKey', @success OUT, 0, @rsaKey EXEC sp_OAMethod @jws, 'SetPrivateKey', @success OUT, 1, @eccKey EXEC sp_OAMethod @jws, 'SetMacKey', @success OUT, 2, @hmacKey, 'base64url' 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. -- Givent that multiple signatures will exist, only the general JSON serialization -- format is possible, and that is what will be produced. 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 @sbRsaJwk EXEC @hr = sp_OADestroy @rsaKey EXEC @hr = sp_OADestroy @sbEccJwk EXEC @hr = sp_OADestroy @eccKey EXEC @hr = sp_OADestroy @jwsProtHdr0 EXEC @hr = sp_OADestroy @jwsUnprotHdr0 EXEC @hr = sp_OADestroy @jwsProtHdr1 EXEC @hr = sp_OADestroy @jwsUnprotHdr1 EXEC @hr = sp_OADestroy @jwsProtHdr2 EXEC @hr = sp_OADestroy @jwsUnprotHdr2 EXEC @hr = sp_OADestroy @jws RETURN END -- The jwsStr is contains 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", -- "signatures": [ -- { -- "protected": "eyJhbGciOiJSUzI1NiJ9", -- "header": { -- "kid": "myRsaKey" -- }, -- "signature": "B04c24gSnpVm1Z-_bemfyNMCpZm6Knj1yB-yzaIOvijsWfDgoF_mSJccTIbzapNgwJudnobr5iDOfZWiRR9iqCyDJLe5M1S40vFF7MFEI3JecYRgrRc6n1lTkYLMRyVq48BwbQlmKgPqmK9drun3agklsr0FmgNx65pfmcnlYdXsgwxf8WbgppefrlrMImp-98-dNtBcUL8ce1aOjbcyVFjGMCzpm3JerQqIzWQvEwBstnMEQle73KHcyx_nsTmlzY70CaydbRTsciOATL7WfiMwuX1q9Y2NIpTg3CbOTWKdwjh7iyfiAKQxNBaF2mApnqj9hjpf8GwR-CfxAzJtPg" -- }, -- { -- "protected": "eyJhbGciOiJFUzI1NiJ9", -- "header": { -- "kid": "myEcKey" -- }, -- "signature": "2cbugKq0ERaQMh01n2B-86EZFYleeMf8bsccaQMxzOxAg14PxfjR3IImvodTJYqkmfBJYW203etz2-7ZtJUOGw" -- }, -- { -- "protected": "eyJhbGciOiJIUzI1NiJ9", -- "header": { -- "kid": "myMacKey" -- }, -- "signature": "e7R9gjx0RsUNa3c7qd8k9mQGEhtcG8vsN1W7jbLb2MA" -- } -- ] -- } EXEC @hr = sp_OADestroy @sbRsaJwk EXEC @hr = sp_OADestroy @rsaKey EXEC @hr = sp_OADestroy @sbEccJwk EXEC @hr = sp_OADestroy @eccKey EXEC @hr = sp_OADestroy @jwsProtHdr0 EXEC @hr = sp_OADestroy @jwsUnprotHdr0 EXEC @hr = sp_OADestroy @jwsProtHdr1 EXEC @hr = sp_OADestroy @jwsUnprotHdr1 EXEC @hr = sp_OADestroy @jwsProtHdr2 EXEC @hr = sp_OADestroy @jwsUnprotHdr2 EXEC @hr = sp_OADestroy @jws EXEC @hr = sp_OADestroy @json END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.