Sample code for 30+ languages & platforms
Zig

ScMinidriver - Change Smart Card PIN (or USB token PIN)

See more ScMinidriver Examples

Demonstrates how to change the PIN for a smart card or USB token.

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

    // Reader names (smart card readers or USB tokens) can be discovered
    // via List Readers or Find Smart Cards
    const reader_name = "Alcor Micro USB Smart Card Reader 0";
    scmd.acquireContext(reader_name) catch {
        std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
        return;
    };

    // If successful, the name of the currently inserted smart card is available:
    std.debug.print("Card name: {s}\n", .{try scmd.getCardName(alloc)});

    // Change the "user" PIN.  (Typically, you'll always be using the "user" PIN.)
    const current_pin = "0000";
    const new_pin = "1234";
    const retval = scmd.pinChange("user", current_pin, new_pin);
    if (retval == -1) {
        std.debug.print("The PIN is already blocked.\n", .{});
        return;
    }

    if (retval == -2) {
        std.debug.print("The PinChange function failed for some unanticipated reason\n", .{});
        std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
        return;
    }

    if (retval == 0) {
        std.debug.print("PIN successfully changed.\n", .{});
    } else {
        std.debug.print("Current PIN is incorrect.\n", .{});
        std.debug.print("Number of attempts remaining = {d}\n", .{retval});
    }

    scmd.deleteContext() catch {};
}