initial example

This commit is contained in:
Lance Vick 2023-11-01 14:55:00 -07:00
commit 67cd263c69
Signed by: lrvick
GPG Key ID: 8E47A1EC35A1551D
4 changed files with 112 additions and 0 deletions

39
examples/rust_pcsc_static/Cargo.lock generated Normal file
View File

@ -0,0 +1,39 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "pcsc"
version = "2.8.0"
source = "git+https://github.com/bluetech/pcsc-rust?rev=9d7c1645a7fd9639d0585f3a4cc698b54d9746e5#9d7c1645a7fd9639d0585f3a4cc698b54d9746e5"
dependencies = [
"bitflags",
"pcsc-sys",
]
[[package]]
name = "pcsc-sys"
version = "1.2.0"
source = "git+https://github.com/bluetech/pcsc-rust?rev=9d7c1645a7fd9639d0585f3a4cc698b54d9746e5#9d7c1645a7fd9639d0585f3a4cc698b54d9746e5"
dependencies = [
"pkg-config",
]
[[package]]
name = "pcsc_demo"
version = "0.1.0"
dependencies = [
"pcsc",
]
[[package]]
name = "pkg-config"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"

View File

@ -0,0 +1,11 @@
[package]
name = "pcsc_demo"
version = "0.1.0"
authors = ["Some Person <demo@person.com>"]
[dependencies]
pcsc = { git = "https://github.com/bluetech/pcsc-rust", rev = "9d7c1645a7fd9639d0585f3a4cc698b54d9746e5" }
[[bin]]
name = "pcsc_demo"
path = "main.rs"

View File

@ -0,0 +1,18 @@
ARG LIBSTD_IMAGE=ocirep:rust-libstd-musl
ARG LIBMUSL_IMAGE=ocirep:libmusl
ARG LIBPCSCLITE_IMAGE=ocirep:libpcsclite-static
FROM ocirep:rust as build
COPY --from=${LIBSTD_IMAGE} . lib
COPY --from=${LIBMUSL_IMAGE} . lib
COPY --from=${LIBPCSCLITE_IMAGE} . lib
COPY . .
ENV PCSC_LIB_DIR='./lib'
ENV PCSC_LIB_NAME='static=pcsclite'
ENV RUSTFLAGS='-L lib -C target-feature=+crt-static'
RUN cargo build --target x86_64-unknown-linux-musl --release
FROM static
COPY --from=build target/release/pcsc_demo .

View File

@ -0,0 +1,44 @@
extern crate pcsc;
use pcsc::*;
use std::str;
fn main() {
// Establish a PC/SC context.
let ctx = Context::establish(Scope::User)
.expect("failed to establish context");
// List available readers.
let mut readers_buf = [0; 2048];
let mut readers = ctx.list_readers(&mut readers_buf)
.expect("failed to list readers");
// Use the first reader.
let reader = readers.next().ok_or(())
.expect("no readers are connected");
println!("Using reader: {:?}", reader);
// Connect to the card.
let card = ctx.connect(reader, ShareMode::Shared, Protocols::T1)
.expect("failed to connect to card");
// Send an SELECT APDU command.
let apdu = b"\x00\xA4\x04\x00\x0A\xA0\x00\x00\x00\x62\x03\x01\x0C\x06\x01";
let mut rapdu_buf = [0; MAX_BUFFER_SIZE];
let rapdu = card.transmit(apdu, &mut rapdu_buf)
.expect("failed to transmit APDU to card");
println!("{:?}", rapdu);
// Send an COMMAND APDU command.
let apdu = b"\x00\x00\x00\x00";
let mut rapdu_buf = [0; MAX_BUFFER_SIZE];
let rapdu = card.transmit(apdu, &mut rapdu_buf)
.expect("failed to transmit APDU to card");
println!("{:?}", rapdu);
// remove the extra 2 SW bytes at the end
let text = &rapdu[0 .. rapdu.len()-2];
// convert to UTF-8 (ASCII in fact)
println!("{}", str::from_utf8(&text).unwrap());
}