Zig Requires Chilkat v10.1.2+
Zig
Sign a File to Create a .p7s (Detached Signature)
See more Encryption Examples
_LANGUAGE_ example to create a detached signature file (.p7s) for any type file. The signature can be verified by calling VerifyP7S and passing the original filename and the .p7s filename.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 .p7s as output:
const in_file = "/Users/chilkat/testData/pdf/sample.pdf";
const sig_file = "/Users/chilkat/testData/p7s/sample.p7s";
crypt.createP7S(in_file, sig_file) catch {
std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
return;
};
crypt.verifyP7S(in_file, sig_file) catch {
std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
return;
};
std.debug.print("Success!\n", .{});
}