Sample code for 30+ languages & platforms
Zig

Set the JWS Protected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetProtectedHeader, which sets the integrity-protected header for a signer from a JsonObject. The header specifies the signature algorithm (alg).

Background. The protected header is covered by the signature, so it cannot be altered without invalidating the JWS. It is configured before creating the JWS.

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

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

    // Build the JWS protected header.  The "alg" member names the signature algorithm.
    const header = try chilkat.JsonObject.init();
    defer header.deinit();
    header.updateString("alg", "HS256") catch {};

    // Set the protected header for signer index 0.  The protected header is integrity-protected: it is
    // covered by the signature.
    jws.setProtectedHeader(0, header) catch {
        std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Protected header set.\n", .{});
}