{"id":3885,"library":"asn1","title":"Python-ASN1","description":"Python-ASN1 is a straightforward library for encoding and decoding ASN.1 data, compatible with Python 2.7+ and 3.5+. It supports both BER (Basic Encoding Rules) for parsing and DER (Distinguished Encoding Rules) for parsing and generation, including indefinite lengths. The library is written entirely in Python and supports most common ASN.1 types, including the REAL type. The latest version is 3.2.0, with a regular release cadence addressing new Python versions and API enhancements.","status":"active","version":"3.2.0","language":"python","source_language":"en","source_url":"https://github.com/andrivet/python-asn1","tags":["asn.1","encoding","decoding","serialization","cryptography","der","ber","x.509"],"install":[{"cmd":"pip install asn1","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for Python 2.7 and 3.x compatibility in older environments. Not strictly needed for modern Python 3.5+ installations.","package":"future","optional":true},{"reason":"Required for type hints in Python 2.7. Not needed for Python 3.5+ as 'typing' is part of the standard library.","package":"typing","optional":true}],"imports":[{"symbol":"Encoder","correct":"from asn1 import Encoder"},{"symbol":"Decoder","correct":"from asn1 import Decoder"},{"symbol":"Numbers","correct":"from asn1 import Numbers"}],"quickstart":{"code":"import asn1\n\n# 1. Encoding an ObjectIdentifier and a Sequence\nwith asn1.Encoder() as encoder:\n    # Encode an ObjectIdentifier (e.g., \"1.2.3\")\n    encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier)\n\n    # Encode a Sequence containing an Integer and a UTF8String\n    with encoder.enter(asn1.Numbers.Sequence) as sequence_encoder:\n        sequence_encoder.write(123, asn1.Numbers.Integer)\n        sequence_encoder.write('hello', asn1.Numbers.UTF8String)\n\n    encoded_bytes = encoder.output()\nprint(f\"Encoded bytes: {encoded_bytes.hex()}\")\n\n# 2. Decoding the ASN.1 data\ndecoded_elements = []\nwith asn1.Decoder(stream=encoded_bytes) as decoder:\n    # Decode the first element (ObjectIdentifier)\n    tag, value = decoder.read()\n    decoded_elements.append(f\"Tag: {tag.nr} (ObjectIdentifier), Class: {tag.cls}, Value: {value}\")\n\n    # Decode the next element, which is the Sequence\n    tag, value = decoder.read()\n    if tag.nr == asn1.Numbers.Sequence:\n        decoded_elements.append(f\"Tag: {tag.nr} (Sequence), Class: {tag.cls}\")\n        # Enter the sequence to decode its elements\n        with decoder.enter() as sequence_decoder:\n            while not sequence_decoder.eof():\n                tag, value = sequence_decoder.read()\n                decoded_elements.append(f\"  -> Tag: {tag.nr}, Class: {tag.cls}, Value: {value}\")\n\nprint(\"\\nDecoded data:\")\nfor item in decoded_elements:\n    print(item)","lang":"python","description":"This quickstart demonstrates encoding and decoding a simple ObjectIdentifier and a Sequence containing an Integer and a UTF8String. It utilizes the modern context manager API introduced in version 3.2.0 for both encoding and decoding, which simplifies resource management and constructed type handling."},"warnings":[{"fix":"Upgrade to version 3.0.1 or later. If staying on 3.0.0, explicitly specify `asn1.Encoder.DER` when initializing the encoder or `start()` method if DER is desired, or be aware that CER will be used by default without a stream, and DER with a stream.","message":"In version 3.0.0, the `Encoder` incorrectly defaulted to CER encoding, diverging from previous versions which used DER. This was a significant behavioral change for users who did not explicitly specify the encoding method.","severity":"breaking","affected_versions":"3.0.0"},{"fix":"Always explicitly specify the ASN.1 `Numbers` type (e.g., `asn1.Numbers.IA5String`) when encoding a Python value if the default mapping is not the desired ASN.1 type.","message":"Due to ASN.1 having more distinct data types than Python, a single Python type might map to multiple ASN.1 types. The library uses the most frequently expected ASN.1 type by default. If a different ASN.1 type (e.g., 'IA5String' vs. 'UTF8String' for a Python string) is intended, it must be explicitly specified during encoding using `encoder.write(value, asn1.Numbers.DesiredType)`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Migrate encoding and decoding logic to use the `with asn1.Encoder(...) as encoder:` and `with asn1.Decoder(...) as decoder:` syntax for improved readability and maintainability. Review the documentation for examples of the new API.","message":"Version 3.2.0 introduced a new, more Pythonic context manager API for `Encoder` and `Decoder` (e.g., `with asn1.Encoder() as encoder:`). While the older `encoder.start()` / `encoder.output()` methods remain backward-compatible, the context manager approach is recommended for cleaner code and proper resource handling, especially for constructed types and streams.","severity":"gotcha","affected_versions":"Prior to 3.2.0"}],"env_vars":null,"search_vec":"'2.7':20 '3.2.0':65 '3.5':22 'address':71 'api':76 'asn.1':15,55,78 'asn1':3,6 'basic':27 'ber':26,84 'cadenc':70 'common':54 'compat':17 'cryptographi':82 'data':16 'decod':14,80 'der':33,83 'distinguish':34 'encod':12,28,35,79 'enhanc':77 'entir':48 'generat':40 'includ':41,57 'indefinit':42 'latest':62 'length':43 'librari':10,45 'new':72 'pars':31,38 'python':2,5,19,50,73 'python-asn1':1,4 'real':59 'regular':68 'releas':69 'rule':29,36 'serial':81 'straightforward':9 'support':24,52 'type':56,60 'version':63,74 'written':47 'x.509':85","created_at":"2026-04-12T03:31:47.574901+00:00","updated_at":"2026-04-15T21:09:11.911681+00:00","problems":[{"fix":"Install the module using pip: 'pip install asn1'.","cause":"The 'asn1' module is not installed in the Python environment.","error":"ModuleNotFoundError: No module named 'asn1'"},{"fix":"Reinstall the 'cryptography' package ensuring all dependencies are met: 'pip install --force-reinstall cryptography'.","cause":"The 'cryptography' package is not properly installed or is missing dependencies.","error":"ImportError: cannot import name 'asn1' from 'cryptography.hazmat.bindings._rust' (unknown location)"},{"fix":"Import 'asn1' directly: 'import asn1'.","cause":"Attempting to import 'asn1' from 'pysnmp' which does not contain an 'asn1' module.","error":"ImportError: cannot import name asn1"},{"fix":"Ensure the entire ASN.1 encoded data is read before decoding.","cause":"The decoder encountered incomplete or truncated ASN.1 data.","error":"asn1.Error: Premature end of input."},{"fix":"Import 'Sequence' from 'pyasn1.type.univ': 'from pyasn1.type.univ import Sequence'.","cause":"Incorrect import of 'Sequence' from 'asn1crypto.core' instead of 'pyasn1.type.univ'.","error":"No field named 'r' defined for ASNBitcoinSignature"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"3.3.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/andrivet/python-asn1","docs":null,"changelog":null,"pypi":"https://pypi.org/project/asn1/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}