libuuid介紹
libuuid是一個開源的用於生成UUID(Universally Unique Identifier,通用唯一標識符)的庫,它的源碼可從https://sourceforge.net/projects/libuuid/ 下載,此庫僅支持在類Linux下編譯使用。UUID來源於OSF(Open Software Foundation,開源軟件基金會)的DCE(Distributed Computing Environment,分佈式計算環境)規範,UUID是指在一臺機器上生成的數字,保證在全球範圍的唯一性。
功能需求
平時在開發中可能需要得到一個全局唯一的標識,這時候可以使用UUID(通用唯一標識符),下邊的示例是自己之前在做加解密項目時用到的代碼塊,功能是獲取一個32字節長度的隨機串作爲公鑰,以做爲對數據加密使用。
#include <uuid/uuid.h>
#include <string>
#include <string.h>
int CreateGuid(uuid_t *outuuid)
{
uuid_generate(reinterpret_cast<unsigned char *>(outuuid));
return 0;
}
string CreateStringGuid()
{
char buf[64] = {0};
uuid_t guid;
CreateGuid(&guid);
for(int i=0; i<16; i++){
snprintf(&buf[i*2],sizeof(buf)-i*2, "%02x",guid[i]);
}
return std::string(buf);
}