Class DerSequence
object --+
|
DerObject --+
|
DerSequence
Class to model a DER SEQUENCE.
This object behaves like a dynamic Python sequence.
Sub-elements that are INTEGERs behave like Python integers.
Any other sub-element is a binary string encoded as a complete DER
sub-element (TLV).
An example of encoding is:
>>> from Crypto.Util.asn1 import DerSequence, DerInteger
>>> from binascii import hexlify, unhexlify
>>> obj_der = unhexlify('070102')
>>> seq_der = DerSequence([4])
>>> seq_der.append(9)
>>> seq_der.append(obj_der.encode())
>>> print hexlify(seq_der.encode())
which will show 3009020104020109070102, the DER encoding of the
sequence containing 4, 9, and the object with payload 02.
For decoding:
>>> s = unhexlify(b'3009020104020109070102')
>>> try:
>>> seq_der = DerSequence()
>>> seq_der.decode(s)
>>> print len(seq_der)
>>> print seq_der[0]
>>> print seq_der[:]
>>> except (ValueError, EOFError):
>>> print "Not a valid DER SEQUENCE"
the output will be:
3
4
[4L, 9L, b'']
|
__init__(self,
startSeq=None,
implicit=None)
Initialize the DER object as a SEQUENCE. |
|
|
|
|
|
|
|
__setitem__(self,
key,
value) |
|
|
|
__setslice__(self,
i,
j,
sequence) |
|
|
|
|
|
|
|
|
|
|
|
|
|
hasInts(self,
onlyNonNegative=True)
Return the number of items in this sequence that are
integers. |
|
|
|
hasOnlyInts(self,
onlyNonNegative=True)
Return True if all items in this sequence are integers
or non-negative integers. |
|
|
|
encode(self)
Return this DER SEQUENCE, fully encoded as a
binary string. |
|
|
|
decode(self,
derEle)
Decode a complete DER SEQUENCE, and re-initializes this
object with it. |
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__sizeof__ ,
__str__ ,
__subclasshook__
|
Inherited from object :
__class__
|
__init__(self,
startSeq=None,
implicit=None)
(Constructor)
|
|
Initialize the DER object as a SEQUENCE.
- Parameters:
startSeq (Python sequence) - A sequence whose element are either integers or
other DER objects.
implicit (integer) - The IMPLICIT tag to use for the encoded object.
It overrides the universal tag for SEQUENCE (16).
- Overrides:
object.__init__
|
hasInts(self,
onlyNonNegative=True)
|
|
Return the number of items in this sequence that are
integers.
- Parameters:
onlyNonNegative (boolean) - If True, negative integers are not counted in.
|
hasOnlyInts(self,
onlyNonNegative=True)
|
|
Return True if all items in this sequence are integers
or non-negative integers.
This function returns False is the sequence is empty,
or at least one member is not an integer.
- Parameters:
onlyNonNegative (boolean) - If True, the presence of negative integers
causes the method to return False.
|
Return this DER SEQUENCE, fully encoded as a
binary string.
- Raises:
ValueError - If some elements in the sequence are neither integers
nor byte strings.
- Overrides:
DerObject.encode
|
Decode a complete DER SEQUENCE, and re-initializes this
object with it.
DER INTEGERs are decoded into Python integers. Any other DER
element is not decoded. Its validity is not checked.
- Parameters:
derEle (byte string) - A complete SEQUENCE DER element.
- Raises:
ValueError - In case of parsing errors.
EOFError - If the DER element is too short.
- Overrides:
DerObject.decode
|