Собственно класс для генерации уникального ключа по железу и виндовс (опционально)
Преимущества:
[LIKES=]
using System;
using System.Security.Cryptography;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
static class HardwareInfo
{
static public string GetKey(bool bind_to_windows = !true)
{
var info = string.Join("|",
GetProcessorId(),
GetHDDSerialNo(),
GetMACAddress(),
GetBoardMaker(),
GetBoardProductId(),
GetBIOSmaker(),
GetBIOSserNo(),
GetBIOScaption(),
GetPhysicalMemory(),
GetNoRamSlots(),
GetCPUManufacturer(),
GetProcessorInformation()
);
if (bind_to_windows)
info += '|' + string.Join("|",
GetOSInformation(),
GetAccountName(),
GetComputerName()
);
var ByteResult = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(info));
string StringResult = string.Empty;
foreach (byte b in ByteResult)
StringResult += String.Format("{0:x2}", b).ToUpper();
Regex regex = new Regex(@"(\w{1,8})");
var Matches = regex.Matches(StringResult);
string[] MsResult = new string[Matches.Count];
for (int i = 0; i < Matches.Count; i++)
MsResult = Matches.Groups[1].Value;
return string.Join("-", MsResult);
// info = (Math.Pow(info.GetHashCode(), 5)) .ToString();
}
static String GetProcessorId()
{
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
String Id = String.Empty;
foreach (ManagementObject mo in moc)
{
Id = mo.Properties["processorID"].Value.ToString();
break;
}
return Id;
}
static String GetHDDSerialNo()
{
ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection mcol = mangnmt.GetInstances();
string result = "";
foreach (ManagementObject strt in mcol)
{
result += Convert.ToString(strt["VolumeSerialNumber"]);
}
return result;
}
static string GetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject mo in moc)
{
if (MACAddress == String.Empty)
{
if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
}
mo.Dispose();
}
MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}
static string GetBoardMaker()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Manufacturer").ToString();
}
catch { }
}
return "Board Maker: Unknown";
}
static string GetBoardProductId()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Product").ToString();
}
catch { }
}
return "Product: Unknown";
}
static string GetCdRomDrive()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_CDROMDrive");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Drive").ToString();
}
catch { }
}
return "CD ROM Drive Letter: Unknown";
}
static string GetBIOSmaker()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Manufacturer").ToString();
}
catch { }
}
return "BIOS Maker: Unknown";
}
static string GetBIOSserNo()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("SerialNumber").ToString();
}
catch { }
}
return "BIOS Serial Number: Unknown";
}
static string GetBIOScaption()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Caption").ToString();
}
catch { }
}
return "BIOS Caption: Unknown";
}
static string GetAccountName()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_UserAccount");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Name").ToString();
}
catch { }
}
return "User Account Name: Unknown";
}
static string GetPhysicalMemory()
{
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oCollection = oSearcher.Get();
long MemSize = 0;
long mCap = 0;
// In case more than one Memory sticks are installed
foreach (ManagementObject obj in oCollection)
{
mCap = Convert.ToInt64(obj["Capacity"]);
MemSize += mCap;
}
MemSize = (MemSize / 1024) / 1024;
return MemSize.ToString() + "MB";
}
static string GetNoRamSlots()
{
int MemSlots = 0;
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery2 = new ObjectQuery("SELECT MemoryDevices FROM Win32_PhysicalMemoryArray");
ManagementObjectSearcher oSearcher2 = new ManagementObjectSearcher(oMs, oQuery2);
ManagementObjectCollection oCollection2 = oSearcher2.Get();
foreach (ManagementObject obj in oCollection2)
{
MemSlots = Convert.ToInt32(obj["MemoryDevices"]);
}
return MemSlots.ToString();
}
static string GetCPUManufacturer()
{
string cpuMan = String.Empty;
//create an instance of the Managemnet class with the
//Win32_Processor class
ManagementClass mgmt = new ManagementClass("Win32_Processor");
//create a ManagementObjectCollection to loop through
ManagementObjectCollection objCol = mgmt.GetInstances();
//start our loop for all processors found
foreach (ManagementObject obj in objCol)
{
if (cpuMan == String.Empty)
{
// only return manufacturer from first CPU
cpuMan = obj.Properties["Manufacturer"].Value.ToString();
}
}
return cpuMan;
}
static string GetDefaultIPGateway()
{
//create out management class object using the
//Win32_NetworkAdapterConfiguration class to get the attributes
//of the network adapter
ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
//create our ManagementObjectCollection to get the attributes with
ManagementObjectCollection objCol = mgmt.GetInstances();
string gateway = String.Empty;
//loop through all the objects we find
foreach (ManagementObject obj in objCol)
{
if (gateway == String.Empty) // only return MAC Address from first card
{
//grab the value from the first network adapter we find
//you can change the string to an array and get all
//network adapters found as well
//check to see if the adapter's IPEnabled
//equals true
if ((bool)obj["IPEnabled"] == true)
{
gateway = obj["DefaultIPGateway"].ToString();
}
}
//dispose of our object
obj.Dispose();
}
//replace the ":" with an empty space, this could also
//be removed if you wish
gateway = gateway.Replace(":", "");
//return the mac address
return gateway;
}
static string GetOSInformation()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return ((string)wmi["Caption"]).Trim() + ", " + (string)wmi["Version"] + ", " + (string)wmi["OSArchitecture"];
}
catch { }
}
return "BIOS Maker: Unknown";
}
static String GetProcessorInformation()
{
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
String info = String.Empty;
foreach (ManagementObject mo in moc)
{
string name = (string)mo["Name"];
name = name.Replace("(TM)", "™").Replace("(tm)", "™").Replace("(R)", "®").Replace("(r)", "®").Replace("(C)", "©").Replace("(c)", "©").Replace(" ", " ").Replace(" ", " ");
info = name + ", " + (string)mo["Caption"] + ", " + (string)mo["SocketDesignation"];
//mo.Properties["Name"].Value.ToString();
//break;
}
return info;
}
static String GetComputerName()
{
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
String info = String.Empty;
foreach (ManagementObject mo in moc)
{
info = (string)mo["Name"];
//mo.Properties["Name"].Value.ToString();
//break;
}
return info;
}
}[/LIKES]
если вызываете метод просто так
GetKey();
то он и так без вин привязки
если так
GetKey(true);
то с привязкой.
Преимущества:
- Используется очень много различной информации, что делает ключ всегда уникальным
- Простота использования
- Возможность привязки не только к железу но и к операционной системе
- Красивый ключ на выходе (XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX)
[LIKES=]
using System;
using System.Security.Cryptography;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
static class HardwareInfo
{
static public string GetKey(bool bind_to_windows = !true)
{
var info = string.Join("|",
GetProcessorId(),
GetHDDSerialNo(),
GetMACAddress(),
GetBoardMaker(),
GetBoardProductId(),
GetBIOSmaker(),
GetBIOSserNo(),
GetBIOScaption(),
GetPhysicalMemory(),
GetNoRamSlots(),
GetCPUManufacturer(),
GetProcessorInformation()
);
if (bind_to_windows)
info += '|' + string.Join("|",
GetOSInformation(),
GetAccountName(),
GetComputerName()
);
var ByteResult = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(info));
string StringResult = string.Empty;
foreach (byte b in ByteResult)
StringResult += String.Format("{0:x2}", b).ToUpper();
Regex regex = new Regex(@"(\w{1,8})");
var Matches = regex.Matches(StringResult);
string[] MsResult = new string[Matches.Count];
for (int i = 0; i < Matches.Count; i++)
MsResult = Matches.Groups[1].Value;
return string.Join("-", MsResult);
// info = (Math.Pow(info.GetHashCode(), 5)) .ToString();
}
static String GetProcessorId()
{
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
String Id = String.Empty;
foreach (ManagementObject mo in moc)
{
Id = mo.Properties["processorID"].Value.ToString();
break;
}
return Id;
}
static String GetHDDSerialNo()
{
ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection mcol = mangnmt.GetInstances();
string result = "";
foreach (ManagementObject strt in mcol)
{
result += Convert.ToString(strt["VolumeSerialNumber"]);
}
return result;
}
static string GetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject mo in moc)
{
if (MACAddress == String.Empty)
{
if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
}
mo.Dispose();
}
MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}
static string GetBoardMaker()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Manufacturer").ToString();
}
catch { }
}
return "Board Maker: Unknown";
}
static string GetBoardProductId()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Product").ToString();
}
catch { }
}
return "Product: Unknown";
}
static string GetCdRomDrive()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_CDROMDrive");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Drive").ToString();
}
catch { }
}
return "CD ROM Drive Letter: Unknown";
}
static string GetBIOSmaker()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Manufacturer").ToString();
}
catch { }
}
return "BIOS Maker: Unknown";
}
static string GetBIOSserNo()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("SerialNumber").ToString();
}
catch { }
}
return "BIOS Serial Number: Unknown";
}
static string GetBIOScaption()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Caption").ToString();
}
catch { }
}
return "BIOS Caption: Unknown";
}
static string GetAccountName()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_UserAccount");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Name").ToString();
}
catch { }
}
return "User Account Name: Unknown";
}
static string GetPhysicalMemory()
{
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oCollection = oSearcher.Get();
long MemSize = 0;
long mCap = 0;
// In case more than one Memory sticks are installed
foreach (ManagementObject obj in oCollection)
{
mCap = Convert.ToInt64(obj["Capacity"]);
MemSize += mCap;
}
MemSize = (MemSize / 1024) / 1024;
return MemSize.ToString() + "MB";
}
static string GetNoRamSlots()
{
int MemSlots = 0;
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery2 = new ObjectQuery("SELECT MemoryDevices FROM Win32_PhysicalMemoryArray");
ManagementObjectSearcher oSearcher2 = new ManagementObjectSearcher(oMs, oQuery2);
ManagementObjectCollection oCollection2 = oSearcher2.Get();
foreach (ManagementObject obj in oCollection2)
{
MemSlots = Convert.ToInt32(obj["MemoryDevices"]);
}
return MemSlots.ToString();
}
static string GetCPUManufacturer()
{
string cpuMan = String.Empty;
//create an instance of the Managemnet class with the
//Win32_Processor class
ManagementClass mgmt = new ManagementClass("Win32_Processor");
//create a ManagementObjectCollection to loop through
ManagementObjectCollection objCol = mgmt.GetInstances();
//start our loop for all processors found
foreach (ManagementObject obj in objCol)
{
if (cpuMan == String.Empty)
{
// only return manufacturer from first CPU
cpuMan = obj.Properties["Manufacturer"].Value.ToString();
}
}
return cpuMan;
}
static string GetDefaultIPGateway()
{
//create out management class object using the
//Win32_NetworkAdapterConfiguration class to get the attributes
//of the network adapter
ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
//create our ManagementObjectCollection to get the attributes with
ManagementObjectCollection objCol = mgmt.GetInstances();
string gateway = String.Empty;
//loop through all the objects we find
foreach (ManagementObject obj in objCol)
{
if (gateway == String.Empty) // only return MAC Address from first card
{
//grab the value from the first network adapter we find
//you can change the string to an array and get all
//network adapters found as well
//check to see if the adapter's IPEnabled
//equals true
if ((bool)obj["IPEnabled"] == true)
{
gateway = obj["DefaultIPGateway"].ToString();
}
}
//dispose of our object
obj.Dispose();
}
//replace the ":" with an empty space, this could also
//be removed if you wish
gateway = gateway.Replace(":", "");
//return the mac address
return gateway;
}
static string GetOSInformation()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return ((string)wmi["Caption"]).Trim() + ", " + (string)wmi["Version"] + ", " + (string)wmi["OSArchitecture"];
}
catch { }
}
return "BIOS Maker: Unknown";
}
static String GetProcessorInformation()
{
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
String info = String.Empty;
foreach (ManagementObject mo in moc)
{
string name = (string)mo["Name"];
name = name.Replace("(TM)", "™").Replace("(tm)", "™").Replace("(R)", "®").Replace("(r)", "®").Replace("(C)", "©").Replace("(c)", "©").Replace(" ", " ").Replace(" ", " ");
info = name + ", " + (string)mo["Caption"] + ", " + (string)mo["SocketDesignation"];
//mo.Properties["Name"].Value.ToString();
//break;
}
return info;
}
static String GetComputerName()
{
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
String info = String.Empty;
foreach (ManagementObject mo in moc)
{
info = (string)mo["Name"];
//mo.Properties["Name"].Value.ToString();
//break;
}
return info;
}
}[/LIKES]
если вызываете метод просто так
GetKey();
то он и так без вин привязки
если так
GetKey(true);
то с привязкой.