EMVCo TLV string parser library for Javascript

1.6k Views Asked by At

I want to parse a TLV string and get the value when I give the tag value.

Sample:

000201021642333310011114235204546757034445802IN5910My Company6005Vegas6304bb02

Based on TLV rules Tag 52 will give value 5467, 57 will be 333.

3

There are 3 best solutions below

0
On BEST ANSWER
var str = '000201021642333310011114235204546757034445802IN5910My Company6005Vegas6304bb02';
var headerLength = 6; // assuming header length is always 6
var header = str.substring(0, 6);

var tags = [];

// start after header
var i = 6;
while (i < str.length) {
    var tag = str.substring(i, i + 2);
    i += 2;
    var valueLength = Number(str.substring(i, i + 2));
    i += 2;
    var value = str.substring(i, i + valueLength);
    i += valueLength;
    tags.push({tag: tag, value: value});
}
1
On

This is not BER-TLV encoding example as used by EMV. Your data example represent Tag Values as String/ASCII instead Binary.

So you need own data parser where:

  • Tag - 2 characters, these are customized tags, not EMV;
  • Length - in Decimal, 2 characters;
  • Value - in ASCII with mentioned decimal length;

This format more simple comparing to real BER TLV, but you need to develop own function.

The result of splitting will be:

000201 // some header
02 16 4233331001111423
52 04 5467
57 03 444
58 02 IN
59 10 My Company
60 05 Vegas
63 04 bb02
0
On

Based on manual EMVco parsing, for example I have a QR content (QR Indonesia Standard): 00020101021226680016ID.CO.TELKOM.WWW011893600898025599662702150001952559966270303UMI51440014ID.CO.QRIS.WWW0215ID10200211817450303UMI520457325303360540825578.005502015802ID5916InterActive Corp6013KOTA SURABAYA61056013662130509413255111630439B7 so we can parse it like this:

var qris = '00020101021226680016ID.CO.TELKOM.WWW011893600898025599662702150001952559966270303UMI51440014ID.CO.QRIS.WWW0215ID10200211817450303UMI520457325303360540825578.005502015802ID5916InterActive Corp6013KOTA SURABAYA61056013662130509413255111630439B7'

var tags = [];

var i = 0;
while (i < qris.length) {
    var tag = qris.substring(i, i + 2);
    i += 2;
    var valueLength = Number(qris.substring(i, i + 2));
    i += 2;
    var value = qris.substring(i, i + valueLength);
    i += valueLength;
    tags.push({tag: tag, length: valueLength, value: value});
}

console.log(tags)

The result will be an array of objects as following:

[
  { tag: '00', length: 2, value: '01' },
  { tag: '01', length: 2, value: '12' },
  {
    tag: '26',
    length: 68,
    value: '0016ID.CO.TELKOM.WWW011893600898025599662702150001952559966270303UMI'
  },
  {
    tag: '51',
    length: 44,
    value: '0014ID.CO.QRIS.WWW0215ID10200211817450303UMI'
  },
  { tag: '52', length: 4, value: '5732' },
  { tag: '53', length: 3, value: '360' },
  { tag: '54', length: 8, value: '25578.00' },
  { tag: '55', length: 2, value: '01' },
  { tag: '58', length: 2, value: 'ID' },
  { tag: '59', length: 16, value: 'InterActive Corp' },
  { tag: '60', length: 13, value: 'KOTA SURABAYA' },
  { tag: '61', length: 5, value: '60136' },
  { tag: '62', length: 13, value: '0509413255111' },
  { tag: '63', length: 4, value: '39B7' }
]