After a number of posts describing how to decode audio streams of magnetic stripes to bits and then characters, I now present a C library that does all that.
The creatively named “mslib” can take a signed 16bit little endian PCM stream of a magnetic stripe and decode it.
My own usage tests have been with 48khz, but it should allow higher frequencies and slightly lower ones. When changing the frequency from 48khz, it may be required to alter the “peakOffset” and “peakThreshold” values using the functions defined and documented in mslib.h.
The code is written in C and available on github here. It’s presently released under the GPLv3 and has a single library dependency of glib-2.0. [Edit: No longer has a glib dependency — should be entirely c99 compatible]
The library is tersely documented like most open source code, but comes with an example/test utility. Decoding a stream consists of the follow steps:
- Create the mslib object and load the PCM stream as a signed array of short integers using ms_create().
- Create a list of peaks, used for decoding the stream to bits, using ms_peaks_find().
- Filter the list of peaks for duplicates using ms_peaks_filter().
- Decode the peaks to bits using ms_decode_peaks(). The bits as written on the card may now be fetched as a NULL terminated char array with ms_get_bitStream().
- Decode the bits to characters using ms_decode_bits(). The characters as written on the card may now be fetched as a NULL terminated char array with ms_get_charStream().
The library can successfully decode any magnetic stripe to bits (subject to a clean audio signal). It will decode the bits to characters for streams encoded using the ABA and IATA encoding schemes as found on almost all magnetic stripes.
Development is still continuing, with a top TODO item of removing the glib dependency [Edit: glib has been removed in current builds]. Bug reports, comments and feature requests are welcome and can be left on github’s site.
Some example PCM streams that can be decoded with the utility:
capitalone.pcm – A Visa credit card
wamu.pcm – A Visa debit card
wamu-bad.pcm – An example of a bad swipe of the Visa debit card.
(All credit card numbers are of course no longer valid [not just expired]. All the streams are from track 2 and use ABA encoding)