Skip to main content

Python BER-TLV parser/encoder for EMV & smartcard data

Project description

TLV-for-Everyone

A small, Python-based BER-TLV (Tag-Length-Value) parser and encoder, designed for EMV and smartcard-style data structures.
It supports:

  • Parsing TLV streams into a tree structure
  • Constructed vs primitive tags
  • Long-form and short-form lengths
  • Nested TLVs inside constructed tags
  • Round-trip encode/decode
  • Case-insensitive tag lookup with simple path syntax (find("9F02"), find("A0/5F2A"))
  • Dummy-root wrapper (FF) for multiple top-level siblings

Installation

pip install tlv-for-everyone

Usage

from TlvParser.TlvParser import BerTlvParser, BerTlvElement

# Parse an EMV-like TLV string
data = "9F02060000000001009F1A0208405F2A020978"
parser = BerTlvParser()
tlv = parser.parse_tlv(data)

# Pretty-print as dict
print(tlv.get_as_dict())

# Find a tag
val = tlv.find("9F02").get_value_as_hex_str()
print("Amount:", val)

Creating TLVs

# Construct a TLV structure programmatically
parent = BerTlvElement(0xA0, {})
child1 = BerTlvElement(0x9A, bytes.fromhex("250826"))
child2 = BerTlvElement(0x5F2A, bytes.fromhex("0124"))
parent.add_child(child1)
parent.add_child(child2)

# Encode to bytes
encoded = parent.encode().hex().upper()
print("Encoded:", encoded)

Running Tests

This project uses pytest and hypothesis.

pip install -r requirements-dev.txt

Run all tests:

pytest -v

Development Roadmap

  • Improve XML/JSON exporters
  • Add support for indefinite-length encoding
  • More robust error handling and diagnostics
  • PyPI packaging

Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you’d like to change.

License

MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tlv_for_everyone-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tlv_for_everyone-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file tlv_for_everyone-0.1.0.tar.gz.

File metadata

  • Download URL: tlv_for_everyone-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tlv_for_everyone-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e87b56391cf618cbc5791bdaf704ca65d71cae50547df179debb128b99922f91
MD5 06f84d4c5638914dca157e7875a133f5
BLAKE2b-256 9ce7edf3f99d71419d8b30653d7bebfd41ea0a69cf5aaa87b6dba29508884894

See more details on using hashes here.

Provenance

The following attestation bundles were made for tlv_for_everyone-0.1.0.tar.gz:

Publisher: publish.yml on vitalij555/tlv-for-everyone

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tlv_for_everyone-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tlv_for_everyone-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b98c21c15d85701600a6122d6ff1e8358c858de699be29fa256fd2eb220a27c6
MD5 01ce58cd3868c5d6a29b071b430f0cc2
BLAKE2b-256 ef4ff5369315cc428aef97339f1eeac593a1b8d9bb49a3266720f9aa68dd69fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for tlv_for_everyone-0.1.0-py3-none-any.whl:

Publisher: publish.yml on vitalij555/tlv-for-everyone

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page