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

Class DerInteger

object --+    
         |    
 DerObject --+
             |
            DerInteger

Class to model a DER INTEGER.

An example of encoding is:

>>> from Crypto.Util.asn1 import DerInteger
>>> from binascii import hexlify, unhexlify
>>> int_der = DerInteger(9)
>>> print hexlify(int_der.encode())

which will show 020109, the DER encoding of 9.

And for decoding:

>>> s = unhexlify(b'020109')
>>> try:
>>>   int_der = DerInteger()
>>>   int_der.decode(s)
>>>   print int_der.value
>>> except (ValueError, EOFError):
>>>   print "Not a valid DER INTEGER"

the output will be 9.

Instance Methods
 
__init__(self, value=0, implicit=None)
Initialize the DER object as an INTEGER.
 
encode(self)
Return the DER INTEGER, fully encoded as a binary string.
 
decode(self, derEle)
Decode a complete DER INTEGER DER, 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 integer value
Properties

Inherited from object: __class__

Method Details

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

 
Initialize the DER object as an INTEGER.
Parameters:
  • value (integer) - The value of the integer.
  • implicit (integer) - The IMPLICIT tag to use for the encoded object. It overrides the universal tag for INTEGER (2).
Overrides: object.__init__

encode(self)

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

decode(self, derEle)

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