Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Create a JWS Using ECDSA P-521 SHA-512

See more JSON Web Signatures (JWS) Examples

Creates and verifies a JSON Web Signature (JWS) that uses ECDSA P-521 SHA-512

Chilkat Zig Downloads

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

    // Use the following ECC key loaded from JWK format.
    const sb_jwk = try chilkat.StringBuilder.init();
    defer sb_jwk.deinit();
    sb_jwk.append("{\"kty\":\"EC\",") catch {};
    sb_jwk.append("\"crv\":\"P-521\",") catch {};
    sb_jwk.append("\"x\":\"AekpBQ8ST8a8VcfVOTNl353vSrDCLLJXmPk06wTjxrrjcBpXp5EOnYG_NjFZ6OvLFV1jSfS9tsz4qUxcWceqwQGk\",") catch {};
    sb_jwk.append("\"y\":\"ADSmRA43Z1DSNx_RvcLI87cdL07l6jQyyBXMoxVg_l2Th-x3S1WDhjDly79ajL4Kkd0AZMaZmh9ubmf63e3kyMj2\",") catch {};
    sb_jwk.append("\"d\":\"AY5pb7A0UFiB3RELSD64fTLOSV_jazdF7fLYyuTw8lOfRhWg6Y6rUrPAxerEzgdRhajnu0ferB0d53vM9mE15j2C\"") catch {};
    sb_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_jwk.getAsString(alloc)) catch {
        std.debug.print("{s}\n", .{try ecc_key.getLastErrorText(alloc)});
        return;
    };

    // Create the JWS Protected Header
    const jws_prot_hdr = try chilkat.JsonObject.init();
    defer jws_prot_hdr.deinit();
    jws_prot_hdr.appendString("alg", "ES512") catch {};

    const jws = try chilkat.Jws.init();
    defer jws.deinit();

    // Set the protected header:
    var signature_index: i32 = 0;
    jws.setProtectedHeader(signature_index, jws_prot_hdr) catch {};

    // Set the ECC key:
    jws.setPrivateKey(signature_index, ecc_key) catch {};

    // Set the payload.
    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
    // By default, the compact serialization is used.
    const jws_compact = jws.createJws(alloc) catch {
        std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("JWS: {s}\n", .{jws_compact});

    // Note: ECC signatures use random values, so the output will be different each time a signature is produced.
    // sample output:
    // JWS: eyJhbGciOiJFUzUxMiJ9.SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu.AFOnOtZ8UTp-ed1PfLfXxwGU7zT_jnmGGxew-d1CP6SfKa5RUhav5mF4szhSof22JaoQ742VIa0eZ0s1rhBnj8y6APr6g6vKzIpYZlJsnjzjUwAjDQOLmcsjxrrRfATh-NEv7G0Z9FrjdJasS44tCG2EnG6aT2TW-sPG0dy5o9jUWCzi

    // Now load the JWS, validate, and recover the original text.
    const jws2 = try chilkat.Jws.init();
    defer jws2.deinit();

    // Load the JWS.
    try jws2.loadJws(jws_compact);

    const ecc_pub_key = try chilkat.PublicKey.init();
    defer ecc_pub_key.deinit();
    ecc_key.toPublicKey(ecc_pub_key) catch {};

    // Set the ECC public key used for validation.
    signature_index = 0;
    jws2.setPublicKey(signature_index, ecc_pub_key) catch {};

    // Validate the 1st (and only) signature at index 0..
    const v = jws2.validate(signature_index);
    if (v < 0) {
        // Perhaps Chilkat was not unlocked or the trial expired..
        std.debug.print("Method call failed for some other reason.\n", .{});
        std.debug.print("{s}\n", .{try jws2.getLastErrorText(alloc)});
        return;
    }

    if (v == 0) {
        std.debug.print("Invalid signature.  The ECC key was incorrect, the JWS was invalid, or both.\n", .{});
        return;
    }

    // If we get here, the signature was validated..
    std.debug.print("Signature validated.\n", .{});

    // Recover the original content:
    std.debug.print("{s}\n", .{try jws2.getPayload(alloc, "utf-8")});

    // Examine the protected header:
    const jose_header = try chilkat.JsonObject.init();
    defer jose_header.deinit();
    jws2.getProtectedH(signature_index, jose_header) catch {};
    jose_header.setEmitCompact(false);

    std.debug.print("Protected (JOSE) header:\n", .{});
    std.debug.print("{s}\n", .{try jose_header.emit(alloc)});

    // Output:
    //     Signature validated.
    //     In our village, folks say God crumbles up the old moon into stars.
    //     Protected (JOSE) header:
    //     {
    //       "alg": "ES512"
    //     }
}