views:

208

answers:

7

Possible Duplicate:
Reliable way of generating unique hardware ID

Am trying to generate an ID that will be unique to a particular computer. The ID will not be generated randomly. It will be calculation based, such that the ID generated for computer A will be fixed and unique to computer A. Everytime the program is executed on computer A, it will continue to generate the same ID and when executed on another computer, it will generate another ID unique to that computer. This is to ensure that two computers don't have the same ID.

My Challenge: For my program to be able to generate an ID unique to a computer, it needs to perform the calculation based on a seed unique to the computer executing it.

My Question: How can i get a value unique to a computer, so that i can use the value as a seed in the ID generation program?

Is it possible to get a value from a computer's hardware(eg motherboard) that is unique to that computer? That way, the value is most likely not to change as long as the computer's motherboard is not replaced.

+6  A: 

MAC address? Thats (for practical purposes) unique to every NIC so it guarantee's reproducibility even if the user is dual booting. Sure there are rare cases of people trading cards, but coupled with other metrics (don't only use this, since network cards can be changed), it's still possible.

How would you get it?

byte[] ni = NetworkInterface.getHardwareAddress();

If you want a String representation, do this

for (int k = 0; k < mac.length; k++) {
    System.out.format("%02X%s", mac[k], (i < mac.length - 1) ? "-" : "");
}

(thanks to http://www.kodejava.org/examples/250.html)

Note: As mentioned in the comments, Mac addresses can be spoofed. But your talking about a small part of the population doing this, and unless your using this for anti-piracy stuff, its unique enough.

TheLQ
On the same Wikipedia page you link to is a link to http://en.wikibooks.org/wiki/Changing_Your_MAC_Address MAC addresses are **not** unique.
msw
Not completely unique and there is no guarantee, in fact, the addresses are known to be reused, but *almost* unique and *almost* guaranteed, sure...
DigitalRoss
@msw @DigitalRoss Hardware can be removed, hardware can be changed, hardware can be spoofed, etc. There is no guaranteed uniqueness. Mac addresses are unique enough. See update
TheLQ
Please don't make me reinstall my software if I change my network card.
Michael Petrotta
MAC addresses can also be changed by the user. They are definitely not unique enough.
EJP
@EJP 95% of the time, they are not spoofed by the user. Your talking about small subset of the population that A) Even knows how to do that, B) Is paranoid, and C) Cares.
TheLQ
@TheLQ In other words it's not unique.
EJP
@EJP it is impossible to find any id which is guaranteed to be unique. Even a 2^128 bit random number is not guaranteed to be unique. It's just very unlikely someone else would generate that exact same number in the same context.
extraneon
@extraneon In other words it's not unique! And users changing MAC addresses is hardly a random process.
EJP
A: 

An easy way to do this is to read the ethernet hardware, or "mac" address.

http://download.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getHardwareAddress()

Mac addresses are not quite as unique as people think, as they do get reused as time goes on. But the odds of one application or network having two identical ones are quite low.

DigitalRoss
A: 

The MAC address is unique enough for what you. See http://en.wikipedia.org/wiki/MAC_address

You didn't specify which language you are using. It may easier in some languages than others. Here is how to do it in Java http://www.kodejava.org/examples/250.html. Google around for your language.

chr
Fortuitously, the post tags *do* specify what language he's using. And three people gave this answer before I finished typing mine... I think that's a new record. :)
djacobson
A: 

Your best option is to base the ID on the MAC address of the primary network adaptor.

This is potentially likely to change at somepoint, but so is any single hard component.

FYI GUIDs are calculated using the MAC address.

slomojo
Ha ha... 4 for the price of one.
slomojo
A: 

Have you access to any information described in this article? Windows-only

http://msdn.microsoft.com/en-us/library/aa394587.aspx

Serial number, asset tag

James
Is the serial number related to Windows, or the actual manufacturer's serial number?
vlad003
And not cross platform...
TheLQ
It relates to the physical enclosure. I did point out it's a Windows only solution, if it works.
James
A: 

Another option IFF you're using intel chips is the processor serial number, assuming you can ensure the feature is enabled. See Intel Serial # Note for more info

Mark Mullin
He's using Java, there is no notion of a processor :)
Longpoke
+4  A: 

Win32 generates a computer SID, that is supposed to be unique for each installation that you can get via WMI or Active Directory, but that is pretty platform specific. You can also use the MAC address, as everyone else has mentioned, just make sure that it is a physical network adapter, as virtual adapters tend to share the same MAC address across computers.

However, UUID's (or GUID's) are 128 bit numbers that are supposed to be guaranteed unique, and were actually created for the purpose of solving the problem of generating unique identifiers across multiple, random machines. According to Wikipedia:

To put these numbers into perspective, one's annual risk of being hit by a meteorite is estimated to be one chance in 17 billion,[25] that means the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs.

The total number of possible combinations is 2^128 (or 3 x 10^38), so I tend to believe it. Also, most modern UUID generators don't use the V1 algorithm anymore (i.e. the one based off the MAC address), since it is considered a security issue due to the fact that one can tell when the GUID was generated, and who generated it. In the Win32 world, a security patch circa Win2K or NT 4 changed to use the V4 version of the algorithm, which is based off of a pseudo-random number instead of the MAC, and the JVM has always used the V3/V4 version.

EDIT: The method used to generate UUID's in Java is via the java.util.UUID class.

Javert93
So would doing `UUID.fromString("some string");` generate the same exact UUID over several VM instances? What about on the same computer but on a different OS?
TheLQ
@TheLQ: "some string" can not be parsed :) Did you read http://www.ietf.org/rfc/rfc4122.txt ?
extraneon
@TheLQ: The syntax would be `UUID.fromString("{00000000-0000-0000-0000-000000000000}")`, and would be used to convert an existing string representation of a UUID to a UUID class, so yes, you would get the same UUID every time for the same input value (assuming you hard coded the input string). To generate a new UUID, you'd use `UUID.randomUUID()`, which should be unique across several VM instances.
Javert93
I forgot to mention it, but the same is true for different OS's.
Javert93