C++ 获取处理器 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5658975/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:37:24  来源:igfitidea点击:

C++ get Processor ID

c++windowsprocessor

提问by Vijay

This thread is ok. How to get Processor and Motherboard Id?

这个线程没问题。 如何获取处理器和主板 ID?

I wanted to get processor ID using C++ code not using WMI or any third party lib.

我想使用 C++ 代码而不使用 WMI 或任何第三方库来获取处理器 ID。

OR anything on a computer that turns out to be unique. One thing is Ethernet ID but which is again removable on some machines. This I want to use mostly for licensing purpose.

或者计算机上的任何东西都是独一无二的。一件事是以太网 ID,但它在某些机器上又是可移动的。我想主要用于许可目的。

Is processor ID unique and available on all major processors?

处理器 ID 是唯一的并且在所有主要处理器上都可用吗?

回答by Tamás Szelei

I had a similar problem lately and I did the following. First I gained some unique system identification values:

我最近遇到了类似的问题,我做了以下事情。首先,我获得了一些独特的系统识别值:

I took these values and combined them in an arbitrary but deterministic way(read update below!)(adding, xoring, dividing and keeping the remainder etc.). Iterate over the values as if they were strings and be creative. In the end, you will get a byte literal which you can transform to the ASCII range of letters and numbers to get a unique, "readable" code that doesn't look like noise.

我将这些值以任意但确定的方式组合起来(阅读下面的更新!)(添加、异或、除和保留余数等)。像字符串一样迭代这些值并具有创造性。最后,您将获得一个字节文字,您可以将其转换为字母和数字的 ASCII 范围,以获得一个独特的、“可读”的、看起来不像噪音的代码。

Another approach can be simply concatenating these values and then "cover them up" with xoring something over them (and maybe transforming to letters again).

另一种方法可以简单地连接这些值,然后通过对它们进行异或运算(并且可能再次转换为字母)来“掩盖它们”。

I'm saying it's unique, because at least one of the inputs is supposed to be unique (the MAC address). Of course you need some understanding of number theory to not blew away this uniqueness, but it should be good enough anyway.

我是说它是唯一的,因为至少有一个输入应该是唯一的(MAC 地址)。当然,你需要对数论有一定的了解才能不破坏这种独特性,但无论如何它应该足够好。

Important update: Since this post I learned a few things about cryptography, and I'm on the opinion that making up an arbitrary combination (essentially your own hash) is almost certainly a bad idea. Hash functions used in practice are constructed to be well-behaved (as in low probability of collisions) and to be hard to break (the ability construct a value that has the same hash value as another). Constructing such a function is a very hard computer science problem and unless you are qualified, you shouldn't attempt. The correct approach for this is to concatenate whatever information you can collect about the hardware (i.e. the ones I listed in the post) and use a cryptographic hash or digital signature to get a verifiable and secure output. Do not implement the cryptographic algorithms yourself either; there are lots of vulnerability pitfalls that take lots of knowledge to avoid. Use a well-known and trusted library for the implementation of the algorithms.

重要更新:自从这篇文章以来,我学到了一些关于密码学的知识,我认为构成一个任意组合(基本上是你自己的哈希)几乎肯定是一个坏主意。实践中使用的散列函数被构造为表现良好(如碰撞概率低)并且难以破坏(能够构造一个与另一个具有相同散列值的值)。构建这样一个函数是一个非常困难的计算机科学问题,除非你有资格,否则你不应该尝试。正确的方法是连接您可以收集的有关硬件的任何信息(即我在帖子中列出的信息),并使用加密哈希或数字签名来获得可验证且安全的输出。也不要自己实现加密算法;有许多漏洞陷阱需要大量知识才能避免。使用知名且值得信赖的库来实现算法。

回答by Xeo

If you're using Visual Studio, Microsoft provides the __cpuidinstrinsic in the <intrin.h>header. Example on the linked msdn site.

如果您使用的是 Visual Studio,Microsoft 会__cpuid<intrin.h>标头中提供内在信息。链接的 msdn 站点上的示例。

回答by maverik

Hm...

嗯...

There are special libraries to generate unique ID based on the hardware installed (so for the specified computer this ID always be the same). Most of them takes motherboard ID + HDD ID + CPU ID and mix these values.

有特殊的库可以根据安装的硬件生成唯一 ID(因此对于指定的计算机,此 ID 始终相同)。它们中的大多数采用主板 ID + HDD ID + CPU ID 并混合这些值。

Whe reinvent the wheel? Why not to use these libraries? Any serious reason?

谁在重新发明轮子?为什么不使用这些库?有什么严重的原因吗?