Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Lianja Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Lianja) Create JWS with Multiple Signatures using the General JSON Serialization Format

Creates 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.

Chilkat Lianja Extension Download

Chilkat Lianja Extension

// 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.
loSbRsaJwk = createobject("CkStringBuilder")
loSbRsaJwk.Append('{"kty":"RSA",')
loSbRsaJwk.Append('"n":"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx')
loSbRsaJwk.Append("HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs")
loSbRsaJwk.Append("D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH")
loSbRsaJwk.Append("SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV")
loSbRsaJwk.Append("MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8")
loSbRsaJwk.Append('NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ",')
loSbRsaJwk.Append('"e":"AQAB",')
loSbRsaJwk.Append('"d":"Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I')
loSbRsaJwk.Append("jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0")
loSbRsaJwk.Append("BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn")
loSbRsaJwk.Append("439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT")
loSbRsaJwk.Append("CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh")
loSbRsaJwk.Append('BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ",')
loSbRsaJwk.Append('"p":"4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi')
loSbRsaJwk.Append("YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG")
loSbRsaJwk.Append('BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc",')
loSbRsaJwk.Append('"q":"uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa')
loSbRsaJwk.Append("ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA")
loSbRsaJwk.Append('-njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc",')
loSbRsaJwk.Append('"dp":"BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q')
loSbRsaJwk.Append("CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb")
loSbRsaJwk.Append('34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0",')
loSbRsaJwk.Append('"dq":"h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa')
loSbRsaJwk.Append("7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky")
loSbRsaJwk.Append('NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU",')
loSbRsaJwk.Append('"qi":"IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o')
loSbRsaJwk.Append("y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU")
loSbRsaJwk.Append('W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U"')
loSbRsaJwk.Append("}")

loRsaKey = createobject("CkPrivateKey")
// 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..)
llSuccess = loRsaKey.LoadJwk(loSbRsaJwk.GetAsString())
if (llSuccess <> .T.) then
    ? loRsaKey.LastErrorText
    release loSbRsaJwk
    release loRsaKey
    return
endif

// ---------------------------------------------------
// Use the following ECC key loaded from JWK format.
loSbEccJwk = createobject("CkStringBuilder")
loSbEccJwk.Append('{"kty":"EC",')
loSbEccJwk.Append('"crv":"P-256",')
loSbEccJwk.Append('"x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",')
loSbEccJwk.Append('"y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0",')
loSbEccJwk.Append('"d":"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI"')
loSbEccJwk.Append("}")

loEccKey = createobject("CkPrivateKey")
// 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..)
llSuccess = loEccKey.LoadJwk(loSbEccJwk.GetAsString())
if (llSuccess <> .T.) then
    ? loEccKey.LastErrorText
    release loSbRsaJwk
    release loRsaKey
    release loSbEccJwk
    release loEccKey
    return
endif

// ---------------------------------------------------
// The HMAC key (in base64url format)
lcHmacKey = "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"

// ---------------------------------------------------
// Prepare the headers..
// RSASSA-PKCS1-v1_5 SHA-256
loJwsProtHdr0 = createobject("CkJsonObject")
loJwsProtHdr0.AppendString("alg","RS256")
loJwsUnprotHdr0 = createobject("CkJsonObject")
loJwsUnprotHdr0.AppendString("kid","myRsaKey")

// ECDSA using P-256 and SHA-256
loJwsProtHdr1 = createobject("CkJsonObject")
loJwsProtHdr1.AppendString("alg","ES256")
loJwsUnprotHdr1 = createobject("CkJsonObject")
loJwsUnprotHdr1.AppendString("kid","myEcKey")

// HMAC using SHA-256
loJwsProtHdr2 = createobject("CkJsonObject")
loJwsProtHdr2.AppendString("alg","HS256")
loJwsUnprotHdr2 = createobject("CkJsonObject")
loJwsUnprotHdr2.AppendString("kid","myMacKey")

// ---------------------------------------------------
// First set the JWS headers, keys, and payload, then we'll create the JWS...
loJws = createobject("CkJws")

loJws.SetProtectedHeader(0,loJwsProtHdr0)
loJws.SetUnprotectedHeader(0,loJwsUnprotHdr0)
loJws.SetProtectedHeader(1,loJwsProtHdr1)
loJws.SetUnprotectedHeader(1,loJwsUnprotHdr1)
loJws.SetProtectedHeader(2,loJwsProtHdr2)
loJws.SetUnprotectedHeader(2,loJwsUnprotHdr2)

loJws.SetPrivateKey(0,loRsaKey)
loJws.SetPrivateKey(1,loEccKey)
loJws.SetMacKey(2,lcHmacKey,"base64url")

llBIncludeBom = .F.
lcPayloadStr = "In our village, folks say God crumbles up the old moon into stars."
loJws.SetPayload(lcPayloadStr,"utf-8",llBIncludeBom)

// ---------------------------------------------------
// Create the JWS.
// Givent that multiple signatures will exist, only the general JSON serialization 
// format is possible, and that is what will be produced.
lcJwsStr = loJws.CreateJws()
if (loJws.LastMethodSuccess <> .T.) then
    ? loJws.LastErrorText
    release loSbRsaJwk
    release loRsaKey
    release loSbEccJwk
    release loEccKey
    release loJwsProtHdr0
    release loJwsUnprotHdr0
    release loJwsProtHdr1
    release loJwsUnprotHdr1
    release loJwsProtHdr2
    release loJwsUnprotHdr2
    release loJws
    return
endif

// 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..
loJson = createobject("CkJsonObject")
loJson.Load(lcJwsStr)
loJson.EmitCompact = .F.
? loJson.Emit()

// 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"
//     }
//   ]
// }


release loSbRsaJwk
release loRsaKey
release loSbEccJwk
release loEccKey
release loJwsProtHdr0
release loJwsUnprotHdr0
release loJwsProtHdr1
release loJwsUnprotHdr1
release loJwsProtHdr2
release loJwsUnprotHdr2
release loJws
release loJson

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.