Zig
Zig
Sign a JWS with a Certificate
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetSigningCert, which sets the certificate whose associated private key is used to create a signature.
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. The certificate must have an accessible private key. This is convenient when the signing identity is held as a certificate (for example loaded from a PFX).
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.
// The PFX password should come from a secure source rather than being hard-coded.
const pfx_password = "myPfxPassword";
// Load a certificate that has an associated private key, then set it as the signing certificate for
// signature 0. The certificate's private key is used to create the signature.
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadPfxFile("qa_data/signer.pfx", pfx_password) catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
jws.setSigningCert(0, cert) 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});
}