Package Crypto :: Package Util :: Module asn1 :: Class DerBitString
[frames] | no frames]

Class DerBitString

object --+    
         |    
 DerObject --+
             |
            DerBitString

Class to model a DER BIT STRING.

An example of encoding is:

>>> from Crypto.Util.asn1 import DerBitString
>>> from binascii import hexlify, unhexlify
>>> bs_der = DerBitString(b'\xaa')
>>> bs_der.value += b'\xbb'
>>> print hexlify(bs_der.encode())

which will show 040300aabb, the DER encoding for the bit string b'\xAA\xBB'.

For decoding:

>>> s = unhexlify(b'040300aabb')
>>> try:
>>>   bs_der = DerBitString()
>>>   bs_der.decode(s)
>>>   print hexlify(bs_der.value)
>>> except (ValueError, EOFError):
>>>   print "Not a valid DER OCTET STRING"

the output will be aabb.

Instance Methods
 
__init__(self, value='', implicit=None)
Initialize the DER object as a BIT STRING.
 
encode(self)
Return the DER BIT STRING, fully encoded as a binary string.
 
decode(self, derEle)
Decode a complete DER BIT STRING, and re-initializes this object with it.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables
  value
The bitstring value (packed)
Properties

Inherited from object: __class__

Method Details

__init__(self, value='', implicit=None)
(Constructor)

 
Initialize the DER object as a BIT STRING.
Parameters:
  • value (byte string) - The initial, packed bit string. If not specified, the bit string is empty.
  • implicit (integer) - The IMPLICIT tag to use for the encoded object. It overrides the universal tag for OCTET STRING (3).
Overrides: object.__init__

encode(self)

 
Return the DER BIT STRING, fully encoded as a binary string.
Overrides: DerObject.encode

decode(self, derEle)

 
Decode a complete DER BIT STRING, and re-initializes this object with it.
Parameters:
  • derEle (byte string) - A complete DER BIT STRING.
Raises:
  • ValueError - In case of parsing errors.
  • EOFError - If the DER element is too short.
Overrides: DerObject.decode