Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Tcl Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
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)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Tcl) 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 Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

#  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.
set sbRsaJwk [new_CkStringBuilder]

CkStringBuilder_Append $sbRsaJwk "{\"kty\":\"RSA\","
CkStringBuilder_Append $sbRsaJwk "\"n\":\"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx"
CkStringBuilder_Append $sbRsaJwk "HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs"
CkStringBuilder_Append $sbRsaJwk "D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH"
CkStringBuilder_Append $sbRsaJwk "SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV"
CkStringBuilder_Append $sbRsaJwk "MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8"
CkStringBuilder_Append $sbRsaJwk "NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ\","
CkStringBuilder_Append $sbRsaJwk "\"e\":\"AQAB\","
CkStringBuilder_Append $sbRsaJwk "\"d\":\"Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I"
CkStringBuilder_Append $sbRsaJwk "jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0"
CkStringBuilder_Append $sbRsaJwk "BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn"
CkStringBuilder_Append $sbRsaJwk "439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT"
CkStringBuilder_Append $sbRsaJwk "CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh"
CkStringBuilder_Append $sbRsaJwk "BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ\","
CkStringBuilder_Append $sbRsaJwk "\"p\":\"4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi"
CkStringBuilder_Append $sbRsaJwk "YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG"
CkStringBuilder_Append $sbRsaJwk "BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc\","
CkStringBuilder_Append $sbRsaJwk "\"q\":\"uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa"
CkStringBuilder_Append $sbRsaJwk "ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA"
CkStringBuilder_Append $sbRsaJwk "-njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc\","
CkStringBuilder_Append $sbRsaJwk "\"dp\":\"BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q"
CkStringBuilder_Append $sbRsaJwk "CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb"
CkStringBuilder_Append $sbRsaJwk "34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0\","
CkStringBuilder_Append $sbRsaJwk "\"dq\":\"h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa"
CkStringBuilder_Append $sbRsaJwk "7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky"
CkStringBuilder_Append $sbRsaJwk "NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU\","
CkStringBuilder_Append $sbRsaJwk "\"qi\":\"IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o"
CkStringBuilder_Append $sbRsaJwk "y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU"
CkStringBuilder_Append $sbRsaJwk "W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U\""
CkStringBuilder_Append $sbRsaJwk "}"

set rsaKey [new_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..)
set success [CkPrivateKey_LoadJwk $rsaKey [CkStringBuilder_getAsString $sbRsaJwk]]
if {[expr $success != 1]} then {
    puts [CkPrivateKey_lastErrorText $rsaKey]
    delete_CkStringBuilder $sbRsaJwk
    delete_CkPrivateKey $rsaKey
    exit
}

#  ---------------------------------------------------
#  Use the following ECC key loaded from JWK format.
set sbEccJwk [new_CkStringBuilder]

CkStringBuilder_Append $sbEccJwk "{\"kty\":\"EC\","
CkStringBuilder_Append $sbEccJwk "\"crv\":\"P-256\","
CkStringBuilder_Append $sbEccJwk "\"x\":\"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU\","
CkStringBuilder_Append $sbEccJwk "\"y\":\"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0\","
CkStringBuilder_Append $sbEccJwk "\"d\":\"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI\""
CkStringBuilder_Append $sbEccJwk "}"

set eccKey [new_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..)
set success [CkPrivateKey_LoadJwk $eccKey [CkStringBuilder_getAsString $sbEccJwk]]
if {[expr $success != 1]} then {
    puts [CkPrivateKey_lastErrorText $eccKey]
    delete_CkStringBuilder $sbRsaJwk
    delete_CkPrivateKey $rsaKey
    delete_CkStringBuilder $sbEccJwk
    delete_CkPrivateKey $eccKey
    exit
}

#  ---------------------------------------------------
#  The HMAC key (in base64url format)
set hmacKey "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"

#  ---------------------------------------------------
#  Prepare the headers..
#  RSASSA-PKCS1-v1_5 SHA-256
set jwsProtHdr0 [new_CkJsonObject]

CkJsonObject_AppendString $jwsProtHdr0 "alg" "RS256"
set jwsUnprotHdr0 [new_CkJsonObject]

CkJsonObject_AppendString $jwsUnprotHdr0 "kid" "myRsaKey"

#  ECDSA using P-256 and SHA-256
set jwsProtHdr1 [new_CkJsonObject]

CkJsonObject_AppendString $jwsProtHdr1 "alg" "ES256"
set jwsUnprotHdr1 [new_CkJsonObject]

CkJsonObject_AppendString $jwsUnprotHdr1 "kid" "myEcKey"

#  HMAC using SHA-256
set jwsProtHdr2 [new_CkJsonObject]

CkJsonObject_AppendString $jwsProtHdr2 "alg" "HS256"
set jwsUnprotHdr2 [new_CkJsonObject]

CkJsonObject_AppendString $jwsUnprotHdr2 "kid" "myMacKey"

#  ---------------------------------------------------
#  First set the JWS headers, keys, and payload, then we'll create the JWS...
set jws [new_CkJws]

CkJws_SetProtectedHeader $jws 0 $jwsProtHdr0
CkJws_SetUnprotectedHeader $jws 0 $jwsUnprotHdr0
CkJws_SetProtectedHeader $jws 1 $jwsProtHdr1
CkJws_SetUnprotectedHeader $jws 1 $jwsUnprotHdr1
CkJws_SetProtectedHeader $jws 2 $jwsProtHdr2
CkJws_SetUnprotectedHeader $jws 2 $jwsUnprotHdr2

CkJws_SetPrivateKey $jws 0 $rsaKey
CkJws_SetPrivateKey $jws 1 $eccKey
CkJws_SetMacKey $jws 2 $hmacKey "base64url"

set bIncludeBom 0
set payloadStr "In our village, folks say God crumbles up the old moon into stars."
CkJws_SetPayload $jws $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.
set jwsStr [CkJws_createJws $jws]
if {[expr [CkJws_get_LastMethodSuccess $jws] != 1]} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkStringBuilder $sbRsaJwk
    delete_CkPrivateKey $rsaKey
    delete_CkStringBuilder $sbEccJwk
    delete_CkPrivateKey $eccKey
    delete_CkJsonObject $jwsProtHdr0
    delete_CkJsonObject $jwsUnprotHdr0
    delete_CkJsonObject $jwsProtHdr1
    delete_CkJsonObject $jwsUnprotHdr1
    delete_CkJsonObject $jwsProtHdr2
    delete_CkJsonObject $jwsUnprotHdr2
    delete_CkJws $jws
    exit
}

#  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..
set json [new_CkJsonObject]

CkJsonObject_Load $json $jwsStr
CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]

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

delete_CkStringBuilder $sbRsaJwk
delete_CkPrivateKey $rsaKey
delete_CkStringBuilder $sbEccJwk
delete_CkPrivateKey $eccKey
delete_CkJsonObject $jwsProtHdr0
delete_CkJsonObject $jwsUnprotHdr0
delete_CkJsonObject $jwsProtHdr1
delete_CkJsonObject $jwsUnprotHdr1
delete_CkJsonObject $jwsProtHdr2
delete_CkJsonObject $jwsUnprotHdr2
delete_CkJws $jws
delete_CkJsonObject $json

 

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