Zig
Zig
Create JWS with Multiple Signatures using the General JSON Serialization Format
See more JSON Web Signatures (JWS) Examples
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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.
const sb_rsa_jwk = try chilkat.StringBuilder.init();
defer sb_rsa_jwk.deinit();
sb_rsa_jwk.append("{\"kty\":\"RSA\",") catch {};
sb_rsa_jwk.append("\"n\":\"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx") catch {};
sb_rsa_jwk.append("HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs") catch {};
sb_rsa_jwk.append("D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH") catch {};
sb_rsa_jwk.append("SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV") catch {};
sb_rsa_jwk.append("MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8") catch {};
sb_rsa_jwk.append("NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ\",") catch {};
sb_rsa_jwk.append("\"e\":\"AQAB\",") catch {};
sb_rsa_jwk.append("\"d\":\"Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I") catch {};
sb_rsa_jwk.append("jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0") catch {};
sb_rsa_jwk.append("BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn") catch {};
sb_rsa_jwk.append("439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT") catch {};
sb_rsa_jwk.append("CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh") catch {};
sb_rsa_jwk.append("BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ\",") catch {};
sb_rsa_jwk.append("\"p\":\"4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi") catch {};
sb_rsa_jwk.append("YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG") catch {};
sb_rsa_jwk.append("BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc\",") catch {};
sb_rsa_jwk.append("\"q\":\"uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa") catch {};
sb_rsa_jwk.append("ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA") catch {};
sb_rsa_jwk.append("-njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc\",") catch {};
sb_rsa_jwk.append("\"dp\":\"BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q") catch {};
sb_rsa_jwk.append("CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb") catch {};
sb_rsa_jwk.append("34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0\",") catch {};
sb_rsa_jwk.append("\"dq\":\"h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa") catch {};
sb_rsa_jwk.append("7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky") catch {};
sb_rsa_jwk.append("NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU\",") catch {};
sb_rsa_jwk.append("\"qi\":\"IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o") catch {};
sb_rsa_jwk.append("y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU") catch {};
sb_rsa_jwk.append("W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U\"") catch {};
sb_rsa_jwk.append("}") catch {};
const rsa_key = try chilkat.PrivateKey.init();
defer rsa_key.deinit();
// 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..)
rsa_key.loadJwk(try sb_rsa_jwk.getAsString(alloc)) catch {
std.debug.print("{s}\n", .{try rsa_key.getLastErrorText(alloc)});
return;
};
// ---------------------------------------------------
// Use the following ECC key loaded from JWK format.
const sb_ecc_jwk = try chilkat.StringBuilder.init();
defer sb_ecc_jwk.deinit();
sb_ecc_jwk.append("{\"kty\":\"EC\",") catch {};
sb_ecc_jwk.append("\"crv\":\"P-256\",") catch {};
sb_ecc_jwk.append("\"x\":\"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU\",") catch {};
sb_ecc_jwk.append("\"y\":\"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0\",") catch {};
sb_ecc_jwk.append("\"d\":\"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI\"") catch {};
sb_ecc_jwk.append("}") catch {};
const ecc_key = try chilkat.PrivateKey.init();
defer ecc_key.deinit();
// 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..)
ecc_key.loadJwk(try sb_ecc_jwk.getAsString(alloc)) catch {
std.debug.print("{s}\n", .{try ecc_key.getLastErrorText(alloc)});
return;
};
// ---------------------------------------------------
// The HMAC key (in base64url format)
const hmac_key = "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow";
// ---------------------------------------------------
// Prepare the headers..
// RSASSA-PKCS1-v1_5 SHA-256
const jws_prot_hdr0 = try chilkat.JsonObject.init();
defer jws_prot_hdr0.deinit();
jws_prot_hdr0.appendString("alg", "RS256") catch {};
const jws_unprot_hdr0 = try chilkat.JsonObject.init();
defer jws_unprot_hdr0.deinit();
jws_unprot_hdr0.appendString("kid", "myRsaKey") catch {};
// ECDSA using P-256 and SHA-256
const jws_prot_hdr1 = try chilkat.JsonObject.init();
defer jws_prot_hdr1.deinit();
jws_prot_hdr1.appendString("alg", "ES256") catch {};
const jws_unprot_hdr1 = try chilkat.JsonObject.init();
defer jws_unprot_hdr1.deinit();
jws_unprot_hdr1.appendString("kid", "myEcKey") catch {};
// HMAC using SHA-256
const jws_prot_hdr2 = try chilkat.JsonObject.init();
defer jws_prot_hdr2.deinit();
jws_prot_hdr2.appendString("alg", "HS256") catch {};
const jws_unprot_hdr2 = try chilkat.JsonObject.init();
defer jws_unprot_hdr2.deinit();
jws_unprot_hdr2.appendString("kid", "myMacKey") catch {};
// ---------------------------------------------------
// First set the JWS headers, keys, and payload, then we'll create the JWS...
const jws = try chilkat.Jws.init();
defer jws.deinit();
jws.setProtectedHeader(0, jws_prot_hdr0) catch {};
jws.setUnprotectedHeader(0, jws_unprot_hdr0) catch {};
jws.setProtectedHeader(1, jws_prot_hdr1) catch {};
jws.setUnprotectedHeader(1, jws_unprot_hdr1) catch {};
jws.setProtectedHeader(2, jws_prot_hdr2) catch {};
jws.setUnprotectedHeader(2, jws_unprot_hdr2) catch {};
jws.setPrivateKey(0, rsa_key) catch {};
jws.setPrivateKey(1, ecc_key) catch {};
jws.setMacKey(2, hmac_key, "base64url") catch {};
const b_include_bom = false;
const payload_str = "In our village, folks say God crumbles up the old moon into stars.";
jws.setPayload(payload_str, "utf-8", b_include_bom) catch {};
// ---------------------------------------------------
// Create the JWS.
// Givent that multiple signatures will exist, only the general JSON serialization
// format is possible, and that is what will be produced.
const jws_str = jws.createJws(alloc) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
// 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..
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(jws_str) catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// 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"
// }
// ]
// }
}