Zig
Zig
Sign a JWS with a Private Key
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetPrivateKey, which sets the private key used to create a signature. The example uses RS256 (RSASSA-PKCS1-v1_5 with SHA-256).
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. Public-key JWS signs with a private key so that anyone holding the corresponding public key can verify the signature.
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 RSASSA-PKCS1-v1_5 with SHA-256 (RS256).
const header = try chilkat.JsonObject.init();
defer header.deinit();
header.updateString("alg", "RS256") catch {};
jws.setProtectedHeader(0, header) 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;
};
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// Load the signer's private key and set it for signature 0.
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
priv_key.loadPemFile("qa_data/signer_privkey.pem") catch {
std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
return;
};
jws.setPrivateKey(0, priv_key) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
const jws_compact = jws.createJws(alloc) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{jws_compact});
}