From fbc26d565f7b73418dd9bfaee3fc67b77a56daba Mon Sep 17 00:00:00 2001 From: taynpg Date: Mon, 7 Apr 2025 23:43:37 +0800 Subject: [PATCH] =?UTF-8?q?change=EF=BC=9Auuid=E7=94=9F=E6=88=90=E6=94=B9?= =?UTF-8?q?=E6=88=90random=E6=A8=A1=E6=8B=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/of_util.h | 2 +- src/of_util.cpp | 60 +++++++++++++++++++++++------------------------ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/include/of_util.h b/include/of_util.h index 4bbfd8e..97a6cb7 100644 --- a/include/of_util.h +++ b/include/of_util.h @@ -47,7 +47,7 @@ public: public: static ofString now_time(); static ofString get_file_size(long long bytes); - static ofString get_uuid(); + static ofString get_sim_uuid(); static uint64_t get_timestamp_ms(); static uint64_t get_timestamp_s(); }; diff --git a/src/of_util.cpp b/src/of_util.cpp index 79071f2..d09933c 100644 --- a/src/of_util.cpp +++ b/src/of_util.cpp @@ -1,18 +1,13 @@ #include "of_util.h" #include #include +#include #include #include #ifdef _WIN32 #include #endif -// 这里这样写是为了处理头文件排序问题 -#ifdef _WIN32 -#include -#else -#include -#endif namespace ofen { void CMutBuffer::push(const char* data, int len) @@ -123,31 +118,36 @@ ofString OfUtil::get_file_size(long long bytes) return oss.str(); } -ofString OfUtil::get_uuid() +ofString OfUtil::get_sim_uuid() { -#ifdef _WIN32 - UUID uuid; - UuidCreate(&uuid); - ofChar* pUuid = nullptr; -#ifdef UNICODE_OFSTR - UuidToStringW(&uuid, (RPC_CSTR*)&pUuid); -#else - UuidToStringA(&uuid, (RPC_CSTR*)&pUuid); -#endif - ofString ret(pUuid); -#ifdef UNICODE_OFSTR - RpcStringFreeW((RPC_CSTR*)&pUuid); -#else - RpcStringFreeA((RPC_CSTR*)&pUuid); -#endif - return ret; -#else - uuid_t uuid; - uuid_generate(uuid); - char uuid_str[37]; - uuid_unparse(uuid, uuid_str); - return ofString(uuid_str); -#endif + static std::random_device rd; + static std::mt19937 gen(rd()); + static std::uniform_int_distribution dis(0, 255); + + // UUID 格式:xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx + // 版本4规则:第13位设为 '4',第17位高4位设为 '8'、'9'、'a' 或 'b' + std::stringstream ss; + + // 生成16字节的随机数据 + for (int i = 0; i < 16; ++i) { + uint8_t byte = dis(gen); + + // 根据UUID规范调整特定字节 + if (i == 6) { + byte = (byte & 0x0F) | 0x40; // 版本4:第7字节高4位设为0100 + } else if (i == 8) { + byte = (byte & 0x3F) | 0x80; // 变体:第9字节高2位设为10 + } + + // 将字节转为16进制字符串(2位) + ss << std::hex << std::setw(2) << std::setfill('0') << static_cast(byte); + + // 插入分隔符:8-4-4-4-12 + if (i == 3 || i == 5 || i == 7 || i == 9) { + ss << "-"; + } + } + return ss.str(); } uint64_t OfUtil::get_timestamp_ms()