Boost 库uuid 的使用
UUID 简介
通用唯一识别码(英语:Universally Unique Identifier,简称UUID)是一种软件建构的标准,亦为开放软件基金会组织在分布式计算环境领域的一部分。
uuid 版本
先看几个例子
Nil/Empty: 00000000-0000-0000-0000-000000000000
UUID v1 : 16824f40-aec1-11e8-a69c-f7a44da04f19
UUID v4 : be1f4811-fd38-4bde-933a-1314dea3e436
UUID v5 : c296b0ad-4d8d-5878-97f6-ae9430684dff
UUID 的第三段的第一个字符,表示UUID的版本号,到目前,有5个版本。
- UUID_v1 : 使用MAC 地址和戳来生成,在过去,唯一性非常好,但是会暴露生成者的主机信息,据说历史上有因UUID v1的使用导致生成者被攻击的。在服务器网卡MAC基本都是随机出来的今天,比较少见了。
- UUID_v2 : 类似 v1, 增加了 DEC security, 因为这个版本没有提供任何实现细节,所以很多的实现都是实现了自己的 v2 版uuid。
- UUID_v3&v5: 这两个版本都是使用一个指定的UUID作为命名空间,然后对一个给定的Name进行Hash 之后生成 UUID, 相同命名空间和相同名称生成出来的UUID是相同的,v3和v5 的区别是, v3 使用MD5 ,而v5 使用 sha1。
- UUID_v4 :这个版本就是使用随机数生成UUID。一般算法都是使用随机数填充整个UUID,然后修改版本位及其它的几个位。
boost 库实现了 UUID 的v4 和 v5.
头文件
使用boost库的uuid,一般需要三个头文件
头文件 <boost/uuid/uuid.hpp>
定义了 UUID 的类型
头文件 <boost/uuid/uuid_generators.hpp>
实现了几种UUID的生成算法
头文件 <boost/uuid/uuid_io.hpp>
实现了UUID 类型的流操作
示例
源代码:
#include <iostream>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <array>
#include <sstream>
#include <string>
#include <random>
namespace uuids = ::boost::uuids;
void compare(const uuids::uuid &l, const uuids::uuid& r) {
std::ostringstream ss;
ss << "{" << l << "}";
if (l > r) {
ss << " > ";
} else if (l == r) {
ss << " = ";
} else {
ss << " < ";
}
ss << "{"<< r << "}"<< std::endl;
std::cout << ss.str();
}
void split_line() {
std::cout << "-------------------------------------------------------------------" << std::endl;
}
int main()
{
std::array<uuids::uuid, 5> ids;
uuids::random_generator uuid_v4;
uuids::random_generator uuid_v4_2;
std::cout << "generate uuid with random_generator(uuid version 4)" << std::endl;
split_line();
for (size_t i = 0; i < ids.size(); ++i) {
if (i % 2 == 0)
ids[i] = uuid_v4();
else
ids[i] = uuid_v4_2();
std::cout << ids[i] << std::endl;
}
std::cout << std::endl;
// convert uuid to string
std::string id_s = uuids::to_string(ids[0]);
std::cout << "uuid compare" << std::endl;
split_line();
for (size_t i = 0; i + 1 < ids.size(); ++i) {
compare(ids.at(i), ids.at(i + 1));
}
compare(ids.at(0), ids.at(0));
std::cout << std::endl;
std::cout << "generate uuid with name_generator(uuid version 5)" << std::endl;
split_line();
uuids::name_generator uuid_v5(uuids::string_generator()("{1E7F9D92-8499-44F9-9958-20445DDBBCDB}"));
auto id = uuid_v5("abcdef");
std::cout << id << std::endl;
id = uuid_v5("abcdef");
std::cout << id << std::endl;
std::cout << std::endl;
std::cout << "generate uuid with nil_generator(empty uuid)" << std::endl;
split_line();
id = uuids::nil_generator()();
std::cout << id << std::endl;
std::cout << std::endl;
std::cout << "generate uuid with string_generator(parse from string)" << std::endl;
split_line();
std::array<std::string, 8> inputs{ "abcdef1234567890abcdef1234567890",
"{abcdef1234567890abcdef1234567890}",
"abcdef12-3456-7890-abcd-ef1234567890",
"{abcdef12-3456-7890-abcd-ef1234567890}",
"[abcdef12-3456-7890-abcd-ef1234567890]",
"[abcdef1234567890abcdef1234567890]",
"helloworldaaaaaaaaaaaaaaaaaaaaaaaaaa",
"nothingbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"};
for (auto input : inputs) {
auto id = uuids::string_generator()(input);
std::cout << input << ":\t{" << id << "}" << std::endl;
}
}
输出:
generate uuid with random_generator(uuid version 4)
-------------------------------------------------------------------
0727e06d-0ec3-4ade-bb98-e84d0376e158
e8549db9-a364-4575-ba9f-fc3122df8d55
5d96dc8a-4bbc-4c1b-a17b-3ef99a023192
56497310-7abc-43a9-b406-0affe1df1d96
80d6bd27-51eb-4581-b68c-b12574fbbe1f
uuid compare
-------------------------------------------------------------------
{0727e06d-0ec3-4ade-bb98-e84d0376e158} < {e8549db9-a364-4575-ba9f-fc3122df8d55}
{e8549db9-a364-4575-ba9f-fc3122df8d55} > {5d96dc8a-4bbc-4c1b-a17b-3ef99a023192}
{5d96dc8a-4bbc-4c1b-a17b-3ef99a023192} > {56497310-7abc-43a9-b406-0affe1df1d96}
{56497310-7abc-43a9-b406-0affe1df1d96} < {80d6bd27-51eb-4581-b68c-b12574fbbe1f}
{0727e06d-0ec3-4ade-bb98-e84d0376e158} = {0727e06d-0ec3-4ade-bb98-e84d0376e158}
generate uuid with name_generator(uuid version 5)
-------------------------------------------------------------------
7367fb7c-1fff-573f-853d-49db6bca0ac7
7367fb7c-1fff-573f-853d-49db6bca0ac7
generate uuid with nil_generator(empty uuid)
-------------------------------------------------------------------
00000000-0000-0000-0000-000000000000
generate uuid with string_generator(parse from string)
-------------------------------------------------------------------
abcdef1234567890abcdef1234567890: {abcdef12-3456-7890-abcd-ef1234567890}
{abcdef1234567890abcdef1234567890}: {abcdef12-3456-7890-abcd-ef1234567890}
abcdef12-3456-7890-abcd-ef1234567890: {abcdef12-3456-7890-abcd-ef1234567890}
{abcdef12-3456-7890-abcd-ef1234567890}: {abcdef12-3456-7890-abcd-ef1234567890}
[abcdef12-3456-7890-abcd-ef1234567890]: {fabcdef1-ff34-56f7-89ff-abcdfef12345}
[abcdef1234567890abcdef1234567890]: {fabcdef1-2345-6789-0abc-def123456789}
helloworldaaaaaaaaaaaaaaaaaaaaaaaaaa: {feffffff-fdaa-aaaa-aaaa-aaaaaaaaaaaa}
nothingbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: {fffffffb-bbbb-bbbb-bbbb-bbbbbbbbbbbb}