Sample code for 30+ languages & platforms
Zig

Get a JWS Protected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.GetProtectedH, which copies the decoded protected header of a signature into a JsonObject.

Background. The protected header is integrity-protected by the signature. Inspecting it (for example the alg value) is useful before deciding how to validate.

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

    // Load the JWS compact serialization.
    const jws_compact = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>";
    jws.loadJws(jws_compact) catch {
        std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
        return;
    };

    // Copy the decoded protected header of signature 0 into a JsonObject.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    jws.getProtectedH(0, json) catch {
        std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
        return;
    };

    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});
}