Skip to main content

Pure Python X.690 implementation

Project description

Pure Python X.690 implementation

Build & Publish Docs

This module contains a pure Python implementation of the “x690” standard for BER encoding/decoding. Other encodings are currently unsupported but pull-requests are welcome.

Type Extensions

It allows defining and detecting new data-types by simply subclassing the base class x690.types.Type. An example for this can be seen in puresnmp

Examples

Encoding to bytes

Encoding to bytes can be done by simply calling the Python builting bytes() on instances from x690.types:

Encoding of a single value

import x690.types as t

>>> myvalue = t.Integer(12)
>>> asbytes = bytes(myvalue)
>>> repr(asbytes)
b'\x02\x01\x0c'

Encoding of a composite value using Sequence

import x690.types as t

>>> myvalue = t.Sequence(
...     t.Integer(12),
...     t.Integer(12),
...     t.Integer(12),
... )
>>> asbytes = bytes(myvalue)
>>> repr(asbytes)
b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0c'

Decoding from bytes

Decode bytes by calling x690.types.pop_tlv on your byte data. This will return a tuple where the first value contains the decoded object, and the second one will contain any remaining bytes which were not decoded.

import x690.types as t
>>> data = b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0c'
>>> decoded, remaining_bytes = t.pop_tlv(data)
>>> decoded
Sequence(Integer(12), Integer(12), Integer(12))
>>> remaining_bytes
b''

Type-Hinting & Enforcing

New in 0.3.0

When decoding bytes, it is possible to specify an expcted type which does two things: Firstly, it tells tools like mypy what the return type will be and secondly, it runs an internal type-check which ensures that the returned value is of the expected type. x690.exc.UnexpectedType is raised otherwise.

This does of course only work if you know the type in advance.

import x690.types as t
>>> data = b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0c'
>>> decoded, remaining_bytes = t.pop_tlv(data, enforce_type=t.Sequence)
>>> decoded
Sequence(Integer(12), Integer(12), Integer(12))
>>> remaining_bytes
b''

Strict Decoding

New in 0.3.0

When decoding using pop_tlv and you don’t expect any remaining bytes, use strict=True which will raise x690.exc.IncompleteDecoding if there’s any remaining data.

import x690.types as t
>>> data = b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0cjunk-bytes'
>>> decoded, remaining_bytes = t.pop_tlv(data, strict=True)
Traceback (most recent call last):
  ...
x690.exc.IncompleteDecoding: Strict decoding still had 10 remaining bytes!

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

x690-0.3.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

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

x690-0.3.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file x690-0.3.0.tar.gz.

File metadata

  • Download URL: x690-0.3.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

File hashes

Hashes for x690-0.3.0.tar.gz
Algorithm Hash digest
SHA256 792988f5dcd7066e040b15a3772db59a2255055b5c260b0edfb7d042de1d41d3
MD5 fcfe71947d4c393b9d87d57688094145
BLAKE2b-256 f238b79cd8eb26fb76268c97253cf8746bc344a8c4bed5b04140d946ac946bae

See more details on using hashes here.

File details

Details for the file x690-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: x690-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

File hashes

Hashes for x690-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ede73981496e0f93db3c93c95183f775d744b58eb5f08b74dfecf93fa10eca0f
MD5 a7f1baad5260f56722e818c5bf05a4e7
BLAKE2b-256 d0232ab3376bf7cd76f7457ba0c2eede2d6ea0e9a02e980e82cb2e445102f79e

See more details on using hashes here.

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