Sample code for 30+ languages & platforms
Zig

Iterate over Certificates on Connected Smart Cards and USB Tokens

See more Cert Store Examples

Demonstrates how to iterate over the certificates on connected smart cards and USB tokens.

Note: Requires Chilkat v10.1.2 or later.

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 cert_store = try chilkat.CertStore.init();
    defer cert_store.deinit();

    // Detects connected smart cards and USB tokens
    // and loads the certificate store object with the certificates found.
    // Works on Windows, MacOS, iOS, and Linux.
    // Note: If something is not detected, contact info@chilkatsoft.com
    // for help or for workarounds.
    const arg_not_used = "";
    cert_store.openSmartcard(arg_not_used) catch {
        std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
        return;
    };

    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    const num_certs = cert_store.getNumCertificates();
    var i: i32 = 0;

    while (i < num_certs) {
        // Load the cert object with the Nth certificate found in the connected HSMs
        cert_store.getCert(i, cert) catch {};
        std.debug.print("{d}: {s}\n", .{ i, try cert.getSubjectCN(alloc) });
        i = i + 1;
    }
}