Javascript 的 EMVCo TLV 字符串解析器库
EMVCo TLV string parser library for Javascript
我想解析一个TLV字符串,在给标签值的时候得到值
样本:
000201021642333310011114235204546757034445802IN5910My
Company6005Vegas6304bb02
根据 TLV 规则,标签 52 将给出值 5467,57 将为 333。
这不是 EMV 使用的 BER-TLV 编码示例。您的数据示例将标记值表示为 String/ASCII 而不是二进制。
因此您需要自己的数据解析器,其中:
- 标签 - 2 个字符,这些是自定义标签,不是 EMV;
- 长度 - 十进制,2 个字符;
- 值 - ASCII 中提到的十进制长度;
这种格式比真正的BER TLV更简单,但需要开发自己的功能。
拆分结果为:
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
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});
}
基于手动EMVco解析,例如我有一个QR内容(QR Indonesia Standard):00020101021226680016ID.CO.TELKOM.WWW011893600898025599662702150001952559966270303UMI51440014ID.CO.QRIS.WWW0215ID10200211817450303UMI520457325303360540825578.005502015802ID5916InterActive Corp6013KOTA SURABAYA61056013662130509413255111630439B7
所以我们可以这样解析它:
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)
结果将是一个对象数组,如下所示:
[
{ 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' }
]
我想解析一个TLV字符串,在给标签值的时候得到值
样本:
000201021642333310011114235204546757034445802IN5910My Company6005Vegas6304bb02
根据 TLV 规则,标签 52 将给出值 5467,57 将为 333。
这不是 EMV 使用的 BER-TLV 编码示例。您的数据示例将标记值表示为 String/ASCII 而不是二进制。
因此您需要自己的数据解析器,其中:
- 标签 - 2 个字符,这些是自定义标签,不是 EMV;
- 长度 - 十进制,2 个字符;
- 值 - ASCII 中提到的十进制长度;
这种格式比真正的BER TLV更简单,但需要开发自己的功能。
拆分结果为:
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
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});
}
基于手动EMVco解析,例如我有一个QR内容(QR Indonesia Standard):00020101021226680016ID.CO.TELKOM.WWW011893600898025599662702150001952559966270303UMI51440014ID.CO.QRIS.WWW0215ID10200211817450303UMI520457325303360540825578.005502015802ID5916InterActive Corp6013KOTA SURABAYA61056013662130509413255111630439B7
所以我们可以这样解析它:
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)
结果将是一个对象数组,如下所示:
[
{ 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' }
]