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

Class DerOctetString

object --+    
         |    
 DerObject --+
             |
            DerOctetString

Class to model a DER OCTET STRING.

An example of encoding is:

>>> from Crypto.Util.asn1 import DerOctetString
>>> from binascii import hexlify, unhexlify
>>> os_der = DerOctetString(b'\xaa')
>>> os_der.payload += b'\xbb'
>>> print hexlify(os_der.encode())

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

For decoding:

>>> s = unhexlify(b'0402aabb')
>>> try:
>>>   os_der = DerOctetString()
>>>   os_der.decode(s)
>>>   print hexlify(os_der.payload)
>>> 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 an OCTET STRING.

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

    Inherited from DerObject
 
decode(self, derEle)
Decode a complete DER element, and re-initializes this object with it.
 
encode(self)
Return this DER element, fully encoded as a binary byte string.
Properties

Inherited from object: __class__

Method Details

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

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