Why unique Id using System.Management and SystemInfo.deviceUniqueIdentifier in Unity are different?

Viewed 189

I have a specific problem with distinguishing computers. I want to get the same unique Id values in my Unity app and in my WPF app. In this post I took the code

private string GetDeviceUniqueIdentifier() {
        string ret = string.Empty;

        string concatStr = string.Empty;
        try {
            using ManagementObjectSearcher searcherBb = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
            foreach (var obj in searcherBb.Get()) {
                concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
            }

            using ManagementObjectSearcher searcherBios = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
            foreach (var obj in searcherBios.Get()) {
                concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
            }

            using ManagementObjectSearcher searcherOs = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
            foreach (var obj in searcherOs.Get()) {
                concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
            }

            using var sha1 = SHA1.Create();
            ret = string.Join("", sha1.ComputeHash(Encoding.UTF8.GetBytes(concatStr)).Select(b => b.ToString("x2")));
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }

        return ret;
    }

for my WPF side and I have SystemInfo.deviceUniqueIdentifier for my Unity side. On my computer these things return the same values, but when I tested it on quite weak PC, they returned different values:
WPF side retured 5f5ea7e20b798fc1c16a7c65747f58e50519cb76 and
Unity side returned c23be19b361fcc9683c544358cea150695ac74ca. I use 2021.2.14f1 version of Unity.
What could be the reason of this? Or is there other ways to have the same unique Ids in .NET Framework and in Unity?

I found out that ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard") and ManagementObjectSearcher("SELECT * FROM Win32_BIOS") return Not Applicable. This is quite strange. I also tried to use string.Empty instead of them, but Ids are still different.

1 Answers

Ok, I found the solution, but I'm not sure, that it is stable solution. As I said:

I found out that ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard") and ManagementObjectSearcher("SELECT * FROM Win32_BIOS") return Not Applicable. This is quite strange. I also tried to use string.Empty instead of them, but Ids are still different.

And I almost gave up, but tried the last one thing:

private string GetDeviceUniqueIdentifier() {
        string ret = string.Empty;

        string concatStr = string.Empty;
        try {
            using ManagementObjectSearcher searcherBb = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
            foreach (var obj in searcherBb.Get()) {
                concatStr += obj.Properties["SerialNumber"].Value.ToString().Trim() ?? string.Empty;
            }

            using ManagementObjectSearcher searcherBios = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
            foreach (var obj in searcherBios.Get()) {
                concatStr += obj.Properties["SerialNumber"].Value.ToString().Trim() ?? string.Empty;
            }

            using ManagementObjectSearcher searcherOs = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
            foreach (var obj in searcherOs.Get()) {
                concatStr += obj.Properties["SerialNumber"].Value.ToString().Trim() ?? string.Empty;
            }

            using var sha1 = SHA1.Create();
            ret = string.Join("", sha1.ComputeHash(Encoding.UTF8.GetBytes(concatStr)).Select(b => b.ToString("x2")));
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }

        return ret;
    }

I added .Trim() at the end of each statement and this solved my problem. Now my Unity Id is equal to .Net Framework Id. Hope this will help someone.

Related