Friday, November 2, 2012

PCSC sample in lua

To continue the list of PC/SC wrapper initiated more than two years ago with "PC/SC sample in different languages" I now present a PC/SC sample in lua.

In a previous blog article "cardpeek: A tool to read the contents of smartcards" I presented an application using the lua language to interact with smart cards.

PC/SC access from lua

Cardpeek is a complete application and not a PC/SC wrapper. The cardpeek author implemented only what was needed for the application instead of a general PC/SC wrapper.

The card functions available from lua are:
  • connect
  • disconnect
  • warm_reset
  • last_atr
  • info
  • send
  • set_command_interval
  • make_file_path

One important missing function is an equivalent of SCardListReaders(). The list of available readers and selection of the reader to use is not available from the lua code. The reader selection is done by the cardpeek application using C code directly.

Some work is missing in order to have a complete PC/SC wrapper for lua.

Source code

function hex_tostring(data)
 local r = ""
 local i
 for i=0,#data-1 do
  r = r .. string.char(data[i]) 
 return r

if card.connect() then

 -- Select applet
 select =, "00 A4 04 00 0A A0 00 00 00 62 03 01 0C 06 01")
 sw, resp = card.send(select)
 print(string.format("SW: %X", sw))

 -- Send command
 command =, "00 00 00 00")
 sw, resp = card.send(command)
 print(string.format("SW: %X", sw))



The output is sent to the console using the lua print() statement. You could also send the output to cardpeek using log.print() instead.


In the console

SW: 9000

SW: 9000
Hello world!

In cardpeek


I reported some improvements for cardpeek (issues 13, 14, 15 and 16). One month after, the issues are still open and the proposed patches not applied or reviewed. A bad news concerning the state of the cardpeek project :-(.

This pseudo-wrapper is the 12th wrapper for PC/SC. What will be the next language?