Skip to content

Utility Methods

get_list

get_list(
    data: list[int] | str | bytearray | int | bytes,
    byte_size: int = 2,
    byteorder: Literal["little", "big"] = "big",
) -> list[int]

Utility method to simplify the conversion of data to a list of integers. Each entry in the list represents one byte of the input data. Convert a bytearray, hex string or int to a list of integers.

Parameters:

Name Type Description Default
data str | bytearray | int | bytes

Input that should be converted to a list of integers.

required
byte_size int

Needed when input data is of type int. Guarantees that the list that is returned has this length.

2
byteorder Literal["little", "big"]

Needed when input data is of type int. Specifies the byte order that should be used when converting the integer to a list of integers.

'big'
Parsing Crypto Keys

This method is particularly useful when parsing keys that are represented as hex strings.

Returns:

Type Description
list[int]

A list of integers (each entry representing one byte).

Source code in src/desfire/util.py
def get_list(
    data: list[int] | str | bytearray | int | bytes, byte_size: int = 2, byteorder: Literal["little", "big"] = "big"
) -> list[int]:
    """
    Utility method to simplify the conversion of data to a list of integers.
    Each entry in the list represents one byte of the input data.
    Convert a bytearray, hex string or int to a list of integers.

    Args:
        data (str | bytearray | int | bytes): Input that should be converted to a list of integers.
        byte_size (int, optional): Needed when input data is of type `int`.
            Guarantees that the list that is returned has this length.
        byteorder (Literal["little", "big"], optional): Needed when input data is of type `int`.
            Specifies the byte order that should be used when converting the integer to a list of integers.

    Tip: Parsing Crypto Keys
        This method is particularly useful when parsing keys that are represented as hex strings.

    Returns:
        A list of integers (each entry representing one byte).
    """
    logger.debug(f"Converting raw data ({data!r}) to list of integers")
    if isinstance(data, list):
        # Already a list. Verify that each entry is an integer between 0 and 255.
        assert all(0 <= x <= 255 for x in data)
        logger.debug(f"Data is already a list of integers: {to_hex_string(data)}")
        return data
    elif isinstance(data, str):
        data = list(bytearray.fromhex(data))
        logger.debug(f"Data is byte array. Conversion result: {to_hex_string(data)}")
        return data
    elif isinstance(data, bytearray) or isinstance(data, bytes):
        data = list(data)
        logger.debug(f"Data is byte array. Conversion result: {to_hex_string(data)}")
        return data
    elif isinstance(data, int):
        data = list(data.to_bytes(byte_size, byteorder=byteorder))
        logger.debug(f"Data is integer. Conversion result: {to_hex_string(data)}")
        return data

    logger.warning(f"Data type not recognized: {type(data)}, returning as is")
    return data