Sample code for 30+ languages & platforms
Zig

CAdES BES Detached Signature

See more Encryption Examples

Demonstrates how to create a CAdES BES detached signature file (.p7s).

Chilkat Zig Downloads

Zig
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.

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();

    // Use a digital certificate and private key from a PFX file (.pfx or .p12).
    const pfx_path = "/Users/chilkat/testData/pfx/acme.pfx";
    const pfx_password = "test123";

    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadPfxFile(pfx_path, pfx_password) catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // Tell the crypt component to use this cert.
    crypt.setSigningCert(cert) catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    // The CadesEnabled property applies to all methods that create PKCS7 signatures.
    // To create a CAdES-BES signature, set this property equal to true.
    crypt.setCadesEnabled(true);

    // 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";

    // Create the detached CAdES-BES signature:
    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", .{});
}