ecdsa.der module

exception ecdsa.der.UnexpectedDER[source]

Bases: Exception

add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

ecdsa.der.encode_bitstring(s, unused=<object object>)[source]

Encode a binary string as a BIT STRING using DER encoding.

Note, because there is no native Python object that can encode an actual bit string, this function only accepts byte strings as the s argument. The byte string is the actual bit string that will be encoded, padded on the right (least significant bits, looking from big endian perspective) to the first full byte. If the bit string has a bit length that is multiple of 8, then the padding should not be included. For correct DER encoding the padding bits MUST be set to 0.

Number of bits of padding need to be provided as the unused parameter. In case they are specified as None, it means the number of unused bits is already encoded in the string as the first byte.

The deprecated call convention specifies just the s parameters and encodes the number of unused bits as first parameter (same convention as with None).

Empty string must be encoded with unused specified as 0.

Future version of python-ecdsa will make specifying the unused argument mandatory.

Parameters:
  • s (bytes like object) – bytes to encode

  • unused (int or None) – number of bits at the end of s that are unused, must be between 0 and 7 (inclusive)

Raises:

ValueError – when unused is too large or too small

Returns:

s encoded using DER

Return type:

bytes

ecdsa.der.encode_constructed(tag, value)[source]
ecdsa.der.encode_integer(r)[source]
ecdsa.der.encode_length(l)[source]
ecdsa.der.encode_number(n)[source]
ecdsa.der.encode_octet_string(s)[source]
ecdsa.der.encode_oid(first, second, *pieces)[source]
ecdsa.der.encode_sequence(*encoded_pieces)[source]
ecdsa.der.int2byte()

S.pack(v1, v2, …) -> bytes

Return a bytes object containing values v1, v2, … packed according to the format string S.format. See help(struct) for more on format strings.

ecdsa.der.is_sequence(string)[source]
ecdsa.der.read_length(string)[source]
ecdsa.der.read_number(string)[source]
ecdsa.der.remove_bitstring(string, expect_unused=<object object>)[source]

Remove a BIT STRING object from string following DER.

The expect_unused can be used to specify if the bit string should have the amount of unused bits decoded or not. If it’s an integer, any read BIT STRING that has number of unused bits different from specified value will cause UnexpectedDER exception to be raised (this is especially useful when decoding BIT STRINGS that have DER encoded object in them; DER encoding is byte oriented, so the unused bits will always equal 0).

If the expect_unused is specified as None, the first element returned will be a tuple, with the first value being the extracted bit string while the second value will be the decoded number of unused bits.

If the expect_unused is unspecified, the decoding of byte with number of unused bits will not be attempted and the bit string will be returned as-is, the callee will be required to decode it and verify its correctness.

Future version of python will require the expected_unused parameter to be specified.

Parameters:
  • string (bytes like object) – string of bytes to extract the BIT STRING from

  • expect_unused (int or None) – number of bits that should be unused in the BIT STRING, or None, to return it to caller

Raises:

UnexpectedDER – when the encoding does not follow DER.

Returns:

a tuple with first element being the extracted bit string and the second being the remaining bytes in the string (if any); if the expect_unused is specified as None, the first element of the returned tuple will be a tuple itself, with first element being the bit string as bytes and the second element being the number of unused bits at the end of the byte array as an integer

Return type:

tuple

ecdsa.der.remove_constructed(string)[source]
ecdsa.der.remove_integer(string)[source]
ecdsa.der.remove_object(string)[source]
ecdsa.der.remove_octet_string(string)[source]
ecdsa.der.remove_sequence(string)[source]
ecdsa.der.topem(der, name)[source]
ecdsa.der.unpem(pem)[source]