import struct


def get_unique_id(pid, computer_name):
    """
    https://github.com/poweradminllc/PAExec/blob/master/Remote.cpp#L1045-L1065
    DWORD RemMsg::GetUniqueID()

    Creates a unique ID based on the PID of the local host and the name of the
    local host. It is derived from the first 4 bytes of a UTF-16 Little Endian
    encoded computer name and the local PID xor'd together.

    This value is sent in the PAExecSettingsMsg to define the process details
    and also the PAExecResponseMsg to control the execution and results of
    the processed based on the settings.

    :param pid: (int) the process id of the current host
    :param computer_name: (str/unicode) of the current hostname
    :return: int of the unique ID derived from the PID and Computer Name
    """
    bcomp_name = computer_name.encode('utf-16-le')[:4]
    bcomp_name = bcomp_name + (b"\x00" * (4 - len(bcomp_name)))
    return pid ^ struct.unpack("<L", bcomp_name)[0]