Sample code for 30+ languages & platforms
Zig

ScMinidriver - Get Smartcard Properties

See more ScMinidriver Examples

Gets information about the smart card currently inserted in the reader (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 PCSC List Readers or PCSC 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;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);

    scmd.getCardProperties(json) catch {
        std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
        return;
    };

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

    // Here is sample output:
    // See below for sample code to parse the JSON.
    //
    // NOTE: Not all smart cards support all properties.
    // The GetCardProperties method will return information for all properties supported by the card.

    // {
    //   "bytesAvailable": 74752,
    //   "containersAvailable": 18,
    //   "maxKeyContainers": 20,
    //   "capabilities": {
    //     "fCertificateCompression": false,
    //     "fKeyGen": true
    //   },
    //   "keySizes": {
    //     "minimumBitlen": 2048,
    //     "defaultBitlen": 2048,
    //     "maximumBitlen": 4096,
    //     "incrementalBitlen": 1024
    //   },
    //   "readOnly": false,
    //   "cacheMode": "global",
    //   "supportsWinX509Enrollment": true,
    //   "guid": "1201803d-03e6-772a-3d80-0112e6032a77",
    //   "serial": "e6032a773d800112e6032a77",
    //   "pins": [
    //     "user",
    //     "admin",
    //     "role3",
    //     "role4"
    //   ],
    //   "pin": {
    //     "user": {
    //       "type": "regular",
    //       "purpose": "primaryCard",
    //       "changePermission": [
    //         "user"
    //       ],
    //       "unblockPermission": [
    //         "admin"
    //       ],
    //       "cachePolicy": "normal",
    //       "policyInfo": "00000000",
    //       "flags": "00000000",
    //       "acceptPlainTextPin": true,
    //       "generateSessionPin": false
    //     },
    //     "admin": {
    //       "type": "challenge/response",
    //       "purpose": "administrator",
    //       "changePermission": [
    //         "admin"
    //       ],
    //       "cachePolicy": "normal",
    //       "policyInfo": "00000000",
    //       "flags": "00000000"
    //     },
    //     "role3": {
    //       "type": "regular",
    //       "purpose": "digitalSignature",
    //       "changePermission": [
    //         "role3"
    //       ],
    //       "unblockPermission": [
    //         "role4"
    //       ],
    //       "cachePolicy": "alwaysPrompt",
    //       "policyInfo": "00000000",
    //       "flags": "00000000",
    //       "acceptPlainTextPin": true,
    //       "generateSessionPin": false
    //     },
    //     "role4": {
    //       "type": "regular",
    //       "purpose": "unblockOnly",
    //       "changePermission": [
    //         "role4"
    //       ],
    //       "cachePolicy": "normal",
    //       "policyInfo": "00000000",
    //       "flags": "00000000",
    //       "acceptPlainTextPin": true,
    //       "generateSessionPin": false
    //     }
    //   },
    //   "importSupport": [
    //     "plainText",
    //     "rsaKeyest",
    //     "eccKeyest",
    //     "symmetric"
    //   ],
    //   "algorithms": [
    //     "AES"
    //   ],
    //   "paddingSchemes": [
    //     "block",
    //     "pkcs1",
    //     "pss",
    //     "oaep"
    //   ],
    //   "chainingModes": [
    //     "ChainingModeCBC"
    //   ]
    // }

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var str_val: [:0]const u8 = "";

    _ = json.intOf("bytesAvailable");
    _ = json.intOf("containersAvailable");
    _ = json.intOf("maxKeyContainers");
    _ = json.boolOf("capabilities.fCertificateCompression");
    _ = json.boolOf("capabilities.fKeyGen");
    _ = json.intOf("keySizes.minimumBitlen");
    _ = json.intOf("keySizes.defaultBitlen");
    _ = json.intOf("keySizes.maximumBitlen");
    _ = json.intOf("keySizes.incrementalBitlen");
    _ = json.boolOf("readOnly");
    _ = try json.stringOf(alloc, "cacheMode");
    _ = json.boolOf("supportsWinX509Enrollment");
    _ = try json.stringOf(alloc, "guid");
    _ = try json.stringOf(alloc, "serial");
    _ = try json.stringOf(alloc, "pin.user.type");
    _ = try json.stringOf(alloc, "pin.user.purpose");
    _ = try json.stringOf(alloc, "pin.user.cachePolicy");
    _ = try json.stringOf(alloc, "pin.user.policyInfo");
    _ = try json.stringOf(alloc, "pin.user.flags");
    _ = json.boolOf("pin.user.acceptPlainTextPin");
    _ = json.boolOf("pin.user.generateSessionPin");
    _ = try json.stringOf(alloc, "pin.admin.type");
    _ = try json.stringOf(alloc, "pin.admin.purpose");
    _ = try json.stringOf(alloc, "pin.admin.cachePolicy");
    _ = try json.stringOf(alloc, "pin.admin.policyInfo");
    _ = try json.stringOf(alloc, "pin.admin.flags");
    _ = try json.stringOf(alloc, "pin.role3.type");
    _ = try json.stringOf(alloc, "pin.role3.purpose");
    _ = try json.stringOf(alloc, "pin.role3.cachePolicy");
    _ = try json.stringOf(alloc, "pin.role3.policyInfo");
    _ = try json.stringOf(alloc, "pin.role3.flags");
    _ = json.boolOf("pin.role3.acceptPlainTextPin");
    _ = json.boolOf("pin.role3.generateSessionPin");
    _ = try json.stringOf(alloc, "pin.role4.type");
    _ = try json.stringOf(alloc, "pin.role4.purpose");
    _ = try json.stringOf(alloc, "pin.role4.cachePolicy");
    _ = try json.stringOf(alloc, "pin.role4.policyInfo");
    _ = try json.stringOf(alloc, "pin.role4.flags");
    _ = json.boolOf("pin.role4.acceptPlainTextPin");
    _ = json.boolOf("pin.role4.generateSessionPin");
    var i: i32 = 0;
    var count_i: i32 = json.sizeOfArray("pins");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "pins[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("pin.user.changePermission");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "pin.user.changePermission[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("pin.user.unblockPermission");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "pin.user.unblockPermission[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("pin.admin.changePermission");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "pin.admin.changePermission[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("pin.role3.changePermission");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "pin.role3.changePermission[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("pin.role3.unblockPermission");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "pin.role3.unblockPermission[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("pin.role4.changePermission");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "pin.role4.changePermission[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("importSupport");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "importSupport[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("algorithms");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "algorithms[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("paddingSchemes");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "paddingSchemes[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("chainingModes");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "chainingModes[i]");
        i = i + 1;
    }

    // Delete the context when finished with the card.
    scmd.deleteContext() catch {
        std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
    };
}