Zig
Zig
Set the Optional JWS Unprotected Header
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetUnprotectedHeader, which sets the optional unprotected header for a signature.
Background. Unlike the protected header, the unprotected header is not covered by the signature. It is carried in the JSON serialization forms of a JWS, so producing a JWS with an unprotected header yields a JSON serialization rather than compact form.
Chilkat Zig Downloads
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();
// Protected header specifying HMAC-SHA256.
const header = try chilkat.JsonObject.init();
defer header.deinit();
header.updateString("alg", "HS256") catch {};
jws.setProtectedHeader(0, header) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
// Set the optional unprotected header for signature 0. Unlike the protected header, it is not
// covered by the signature. It is carried in the JSON serialization forms of a JWS.
const unprotected = try chilkat.JsonObject.init();
defer unprotected.deinit();
unprotected.updateString("kid", "signer-key-1") catch {};
jws.setUnprotectedHeader(0, unprotected) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
const b_include_bom = false;
jws.setPayload("This is the content to sign.", "utf-8", b_include_bom) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
const base64_key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=";
jws.setMacKey(0, base64_key, "base64") catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
// Because an unprotected header is present, the JWS is produced in a JSON serialization form.
const jws_json = jws.createJws(alloc) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{jws_json});
}