Basic Encoding Rules

pyasn1.codec.ber.encoder.encode(value, defMode=True, maxChunkSize=0)

Turns ASN.1 object into BER octet stream.

Takes any ASN.1 object (e.g. PyAsn1Item derivative) walks all its components recursively and produces a BER octet stream.

Parameters:A pyasn1 object to encode
defMode: bool
If False, produces indefinite length encoding
maxChunkSize: int
Maximum chunk size in chunked encoding mode (0 denotes unlimited chunk size)
Returns:bytes (Python 3) or str (Python 2) – Given ASN.1 object encoded into BER octetstream
Raises:: :py:class:`pyasn1.error.PyAsn1Error` – On encoding errors
pyasn1.codec.ber.decoder.decode(substrate, asn1Spec=None)

Turns BER octet stream into an ASN.1 object.

Takes BER octetstream and decode it into an ASN.1 object (e.g. PyAsn1Item derivative) which may be a scalar or an arbitrary nested structure.

  • substrate (bytes (Python 3) or str (Python 2)) – BER octetstream
  • asn1Spec (any pyasn1 type object e.g. PyAsn1Item derivative) – A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure being decoded, asn1Spec may or may not be required. Most common reason for it to require is that ASN.1 structure is encoded in IMPLICIT tagging mode.

tuple – A tuple of pyasn1 object recovered from BER substrate (PyAsn1Item derivative) and the unprocessed trailing portion of the substrate (may be empty)


: :py:class:`pyasn1.error.PyAsn1Error` – On decoding errors