Zig Requires Chilkat v10.1.2+
Zig
Create PKCS7 Signed File (.p7m)
See more Encryption Examples
Demonstrates how to sign a file to create a .p7m that contains both the file contents and 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();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
const cert_store = try chilkat.CertStore.init();
defer cert_store.deinit();
// Load a PFX file into a certificate store object.
cert_store.loadPfxFile("myPfx.pfx", "pfxPassword") catch {
std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
return;
};
// Get the certificate by subject common name.
// This should be the cert within the PFX that also
// has a private key (also stored within the PFX).
const json_cn = try chilkat.JsonObject.init();
defer json_cn.deinit();
json_cn.updateString("CN", "myCert") catch {};
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert_store.findCert(json_cn, cert) catch {
std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
return;
};
// Tell the crypt object to use the certificate for signing:
try crypt.setSigningCert(cert);
// Sign a file, producing a .p7m as output.
// The input file is unchanged, the test.p7m contains the
// contents of the input file and the signature.
const in_file = "test.txt";
const out_file = "testp7m";
crypt.createP7M(in_file, out_file) catch {
std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
return;
};
std.debug.print("Success!\n", .{});
}