Initial commit
This commit is contained in:
70
Valheim Dashboard/HWID.cs
Normal file
70
Valheim Dashboard/HWID.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.Win32;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
class HWID
|
||||
{
|
||||
public static string GetHWID()
|
||||
{
|
||||
string rawData = "";
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
rawData = GetWindowsHWID();
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
rawData = GetLinuxHWID();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException("Unsupported OS");
|
||||
}
|
||||
|
||||
return ComputeHash(rawData);
|
||||
}
|
||||
|
||||
private static string GetWindowsHWID()
|
||||
{
|
||||
string machineGuid = GetRegistryValue(@"SOFTWARE\Microsoft\Cryptography", "MachineGuid");
|
||||
string uuid = GetRegistryValue(@"SYSTEM\CurrentControlSet\Control\IDConfigDB\Hardware Profiles\0001", "HwProfileGuid");
|
||||
|
||||
return $"{machineGuid}-{uuid}";
|
||||
}
|
||||
|
||||
private static string GetRegistryValue(string path, string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(path))
|
||||
{
|
||||
return regKey?.GetValue(key)?.ToString();
|
||||
}
|
||||
}
|
||||
catch { return null; }
|
||||
}
|
||||
|
||||
private static string GetLinuxHWID()
|
||||
{
|
||||
return ReadFile("/etc/machine-id") ?? ReadFile("/var/lib/dbus/machine-id");
|
||||
}
|
||||
|
||||
private static string ReadFile(string path)
|
||||
{
|
||||
try { return File.ReadAllText(path).Trim(); }
|
||||
catch { return null; }
|
||||
}
|
||||
|
||||
private static string ComputeHash(string input)
|
||||
{
|
||||
using (SHA256 sha256 = SHA256.Create())
|
||||
{
|
||||
byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
return BitConverter.ToString(hash).Replace("-", "").ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user