bran

Bran provides DER-Encoded ASN.1 Serialization and Deserialization.

Other serialization methods abound, but DER-encoded ASN.1 is used in cryptography for one particular reason: given the same object, its encoding is the same, which means hashes over its encoded form are.

For much the same reason, DER-encoded ASN.1 is useful in other contexts.

class bran.ASN1Transcoder(**kwargs)[source]

Bases: object

Transcode Python builtin (and extended) types to and from ASN.1 classes.

Note that encoding is lossy with regards to specialized types.

For many Python builtin types, there are corresponding universal ASN.1 data types available.

For tuple() and list(), we use explicit tagging (which, when DER-encoded, takes up a few extra Bytes of space) to disambiguate one kind of sequence from another. Any collections.Sequence that is not specifically a list is encoded as a tuple(), any additional type information.

Similarly, any collections.Mapping is encoded in the same way, and so is any collections.Set. If you use special subtypes of these Abstract Base Classes, that type information is lost.

Still, it means that any nested structure will result in an equality- comparable nested structure, which is all that often times is required.

decode(value)[source]

Decode the given ASN.1 class into a Python value.

Nested values (for Sequence, Set and Mapping) are recursively decoded.

Parameters:value (mixed) – The value to decode.
Returns:An Python value.
encode(value)[source]

Encode the given Python value.

Nested values (for Sequence, Set and Mapping) are recursively encoded.

Parameters:value (mixed) – The value to encode.
Returns:An ASN.1 class encapsulating the value.
class bran.DERTranscoder(inner=None)[source]

Bases: object

DER-encode Python builtin (and extended) types.

The class first uses ASN1Transcoder to encode valuesin ASN.1 classes, then DER-encodes the result.

decode(value)[source]

DER-deocde the given byte sequence.

Parameters:value (bytes) – The value to decode.
Returns:A Python value, the result of passing the parameter through DER decoding and ASN.1 decoding.
encode(value)[source]

DER-encode the given value.

Nested values are encoded recursively.

Parameters:value (mixed) – The value to encode.
Returns:An DER-encoded ASN.1 value, i.e. a byte sequence.

Classes

class bran.ASN1Transcoder(**kwargs)[source]

Bases: object

Transcode Python builtin (and extended) types to and from ASN.1 classes.

Note that encoding is lossy with regards to specialized types.

For many Python builtin types, there are corresponding universal ASN.1 data types available.

For tuple() and list(), we use explicit tagging (which, when DER-encoded, takes up a few extra Bytes of space) to disambiguate one kind of sequence from another. Any collections.Sequence that is not specifically a list is encoded as a tuple(), any additional type information.

Similarly, any collections.Mapping is encoded in the same way, and so is any collections.Set. If you use special subtypes of these Abstract Base Classes, that type information is lost.

Still, it means that any nested structure will result in an equality- comparable nested structure, which is all that often times is required.

decode(value)[source]

Decode the given ASN.1 class into a Python value.

Nested values (for Sequence, Set and Mapping) are recursively decoded.

Parameters:value (mixed) – The value to decode.
Returns:An Python value.
encode(value)[source]

Encode the given Python value.

Nested values (for Sequence, Set and Mapping) are recursively encoded.

Parameters:value (mixed) – The value to encode.
Returns:An ASN.1 class encapsulating the value.
class bran.DERTranscoder(inner=None)[source]

Bases: object

DER-encode Python builtin (and extended) types.

The class first uses ASN1Transcoder to encode valuesin ASN.1 classes, then DER-encodes the result.

decode(value)[source]

DER-deocde the given byte sequence.

Parameters:value (bytes) – The value to decode.
Returns:A Python value, the result of passing the parameter through DER decoding and ASN.1 decoding.
encode(value)[source]

DER-encode the given value.

Nested values are encoded recursively.

Parameters:value (mixed) – The value to encode.
Returns:An DER-encoded ASN.1 value, i.e. a byte sequence.