Zig Requires Chilkat v10.1.2+
Zig
Sign a File to Create a .p7m File (using a PFX)
See more Encryption Examples
_LANGUAGE_ example to sign a file creating a .p7m file as output. The .p7m contains the signed contents of the original file. It can be verified and restored by calling VerifyP7M.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();
// Use a digital certificate and private key from a PFX file (.pfx or .p12).
const signing_cert_subject = "Acme Inc";
const pfx_filename = "/Users/chilkat/testData/pfx/acme.pfx";
const pfx_password = "test123";
const cert_store = try chilkat.CertStore.init();
defer cert_store.deinit();
cert_store.loadPfxFile(pfx_filename, pfx_password) catch {
std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
return;
};
const json_cn = try chilkat.JsonObject.init();
defer json_cn.deinit();
json_cn.updateString("CN", signing_cert_subject) catch {};
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert_store.findCert(json_cn, cert) catch {
std.debug.print("Failed to find certificate by subject common name.\n", .{});
return;
};
// Tell the crypt component to use this cert.
try crypt.setSigningCert(cert);
// We can sign any type of file, creating a .p7m as output:
var in_file: [:0]const u8 = "/Users/chilkat/testData/pdf/sample.pdf";
var output_file: [:0]const u8 = "/Users/chilkat/testData/p7m/sample.pdf.p7m";
crypt.createP7M(in_file, output_file) catch {
std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
return;
};
// Verify and restore the original file:
try crypt.setVerifyCert(cert);
in_file = output_file;
output_file = "/Users/chilkat/testData/pdf/restored.pdf";
crypt.verifyP7M(in_file, output_file) catch {
std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
return;
};
std.debug.print("Success!\n", .{});
}