C++ 中独立于平台的 GUID 生成?

发布于 2024-07-13 09:57:08 字数 203 浏览 4 评论 0原文

在 C++ 中以编程方式生成 GUID 或 UUID 而不依赖于特定于平台的工具的最佳方法是什么? 我正在尝试为模拟中的对象创建唯一标识符,但不能依赖 Microsoft 的实现,因为该项目是跨平台的。

注意:

  • 由于这是针对模拟器的,所以我 并不真正需要加密 随机性。
  • 最好是 32 位数字。

What is the best way to programmatically generate a GUID or UUID in C++ without relying on a platform-specific tool? I am trying to make unique identifiers for objects in a simulation, but can't rely on Microsoft's implementation as the project is cross-platform.

Notes:

  • Since this is for a simulator, I
    don't really need cryptographic
    randomness.
  • It would be best if this is a 32 bit number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

も星光 2024-07-20 09:57:08

如果您有能力使用 Boost,那么有一个 UUID 库应该可以解决这个问题。 使用起来非常简单 - 查看文档和此答案

If you can afford to use Boost, then there is a UUID library that should do the trick. It's very straightforward to use - check the documentation and this answer.

看透却不说透 2024-07-20 09:57:08

在 linux 上: man uuid

在 win 上:在 msdn 中查看 UUID 结构和 UuidCreate 函数

[编辑] 该函数将如下所示

extern "C"
{
#ifdef WIN32
#include <Rpc.h>
#else
#include <uuid/uuid.h>
#endif
}

std::string newUUID()
{
#ifdef WIN32
    UUID uuid;
    UuidCreate ( &uuid );

    unsigned char * str;
    UuidToStringA ( &uuid, &str );

    std::string s( ( char* ) str );

    RpcStringFreeA ( &str );
#else
    uuid_t uuid;
    uuid_generate_random ( uuid );
    char s[37];
    uuid_unparse ( uuid, s );
#endif
    return s;
}

on linux: man uuid

on win: check out for UUID structure and UuidCreate function in msdn

[edit] the function would appear like this

extern "C"
{
#ifdef WIN32
#include <Rpc.h>
#else
#include <uuid/uuid.h>
#endif
}

std::string newUUID()
{
#ifdef WIN32
    UUID uuid;
    UuidCreate ( &uuid );

    unsigned char * str;
    UuidToStringA ( &uuid, &str );

    std::string s( ( char* ) str );

    RpcStringFreeA ( &str );
#else
    uuid_t uuid;
    uuid_generate_random ( uuid );
    char s[37];
    uuid_unparse ( uuid, s );
#endif
    return s;
}
月下凄凉 2024-07-20 09:57:08

如果您无法负担使用 Boost,那么我实现了一个非常小的库,它只是充当每个操作系统的本机 guid 实现的包装器。 它应该适用于 Windows(使用 CoCreateGuid)、Linux(使用 libuuid)、MacOS(使用 CFUUID)、iOS(也使用 CFUUID)和 Android(使用 JNI 调用 java.util.UUID)。 guid 生成函数在每个系统上都有不同的实现,但有用于比较、字符串化和解析的单一通用实现。

它已获得 MIT 许可,可在 GitHub 上获取:

https://github.com/graeme-hill/crossguid

If you cannot afford to use Boost, then there is a very minimal library that I implemented which simply acts as a wrapper around each operating system's native guid implementation. It should work on Windows (using CoCreateGuid), Linux (using libuuid), MacOS (using CFUUID), iOS (also using CFUUID), and Android (using JNI calls to java.util.UUID). The guid generation function has a different implementation on each system but there is single generic implementation for comparing, stringifying, and parsing.

It is MIT licensed and available on GitHub:

https://github.com/graeme-hill/crossguid

空心↖ 2024-07-20 09:57:08

最好只使用目标平台上存在的任何 guid/uuid。 正确地做起来很难(让我们去购物吧;))。

来自不同但执行良好的实现的任何两个标识符之间发生冲突的概率应该远远超出在这个宇宙的生命周期中发生的任何合理机会。

Simply using whatever guid/uuid is present on the target platform is best. Doing it right is hard (let's go shopping ;)).

The probability of a collision between any two identifiers from different but well executed implementations should be well beyond any reasonable chance of happening in this universe's lifetime.

我要还你自由 2024-07-20 09:57:08

GUID 生成器通常依赖于机器的硬件特征,通常是 MAC 地址或 IP 之类的东西。 您唯一可以做的完全独立于平台的事情是使用某种 PRNG 手动播种或从某个时间源播种。 这通常不会生成真正的全球唯一标识符,因为可以保证世界上没有人生成相同的号码。

A GUID generator usually relies on hardware characteristics of the machine, usually stuff like MAC addresses or IPs. The only thing you can do which is absolutely platform independent is use some kind of PRNG seeded manually or seeded from some time source. This usually does not generate a true globally unique identifier in the sense that you are guaranteed that no one in the world generates the same number.

迷雾森÷林ヴ 2024-07-20 09:57:08

好吧,一种另类(?)的解决方案是使用 Web 服务来获取 GUID,但我怀疑这对于 C++ 程序来说是否是一个好的解决方案,特别是如果它没有启用网络。

如果您选择采用此选项,以下 URL 可能会派上用场: http://www .hoskinson.net/GuidGenerator/default.asp

Well one offbeat(?) solution would be to use a web service to get the GUID but I doubt this will be a good solution for a C++ program especially if it's not network enabled.

Here is a URL which might come in handy if you choose to pursue this option: http://www.hoskinson.net/GuidGenerator/default.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文