Sample code for 30+ languages & platforms
Zig

Add S/MIME Signature using PFX

See more MIME Examples

Add a digital signature to a MIME message using the certificate + private key from a PFX file.

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 requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const mime = try chilkat.Mime.init();
    defer mime.deinit();

    // Load a PFX file into a certificate object.
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    const pfx_filepath = "pfxFiles/something.pfx";
    const pfx_password = "secret";
    cert.loadPfxFile(pfx_filepath, pfx_password) catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    try mime.setBodyFromPlainText("This is the plain-text MIME body.");

    mime.setCharset("utf-8");
    mime.setEncoding("quoted-printable");

    // Sign the MIME (adds a PKCS7 detached signature)
    mime.addDetachedSignature(cert) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    // Save the S/MIME to a file.
    mime.saveMime("/temp/signedMime.txt") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("success!\n", .{});
}