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 <chrono> #include <iomanip> +#include <random> #include <sstream> #include <utf8.h> #ifdef _WIN32 #include <windows.h> #endif -// 这里这样写是为了处理头文件排序问题 -#ifdef _WIN32 -#include <rpcdce.h> -#else -#include <uuid/uuid.h> -#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<uint16_t> 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<int>(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()