I wanted to generate a hardware hash that would be used to generate a key to encrypt a file so it can’t be exchanged with a different computer. This isn’t a license issue, it’s to store a backup copy of some data from our online database so that the app can be used offline (but the data contained in it is reasonably secure from snooping). Previously, I grabbed MAC address of (real) network adapters like this:
ManagementClass mng = new ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection apts = mng.GetInstances();
IEnumerable<string> macs = from ManagementObject mo in apts
where mo["MacAddress"] != null && mo["NetConnectionStatus"] != null
select mo["MacAddress"].ToString();
This worked reasonably well, except now I find with a new computer I have that when the wireless is turned off, I get two MAC address (one for the wired network card and one for the wireless), when I turn wireless on, I get three (the previous two, plus a Bluetooth one!) and I don’t see any really obvious way to filter out the Bluetooth one (short of searching for “Bluetooth” in the NetConnectionID).
Now I don’t need a super stable hash here. If the hash doesn’t match, then the app can’t decode the encrypted data and will download a fresh copy if on the network, so it’s not that big of a deal (if you’re not on the network, it’ll just tell you it’s not available right now). But if turning the wireless on and off is enough to change my hash, then it’s clearly not stable enough!
So my questions:
1) Is there a better way to tell that one of the network adapters might disappear suddenly so I can exclude it from the original hash?
2) Is the order of adapter guaranteed to be the same? Is the first adapter listed always the wired network card? If that’s the case, maybe I’ll just restrict it to using only the first adapter.
3) Is there a better, more stable hash I could use? Or a combination that would be reasonably stable?
Note: I also throw in the machine name from Environment.MachineName to my hash already.
I don’t know the answer to 1 or 2, but here’s a potential workaround:
You can create a more stable hash by combining machine name, processorID, & motherboardID. If you’re just using this for local identification that should be strong enough. This CodePlex article has a good list of the supported hardware keys. Please let me know if you have any questions.