Skip to content

API Reference

Core functionality for getting hardware IDs.

get_hwid()

Get the hardware ID of the current machine.

Returns:

Name Type Description
str str

The hardware ID string.

Raises:

Type Description
UnsupportedOSError

If the operating system is not supported.

InvalidHWIDError

If the retrieved hardware ID is invalid.

Source code in hwid/core.py
def get_hwid() -> str:
    """Get the hardware ID of the current machine.

    Returns:
        str: The hardware ID string.

    Raises:
        UnsupportedOSError: If the operating system is not supported.
        InvalidHWIDError: If the retrieved hardware ID is invalid.
    """
    if platform in {"linux", "linux2"}:
        command = "sudo dmidecode -s system-uuid"
        output = subprocess.check_output(command, shell=True)
        output = output.decode("utf-8").strip()
    elif platform == "win32":
        command = 'powershell -Command "(Get-CimInstance -ClassName Win32_ComputerSystemProduct).UUID"'
        output = subprocess.check_output(command, shell=True)
        output = output.decode("utf-8").strip()
    elif platform == "darwin":
        command = "system_profiler SPHardwareDataType | grep 'UUID'"
        output = subprocess.check_output(command, shell=True)
        output = output.decode("utf-8").strip()
        output = output.split(":")[1].strip()
    else:
        msg = "Unsupported OS"
        raise UnsupportedOSError(msg)
    if validate_hwid(value=output):
        return output
    msg = "Invalid HWID"
    raise InvalidHWIDError(msg)

validate_hwid(value)

Validate if a string matches the HWID format.

Parameters:

Name Type Description Default
value str

The string to validate.

required

Returns:

Name Type Description
bool bool

True if valid, False otherwise.

Source code in hwid/core.py
def validate_hwid(value: str) -> bool:
    """Validate if a string matches the HWID format.

    Args:
        value: The string to validate.

    Returns:
        bool: True if valid, False otherwise.
    """
    return bool(
        re.match(r"^[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}$", value)
    )