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

JWS Using HMAC SHA-256

See more JSON Web Signatures (JWS) Examples

Creates a JSON Web Signatures (JWS) using HMAC SHA-256.

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.

    // First create the JWS Protected Header
    const jws_prot_hdr = try chilkat.JsonObject.init();
    defer jws_prot_hdr.deinit();
    jws_prot_hdr.appendString("typ", "JWT") catch {};
    jws_prot_hdr.appendString("alg", "HS256") catch {};
    std.debug.print("JWS Protected Header: {s}\n", .{try jws_prot_hdr.emit(alloc)});

    // Output:
    // JWS Protected Header: {"typ":"JWT","alg":"HS256"}

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

    // Set the HMAC key:
    const hmac_key = "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow";
    var signature_index: i32 = 0;
    jws.setMacKey(signature_index, hmac_key, "base64url") catch {};

    // Set the protected header:
    jws.setProtectedHeader(signature_index, jws_prot_hdr) 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});

    // sample output:
    // JWS: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu.bsYsi8HJ0N6OqGI1hKQ9QQRNPxxA5qMpcHLtOvXatk8

    // 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);

    // Set the MAC key used for validation.
    signature_index = 0;
    jws2.setMacKey(signature_index, hmac_key, "base64url") 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 MAC 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 {
        std.debug.print("{s}\n", .{try jws2.getLastErrorText(alloc)});
        return;
    };

    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:
    //     {
    //       "typ": "JWT",
    //       "alg": "HS256"
    //     }
}