import struct
import codecs


def _check_key(key_rank, key_idx, received_key_hex):
    """Check that the received key is the same as the sent key.

    Args:
        key_rank (int): 32-bit integer
        key_idx (int): 32-bit integer
        received_key_hex (int): hex(key_rank|key_idx)

    Returns:
        bool: is the received key is the same as the sent key

    """
    if len(received_key_hex) != 16:
        return False

    rank = struct.unpack("<I",
                         codecs.decode(received_key_hex[:8], "hex"))[0]
    idx = struct.unpack("<I",
                        codecs.decode(received_key_hex[8:], "hex"))[0]

    return (rank == key_rank) and (idx == key_idx)