A Kotlin library for parsing and building BER TLV (Basic Encoding Rules Tag-Length-Value) data structures, commonly used in payment processing and EMV applications.
- Parse BER TLV data from byte arrays
- Build TLV structures programmatically
- Support for both primitive and constructed TLV elements
- Comprehensive search and navigation capabilities
- Hexadecimal utility functions
- Extensive logging support for debugging
How to change the version: Change 1.0.2 in the snippets above to any release or tag from this repository.
repositories {
maven { url = uri("https://jitpack.io") }
}
dependencies {
implementation("com.github.bsreeram08:Ber-TLV-Kotlin:1.0.2")
}repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.bsreeram08:Ber-TLV-Kotlin:1.0.2'
}<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.bsreeram08</groupId>
<artifactId>Ber-TLV-Kotlin</artifactId>
<version>1.0.2</version>
</dependency>Download the latest JAR from Releases and add it to your project's classpath.
import com.sreeram.tlv.*
// Parse from hex string
val hexData = "500456495341"
val bytes = HexUtil.parseHex(hexData)
val parser = BerTlvParser()
val tlvs = parser.parse(bytes)
// Find specific tags
val applicationLabel = tlvs.find(BerTag(0x50))
println("Application Label: ${applicationLabel?.textValue}") // "VISA"import com.sreeram.tlv.*
val builder = BerTlvBuilder()
.addText(BerTag(0x50), "VISA")
.addHex(BerTag(0x9F, 0x02), "000000001000")
.addAmount(BerTag(0x9F, 0x02), BigDecimal("10.00"))
val tlv = builder.buildTlv()
val bytes = builder.buildArray()// Create tags
val tag1 = BerTag(0x50) // Single byte tag
val tag2 = BerTag(0x9F, 0x02) // Multi-byte tag
val tag3 = BerTag("9F02") // From hex string
// Search operations
val found = tlvs.find(BerTag(0x50)) // Find first occurrence
val allFound = tlvs.findAll(BerTag(0x50)) // Find all occurrences
// Navigate constructed TLVs
if (tlv.isConstructed) {
val children = tlv.values
children.forEach { child ->
println("Child tag: ${child.tag}")
}
}Represents a single TLV element.
Properties:
tag: BerTag- The tagisPrimitive: Boolean- Whether this is a primitive elementisConstructed: Boolean- Whether this contains other TLV elementshexValue: String- Value as hex string (primitive only)textValue: String- Value as ASCII text (primitive only)bytesValue: ByteArray- Raw byte value (primitive only)intValue: Int- Value as integer (primitive only)values: List<BerTlv>- Child elements (constructed only)
Methods:
find(tag: BerTag): BerTlv?- Find first child with tagfindAll(tag: BerTag): List<BerTlv>- Find all children with tag
Container for multiple TLV elements.
Properties:
list: List<BerTlv>- All TLV elements
Methods:
find(tag: BerTag): BerTlv?- Find first element with tagfindAll(tag: BerTag): List<BerTlv>- Find all elements with tag
Parses byte arrays into TLV structures.
Methods:
parse(buf: ByteArray): BerTlvs- Parse multiple TLVsparseConstructed(buf: ByteArray): BerTlv- Parse single constructed TLV
Builds TLV structures programmatically.
Methods:
addText(tag: BerTag, text: String): BerTlvBuilderaddHex(tag: BerTag, hex: String): BerTlvBuilderaddBytes(tag: BerTag, bytes: ByteArray): BerTlvBuilderaddAmount(tag: BerTag, amount: BigDecimal): BerTlvBuilderaddDate(tag: BerTag, date: Date): BerTlvBuilderaddTime(tag: BerTag, date: Date): BerTlvBuilderbuildTlv(): BerTlvbuildArray(): ByteArray
Utility functions for hexadecimal operations.
Methods:
parseHex(hex: String): ByteArray- Convert hex string to bytestoHexString(bytes: ByteArray): String- Convert bytes to hex string
- Kotlin 1.9.0 or higher
- Java 21 or higher
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Based on the original Java implementation from PaynetEasy's TLV library, converted to Kotlin with improvements and modernizations.