๐ Language: English ยท เนเธเธข
libthai-idcard
A Rust library for reading Thai National ID smart cards via PC/SC smart card readers, with C, Go, Python, and Ruby usage examples.
Overview
Thai National ID smart cards store citizen identification data, a JPEG face photo, NHSO (National Health Security Office) insurance information, and a laser-engraved card serial number. This library handles the low-level APDU communication, TIS-620 (Thai) text decoding, and Buddhist-to-Gregorian date conversion, exposing the data through a clean multi-language API.
Features
- โ Read citizen ID, name (Thai & English), date of birth, gender
- โ Read registered address (parsed into components)
- โ Read card issuer, issue date, expiry date
- โ Read JPEG face photo (returned as base64)
- โ Read laser-engraved card serial number
- โ Read NHSO insurance data (main/sub hospitals, coverage dates, etc.)
- โ Buddhist year โ Gregorian calendar conversion
- โ TIS-620 (Windows-874) Thai text decoding
- โ Auto-detect card reader or specify by name
- โ Daemon mode for continuous card monitoring
- โ C usage example (dynamic loading or link-time)
- โ
Go usage example (via
cgo) - โ
Python usage example (via
ctypes) - โ
Ruby usage example (via
fiddle)
Requirements
- Hardware: A PC/SC-compatible smart card reader and a Thai National ID card
- Software: PC/SC Lite (
pcsclite) โ installed by default on macOS and most Linux distributions- macOS: Built-in (
PCSC.framework) - Linux:
sudo apt install libpcsclite-dev(Debian/Ubuntu) orsudo dnf install pcsc-lite-devel(Fedora) - Windows: Winscard (built-in)
- macOS: Built-in (
Usage
Rust
use ;
let card = new;
let data = card.read.unwrap;
let personal = data.personal.unwrap;
println!;
println!;
println!;
Or with a specific reader and extra data sections:
use ;
let opts = Options ;
let card = new;
let data = card.read.unwrap;
See examples/rust_usage.rs for a complete example.
C
A C usage example is available at examples/c_usage.c.
It demonstrates both dynamic loading (dlopen/dlsym) and compile-time
linking against the shared library.
# Dynamic loading (no linker flags)
# Compile-time linking
Go
A Go usage example is available at
examples/go_usage.go.
It uses cgo to load the shared library via dlopen and read card data.
Python
A Python usage example is available at
examples/python_usage.py.
It uses ctypes to load the shared library and read card data.
Ruby
A Ruby usage example is available at
examples/ruby_usage.rb.
It uses fiddle (Ruby's built-in FFI library) to load the shared library
and read card data.
Building
# Build all Rust targets (library + examples)
# Build only the native shared library (.dylib/.so)
# Generate C header file (requires cbindgen)
# Run the Rust usage example
# Run the C example
# Run the Go example
# Run the Python example
# Run the Ruby example
Cross-compilation
All cross-compilation targets use Docker (except build-win-native* which
require a locally installed mingw-w64). macOS builds use osxcross to
provide the Apple SDK and toolchain inside the Linux-based Docker image.
# Build Linux .so via Docker
# Build macOS .dylib via Docker using osxcross (Apple Silicon)
# Build macOS .dylib via Docker using osxcross (Intel)
# Build Windows DLL via Docker
# Build Windows DLL natively (requires mingw-w64)
# Build Windows DLL natively in release mode
Or directly with Cargo:
Data Model
CardData
โโโ personal: Personal
โ โโโ cid โ 13-digit citizen ID
โ โโโ name โ Full name in Thai (with prefix, first, middle, last)
โ โโโ name_en โ Full name in English
โ โโโ dob โ Date of birth (YYYY-MM-DD)
โ โโโ gender โ M or F
โ โโโ card_issuer โ Issuing authority
โ โโโ issue_date โ Card issue date (YYYY-MM-DD)
โ โโโ expire_date โ Card expiry date (YYYY-MM-DD)
โ โโโ address โ Registered address (parsed into components)
โ โโโ face_image โ Face photo as base64 JPEG
โโโ card: Card
โ โโโ laser_id โ Laser-engraved serial number
โโโ nhso: Nhso
โโโ main_inscl โ Main insurance scheme
โโโ sub_inscl โ Sub insurance scheme
โโโ main_hospital โ Primary hospital
โโโ sub_hospital โ Secondary hospital
โโโ paid_type โ Payment type
โโโ issue_date โ NHSO coverage start date
โโโ expire_date โ NHSO coverage end date
โโโ update_date โ Last update date
โโโ change_hospital_amount โ Hospital change count
Project Structure
โโโ Cargo.toml
โโโ Makefile
โโโ Dockerfile.build โ Cross-compilation Docker image (osxcross)
โโโ src/
โ โโโ lib.rs โ Main API (SmartCard, start_daemon)
โ โโโ ffi.rs โ C-compatible FFI exports
โ โโโ model.rs โ Data types & parsing helpers
โ โโโ apdu.rs โ APDU command constants
โ โโโ reader.rs โ Low-level PC/SC operations
โ โโโ personal.rs โ Personal data reader
โ โโโ nhso.rs โ NHSO data reader
โ โโโ laser.rs โ Laser ID reader
โ โโโ options.rs โ Configuration options
โโโ examples/
โโโ rust_usage.rs โ Rust usage example
โโโ c_usage.c โ C usage example
โโโ go_usage.go โ Go usage example
โโโ python_usage.py โ Python usage example
โโโ ruby_usage.rb โ Ruby usage example
References
License
Licensed under either of:
- MIT license (LICENSE-MIT)
- Apache License, Version 2.0 (LICENSE-APACHE)
at your option.