pcsc-nim

August 26, 2025 ยท View on GitHub

๐Ÿ”’ PC/SC bindings + idiomatic Nim wrapper

pcsc-nim is a cross-platform PC/SC library for smart card access from Nim.
It provides both low-level FFI bindings to PCSC/winscard as well as a high-level API for everyday use.

  • โœ… Linux (libpcsclite.so)
  • โœ… Windows (winscard.dll)
  • โœ… macOS (PCSC.framework)

โœจ Features

  • Establish and release PC/SC contexts
  • Enumerate available smart card readers
  • Connect to a card with T=0 or T=1 protocol
  • Send/receive raw APDUs
  • Helper utilities for hex parsing & pretty printing
  • Safe error handling with Nim exceptions

๐Ÿ“ฆ Installation

nimble install https://github.com/mmlado/pcsc-nim

Or for development:

git clone https://github.com/mmlado/pcsc-nim
cd pcsc-nim
nimble develop

๐Ÿš€ Usage

List readers & select a card

import pcsc

let ctx = establishContext()
let readers = ctx.listReaders()

if readers.len == 0:
  quit "No readers found"

echo "Readers:"
for r in readers:
  echo "  ", r

let card = ctx.connect(readers[0])

Transmit an APDU

# SELECT example AID
let resp = card.transmitHex("00 A4 04 00 08 A0 00 00 08 04 00 01 01")

echo "Response:"
echo "  ", prettyHex(resp)

let (sw1, sw2) = sw(resp)
echo "SW1SW2: 0x", (sw1.int shl 8 or sw2.int).toHex(4)

Example output:

Readers:
  Generic USB2.0-CRW [Smart Card Reader Interface]
Response:
  A4 61 8F 10 ... 90 00
SW1SW2: 0x9000

๐Ÿ›  Development

Run the test suite:

nimble test

Run examples:

nimble transmit_apdu

๐Ÿ“œ License

MIT ยฉ mmlado