Generating a Unique Device Identifier

When integrating with our SDK, one important requirement is generating a unique, persistent device ID for each device before calling the SDK. This device ID should remain consistent for the duration of the application's lifecycle on the device, from installation to uninstallation. Integrating device ID generation is a key first step to enabling the SDK's full functionality. The exact generation logic can vary based on your application's tech stack and architecture.

Why Persistent Device IDs Matter

Reason
Description

Uniquely identifying devices

Each device should have its own unique ID to distinguish it from other devices.

Enabling device tracking

The device ID allows tracking application usage across sessions and linking it to other data like purchases.

Supporting signed user features

For signed-in users, the device ID can help identify linked devices.

Maintaining data privacy

Using a device hash rather than personally identifiable information keeps the collected data anonymized.

Generating the Device ID

The device ID should be a string that uniquely identifies a specific device instance. Some options for generating it include:

  • Machine GUID (globally unique identifier)

  • A hash based on hardware characteristics

  • A hash incorporating the application's installation path

  • Any internally used unique device instance ID

The key is that the ID should be persistently tied to that specific device install.

For example, in VPN applications, developers generate a device_hash value based on hardware details and other settings. This hash is unique per app instance across all their devices and apps. It allows them to track application usage flows in their reports, link the data to purchases, manage linked devices for signed-in users, etc., all while keeping the data anonymized.

Handling Cloned Devices

One edge case is cloned devices, such as multiple VM instances which may end up with the same machine GUID.

If multiple devices with the same device_id attempt to authenticate with the VPN node (login and connect), whenever one logs in, the others will be automatically logged out since they appear to be the same device.

Therefore, it's important to incorporate additional unique elements beyond just the machine ID when generating your device_id hash to avoid unintended logouts in cloned device scenarios.

Methods to Generate a Device ID

Here are several methods to generate a persistent device ID on Windows:

Example 1: Using Machine GUID

string GetMachineGuid()
{
    string machineGuid = string.Empty;
    using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography"))
    {
        if (key != null)
        {
            machineGuid = key.GetValue("MachineGuid").ToString();
        }
    }
    return machineGuid;
}

Example 2: Combining Hardware Information

string GetHardwareId()
{
    string cpuId = GetCpuId();
    string biosSerial = GetBiosSerial();
    string macAddress = GetMacAddress();

    return $"{cpuId}-{biosSerial}-{macAddress}";
}

string GetCpuId()
{
    // Code to get CPU ID
}

string GetBiosSerial()
{
    // Code to get BIOS Serial
}

string GetMacAddress()
{
    // Code to get MAC Address
}

Hashing for Privacy

To ensure that the device ID is non-personalized, you can hash the combined hardware information.

string GenerateDeviceId()
{
    string hardwareId = GetHardwareId();
    using (SHA256 sha256 = SHA256.Create())
    {
        byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(hardwareId));
        return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
    }
}

Last updated