java 生成特定计算机的唯一 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3644722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating ID unique to a particular computer
提问by Dami Lola
Possible Duplicate:
Reliable way of generating unique hardware ID
可能的重复:
生成唯一硬件 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.
我正在尝试生成一个对特定计算机唯一的 ID。ID 不会随机生成。它将是基于计算的,这样为计算机 A 生成的 ID 将是固定的,并且是计算机 A 唯一的。程序每次在计算机 A 上执行时,都会继续生成相同的 ID,当在另一台计算机上执行时,它会生成该计算机的另一个唯一 ID。这是为了确保两台计算机没有相同的 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.
我的挑战:为了让我的程序能够生成计算机唯一的 ID,它需要根据执行它的计算机唯一的种子来执行计算。
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?
我的问题:如何获得计算机独有的值,以便我可以将该值用作 ID 生成程序中的种子?
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.
是否可以从计算机的硬件(例如主板)中获取该计算机独有的值?这样,只要不更换计算机的主板,该值很可能不会改变。
采纳答案by TheLQ
MAC address? Thats (for practical purposes) unique to every NICso 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.
MAC地址?这(出于实际目的)对每个 NIC都是独一无二的,因此即使用户是双引导,它也能保证可重复性。当然,很少有人交换卡片,但结合其他指标(不要只使用这个,因为网卡可以更改),这仍然是可能的。
How would you get it?
你会如何得到它?
public static byte[] getMACAddress() throws SocketException, UnknownHostException {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address);
return networkInterface.getHardwareAddress();
}
If you want a String representation, do this
如果你想要一个字符串表示,这样做
for (int byteIndex = 0; byteIndex < macAddress.length; byteIndex++) {
System.out.format("%02X%s", macAddress[byteIndex], (byteIndex < macAddress.length - 1) ? "-" : "");
}
(thanks to http://www.kodejava.org/examples/250.html)
(感谢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.
注意:正如评论中提到的,Mac 地址可以被欺骗。但是您谈论的是一小部分人这样做,除非您将其用于反盗版,否则它足够独特。
回答by Javert93
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.
Win32 生成一个计算机 SID,对于您可以通过 WMI 或 Active Directory 获得的每个安装,它应该是唯一的,但它是特定于平台的。您也可以使用 MAC 地址,正如其他人所提到的,只要确保它是一个物理网络适配器,因为虚拟适配器往往会在计算机之间共享相同的 MAC 地址。
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:
但是,UUID(或 GUID)是 128 位数字,应该保证唯一,并且实际上是为了解决跨多个随机机器生成唯一标识符的问题而创建的。根据维基百科:
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.
从这些数字的角度来看,一个人每年被陨石撞击的风险估计为 170 亿分之一,[25] 这意味着概率约为 0.00000000006 (6 × 10?11),相当于创造的几率一年中有几十万亿个 UUID,并且有一个重复。换句话说,只有在接下来的 100 年中每秒生成 10 亿个 UUID 之后,仅创建一个重复项的概率约为 50%。如果地球上每个人都拥有 6 亿个 UUID,一个重复的概率约为 50%。
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.
可能的组合总数是 2^128(或 3 x 10^38),所以我倾向于相信它。此外,大多数现代 UUID 生成器不再使用 V1 算法(即基于 MAC 地址的算法),因为它被认为是一个安全问题,因为人们可以知道 GUID 何时生成以及是谁生成的. 在 Win32 世界中,大约 Win2K 或 NT 4 的安全补丁更改为使用 V4 版本的算法,该算法基于伪随机数而不是 MAC,并且 JVM 一直使用 V3/V4 版本。
EDIT: The method used to generate UUID's in Java is via the java.util.UUID class.
编辑:用于在 Java 中生成 UUID 的方法是通过 java.util.UUID 类。
回答by DigitalRoss
An easy way to do this is to read the ethernet hardware, or "mac" address.
一种简单的方法是读取以太网硬件或“mac”地址。
http://download.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getHardwareAddress()
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.
Mac 地址并不像人们想象的那么独特,因为它们确实会随着时间的推移而重复使用。但是一个应用程序或网络具有两个相同的应用程序或网络的几率非常低。
回答by Christian Garbin
The MAC address is unique enough for what you. See http://en.wikipedia.org/wiki/MAC_address
MAC 地址对于您来说足够独特。见 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.
您没有指定您使用的是哪种语言。在某些语言中它可能比其他语言更容易。以下是如何在 Java http://www.kodejava.org/examples/250.html 中执行此操作 。谷歌搜索您的语言。
回答by ocodo
Your best option is to base the ID on the MAC address of the primary network adaptor.
您最好的选择是将 ID 基于主网络适配器的 MAC 地址。
This is potentially likely to change at somepoint, but so is any single hard component.
这可能会在某个时候改变,但任何单一的硬组件也是如此。
FYI GUIDs are calculated using the MAC address.
仅供参考 GUID 是使用 MAC 地址计算的。
回答by James
Have you access to any information described in this article? Windows-only
您是否可以访问本文中描述的任何信息?仅限 Windows
http://msdn.microsoft.com/en-us/library/aa394587.aspx
http://msdn.microsoft.com/en-us/library/aa394587.aspx
Serial number, asset tag
序列号、资产标签
回答by Mark Mullin
Another option IFF you're using intel chips is the processor serial number, assuming you can ensure the feature is enabled. See Intel Serial # Notefor more info
您使用英特尔芯片的另一个选项 IFF 是处理器序列号,假设您可以确保启用该功能。有关详细信息,请参阅Intel Serial # Note