Zig
Zig
Add Private Key and Certificate to a PEM
See more PEM Examples
Demonstrates how to add certificates and private keys to a PEM.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The Chilkat PEM class was introduced in v9.5.0.49.
// It requires the bundle to be unlocked, as shown above.
const pem = try chilkat.Pem.init();
defer pem.deinit();
// Add the private key found in alice.key to this PEM.
//
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
priv_key.loadAnyFormatFile("qa_data/alice.key", "") catch {
std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
return;
};
// Add it to the PEM:
try pem.addPrivateKey(priv_key);
// Add the certificate found in alice.crt to this PEM.
//
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadFromFile("qa_data/alice.crt") catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
// Add it to the PEM:
const include_cert_chain = false;
try pem.addCert(cert, include_cert_chain);
// Write the PEM containing the private key and certificate.
// The private key will be output in PKCS8 encrypted form.
// Certificates are never encrypted.
// This is the password that will be required to open and access the private key
// from the PEM we're about to write..
const password = "secret";
const extended_attrs = false;
const no_keys = false;
const no_certs = false;
const no_ca_certs = false;
const encrypt_alg = "aes128";
const pem_str = try pem.toPemEx(alloc, extended_attrs, no_keys, no_certs, no_ca_certs, encrypt_alg, password);
std.debug.print("{s}\n", .{pem_str});
}