change:uuid生成改成random模拟

This commit is contained in:
taynpg 2025-04-07 23:43:37 +08:00
parent 931380105b
commit fbc26d565f
2 changed files with 31 additions and 31 deletions

View File

@ -47,7 +47,7 @@ public:
public: public:
static ofString now_time(); static ofString now_time();
static ofString get_file_size(long long bytes); 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_ms();
static uint64_t get_timestamp_s(); static uint64_t get_timestamp_s();
}; };

View File

@ -1,18 +1,13 @@
#include "of_util.h" #include "of_util.h"
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include <random>
#include <sstream> #include <sstream>
#include <utf8.h> #include <utf8.h>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif #endif
// 这里这样写是为了处理头文件排序问题
#ifdef _WIN32
#include <rpcdce.h>
#else
#include <uuid/uuid.h>
#endif
namespace ofen { namespace ofen {
void CMutBuffer::push(const char* data, int len) void CMutBuffer::push(const char* data, int len)
@ -123,31 +118,36 @@ ofString OfUtil::get_file_size(long long bytes)
return oss.str(); return oss.str();
} }
ofString OfUtil::get_uuid() ofString OfUtil::get_sim_uuid()
{ {
#ifdef _WIN32 static std::random_device rd;
UUID uuid; static std::mt19937 gen(rd());
UuidCreate(&uuid); static std::uniform_int_distribution<uint16_t> dis(0, 255);
ofChar* pUuid = nullptr;
#ifdef UNICODE_OFSTR // UUID 格式:xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
UuidToStringW(&uuid, (RPC_CSTR*)&pUuid); // 版本4规则:第13位设为 '4',第17位高4位设为 '8'、'9'、'a' 或 'b'
#else std::stringstream ss;
UuidToStringA(&uuid, (RPC_CSTR*)&pUuid);
#endif // 生成16字节的随机数据
ofString ret(pUuid); for (int i = 0; i < 16; ++i) {
#ifdef UNICODE_OFSTR uint8_t byte = dis(gen);
RpcStringFreeW((RPC_CSTR*)&pUuid);
#else // 根据UUID规范调整特定字节
RpcStringFreeA((RPC_CSTR*)&pUuid); if (i == 6) {
#endif byte = (byte & 0x0F) | 0x40; // 版本4:第7字节高4位设为0100
return ret; } else if (i == 8) {
#else byte = (byte & 0x3F) | 0x80; // 变体:第9字节高2位设为10
uuid_t uuid; }
uuid_generate(uuid);
char uuid_str[37]; // 将字节转为16进制字符串(2位)
uuid_unparse(uuid, uuid_str); ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
return ofString(uuid_str);
#endif // 插入分隔符:8-4-4-4-12
if (i == 3 || i == 5 || i == 7 || i == 9) {
ss << "-";
}
}
return ss.str();
} }
uint64_t OfUtil::get_timestamp_ms() uint64_t OfUtil::get_timestamp_ms()