Zig
Zig
Set the Shared JWE Unprotected Header
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetUnprotectedHeader, which sets the shared JWE unprotected header from a JsonObject.
Background. The unprotected header is shared by all recipients but, unlike the protected header, is not integrity-protected. It is carried in the JSON serialization forms of a JWE.
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 jwe = try chilkat.Jwe.init();
defer jwe.deinit();
// Protected header: AES key-wrap with AES-256-GCM content encryption.
const header = try chilkat.JsonObject.init();
defer header.deinit();
header.updateString("alg", "A256KW") catch {};
header.updateString("enc", "A256GCM") catch {};
jwe.setProtectedHeader(header) catch {
std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
return;
};
// The 256-bit key-wrapping key (base64) for recipient index 0. In production, obtain the key from a
// secure source rather than hard-coding it.
const base64_key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=";
jwe.setWrappingKey(0, base64_key, "base64") catch {
std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
return;
};
// Set the shared JWE unprotected header. It is shared by all recipients but, unlike the protected
// header, is not integrity-protected.
const unprotected = try chilkat.JsonObject.init();
defer unprotected.deinit();
unprotected.updateString("cty", "text/plain") catch {};
jwe.setUnprotectedHeader(unprotected) catch {
std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
return;
};
const sb_content = try chilkat.StringBuilder.init();
defer sb_content.deinit();
sb_content.append("This is the secret content.") catch {};
const sb_jwe = try chilkat.StringBuilder.init();
defer sb_jwe.deinit();
jwe.encryptSb(sb_content, "utf-8", sb_jwe) catch {
std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{try sb_jwe.getAsString(alloc)});
}