Skip to content

Instantly share code, notes, and snippets.

@vaibhavpandeyvpz
Last active February 11, 2024 11:09
Show Gist options
  • Save vaibhavpandeyvpz/d6a90bfce25c8f06dba4a01ffa983759 to your computer and use it in GitHub Desktop.
Save vaibhavpandeyvpz/d6a90bfce25c8f06dba4a01ffa983759 to your computer and use it in GitHub Desktop.
Generate unique device ID or fingerprint for licensing etc. on Windows based system.
using System;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
namespace DeviceID
{
internal class Fingerprint
{
public static string Generate()
{
var str = GetBoardSerialNumber() + "_" + GetCpuID() + "_" + GetHardDiskID();
return CreateMD5(str);
}
private static string GetBoardSerialNumber() =>
GetManagementObjectValue("SELECT SerialNumber FROM Win32_BaseBoard", "SerialNumber");
public static string GetCpuID() =>
GetManagementObjectValue("SELECT ProcessorID FROM Win32_Processor", "ProcessorID");
private static string GetHardDiskID() =>
GetManagementObjectValue("SELECT SerialNumber FROM Win32_DiskDrive WHERE InterfaceType = 'SCSI'", "SerialNumber");
private static string GetManagementObjectValue(string query, string property)
{
try
{
var mos = new ManagementObjectSearcher(query);
var moc = mos.Get();
foreach (ManagementObject mo in moc.Cast<ManagementObject>())
{
return mo[property].ToString();
}
}
catch
{
// ignore
}
return string.Empty;
}
private static string CreateMD5(string input)
{
using var md5 = MD5.Create();
var bytes = Encoding.ASCII.GetBytes(input);
var hash = md5.ComputeHash(bytes);
return Convert.ToHexString(hash);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment