Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.4.0+

Example: Mime.GetDecryptCertInfo method

Demonstrates the GetDecryptCertInfo method.

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();

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

    // Load MIME that is has Content-Type like this:
    // Content-Type: application/pkcs7-mime; smime-type="enveloped-data"; name="smime.p7m"; smime-type="enveloped-data"
    mime.loadMimeFile("qa_data/mime/enveloped_data.eml") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    // Get information about the certificate that would be needed to decrypt.
    // An enveloped-data can potentially be decrypted by multiple certificates if it was encrypted in a way that allows it,
    // but in most cases, only a single certificate with associated private key (that of the message recipient) is possible.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    mime.getDecryptCertInfo(json) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Sample output:

    // {
    //   "recipientInfo": [
    //     {
    //       "serial": "****",
    //       "issuerCN": "****"
    //     }
    //   ]
    // }

    // Get each certificate's information like this:
    var serial: [:0]const u8 = "";
    var issuer_cn: [:0]const u8 = "";

    var i: i32 = 0;
    const count = json.sizeOfArray("recipientInfo");
    while (i < count) {
        json.setI(i);
        serial = try json.stringOf(alloc, "recipientInfo[i].serial");
        issuer_cn = try json.stringOf(alloc, "recipientInfo[i].issuerCN");
        i = i + 1;
    }
}