# Path: asn1crypto/util.py
# def int_from_bytes(value, signed=False):
#     """
#     Converts a byte string to an integer
# 
#     :param value:
#         The byte string to convert
# 
#     :param signed:
#         If the byte string should be interpreted using two's complement
# 
#     :return:
#         An integer
#     """
# 
#     if value == b'':
#         return 0
# 
#     num = long(value.encode("hex"), 16)  # noqa
# 
#     if not signed:
#         return num
# 
#     # Check for sign bit and handle two's complement
#     if ord(value[0:1]) & 0x80:
#         bit_len = len(value) * 8
#         return num - (1 << bit_len)
# 
#     return num
# 
# def int_to_bytes(value, signed=False, width=None):
#     """
#     Converts an integer to a byte string
# 
#     :param value:
#         The integer to convert
# 
#     :param signed:
#         If the byte string should be encoded using two's complement
# 
#     :param width:
#         If None, the minimal possible size (but at least 1),
#         otherwise an integer of the byte width for the return value
# 
#     :return:
#         A byte string
#     """
# 
#     if value == 0 and width == 0:
#         return b''
# 
#     # Handle negatives in two's complement
#     is_neg = False
#     if signed and value < 0:
#         is_neg = True
#         bits = int(math.ceil(len('%x' % abs(value)) / 2.0) * 8)
#         value = (value + (1 << bits)) % (1 << bits)
# 
#     hex_str = '%x' % value
#     if len(hex_str) & 1:
#         hex_str = '0' + hex_str
# 
#     output = hex_str.decode('hex')
# 
#     if signed and not is_neg and ord(output[0:1]) & 0x80:
#         output = b'\x00' + output
# 
#     if width is not None:
#         if len(output) > width:
#             raise OverflowError('int too big to convert')
#         if is_neg:
#             pad_char = b'\xFF'
#         else:
#             pad_char = b'\x00'
#         output = (pad_char * (width - len(output))) + output
#     elif is_neg and ord(output[0:1]) & 0x80 == 0:
#         output = b'\xFF' + output
# 
#     return output


import sys
from ._types import byte_cls, chr_cls, type_name
from .util import int_from_bytes, int_to_bytes
        An integer ASN.1 tag value

    :param contents:
        A byte string of the encoded byte contents

    :return:
        A byte string of the ASN.1 DER header
    """

    header = b''

    id_num = 0
    id_num |= class_ << 6
    id_num |= method << 5

    if tag >= 31:
        cont_bit = 0
        while tag > 0:
            header = chr_cls(cont_bit | (tag & 0x7f)) + header
            if not cont_bit:
                cont_bit = 0x80
            tag = tag >> 7
        header = chr_cls(id_num | 31) + header
    else:
        header += chr_cls(id_num | tag)

    length = len(contents)
    if length <= 127:
        header += chr_cls(length)
    else:

# Path: asn1crypto/util.py
# def int_from_bytes(value, signed=False):
#     """
#     Converts a byte string to an integer
# 
#     :param value:
#         The byte string to convert
# 
#     :param signed:
#         If the byte string should be interpreted using two's complement
# 
#     :return:
#         An integer
#     """
# 
#     if value == b'':
#         return 0
# 
#     num = long(value.encode("hex"), 16)  # noqa
# 
#     if not signed:
#         return num
# 
#     # Check for sign bit and handle two's complement
#     if ord(value[0:1]) & 0x80:
#         bit_len = len(value) * 8
#         return num - (1 << bit_len)
# 
#     return num
# 
# def int_to_bytes(value, signed=False, width=None):
#     """
#     Converts an integer to a byte string
# 
#     :param value:
#         The integer to convert
# 
#     :param signed:
#         If the byte string should be encoded using two's complement
# 
#     :param width:
#         If None, the minimal possible size (but at least 1),
#         otherwise an integer of the byte width for the return value
# 
#     :return:
#         A byte string
#     """
# 
#     if value == 0 and width == 0:
#         return b''
# 
#     # Handle negatives in two's complement
#     is_neg = False
#     if signed and value < 0:
#         is_neg = True
#         bits = int(math.ceil(len('%x' % abs(value)) / 2.0) * 8)
#         value = (value + (1 << bits)) % (1 << bits)
# 
#     hex_str = '%x' % value
#     if len(hex_str) & 1:
#         hex_str = '0' + hex_str
# 
#     output = hex_str.decode('hex')
# 
#     if signed and not is_neg and ord(output[0:1]) & 0x80:
#         output = b'\x00' + output
# 
#     if width is not None:
#         if len(output) > width:
#             raise OverflowError('int too big to convert')
#         if is_neg:
#             pad_char = b'\xFF'
#         else:
#             pad_char = b'\x00'
#         output = (pad_char * (width - len(output))) + output
#     elif is_neg and ord(output[0:1]) & 0x80 == 0:
#         output = b'\xFF' + output
# 
#     return output


# Path: asn1crypto/parser.py
import sys
from ._types import byte_cls, chr_cls, type_name
from .util import int_from_bytes, int_to_bytes

        An integer ASN.1 tag value

    :param contents:
        A byte string of the encoded byte contents

    :return:
        A byte string of the ASN.1 DER header
    """

    header = b''

    id_num = 0
    id_num |= class_ << 6
    id_num |= method << 5

    if tag >= 31:
        cont_bit = 0
        while tag > 0:
            header = chr_cls(cont_bit | (tag & 0x7f)) + header
            if not cont_bit:
                cont_bit = 0x80
            tag = tag >> 7
        header = chr_cls(id_num | 31) + header
    else:
        header += chr_cls(id_num | tag)

    length = len(contents)
    if length <= 127:
        header += chr_cls(length)
    else:

        length_bytes = int_to_bytes(length)