Skip to content

Commit eac53b7

Browse files
author
talitvin
committed
feat userver: add boost uuidv5
(https://nda.ya.ru/t/cJ3O-i3V7Z4UZp commit_hash:24dde96d4557ad55dae15f81ab479cd20a343aa5
1 parent c40f600 commit eac53b7

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

.mapping.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5497,6 +5497,7 @@
54975497
"universal/include/userver/utils/atomic.hpp":"taxi/uservices/userver/universal/include/userver/utils/atomic.hpp",
54985498
"universal/include/userver/utils/boost_filesystem_file_status.hpp":"taxi/uservices/userver/universal/include/userver/utils/boost_filesystem_file_status.hpp",
54995499
"universal/include/userver/utils/boost_uuid4.hpp":"taxi/uservices/userver/universal/include/userver/utils/boost_uuid4.hpp",
5500+
"universal/include/userver/utils/boost_uuid5.hpp":"taxi/uservices/userver/universal/include/userver/utils/boost_uuid5.hpp",
55005501
"universal/include/userver/utils/boost_uuid7.hpp":"taxi/uservices/userver/universal/include/userver/utils/boost_uuid7.hpp",
55015502
"universal/include/userver/utils/box.hpp":"taxi/uservices/userver/universal/include/userver/utils/box.hpp",
55025503
"universal/include/userver/utils/bytes_per_second.hpp":"taxi/uservices/userver/universal/include/userver/utils/bytes_per_second.hpp",
@@ -5814,6 +5815,8 @@
58145815
"universal/src/utils/atomic_test.cpp":"taxi/uservices/userver/universal/src/utils/atomic_test.cpp",
58155816
"universal/src/utils/boost_uuid4.cpp":"taxi/uservices/userver/universal/src/utils/boost_uuid4.cpp",
58165817
"universal/src/utils/boost_uuid4_test.cpp":"taxi/uservices/userver/universal/src/utils/boost_uuid4_test.cpp",
5818+
"universal/src/utils/boost_uuid5.cpp":"taxi/uservices/userver/universal/src/utils/boost_uuid5.cpp",
5819+
"universal/src/utils/boost_uuid5_test.cpp":"taxi/uservices/userver/universal/src/utils/boost_uuid5_test.cpp",
58175820
"universal/src/utils/boost_uuid7.cpp":"taxi/uservices/userver/universal/src/utils/boost_uuid7.cpp",
58185821
"universal/src/utils/boost_uuid7_test.cpp":"taxi/uservices/userver/universal/src/utils/boost_uuid7_test.cpp",
58195822
"universal/src/utils/boost_uuids_benchmark.cpp":"taxi/uservices/userver/universal/src/utils/boost_uuids_benchmark.cpp",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
/// @file userver/utils/boost_uuid5.hpp
4+
/// @brief @copybrief utils::generators::GenerateBoostUuidV5()
5+
6+
#include <string_view>
7+
8+
#include <boost/uuid/uuid.hpp>
9+
10+
USERVER_NAMESPACE_BEGIN
11+
12+
namespace utils::generators {
13+
14+
/// RFC 9562 name-based UUID namespace IDs (same as boost::uuids::ns::*, see boost_uuid5.cpp).
15+
namespace ns {
16+
17+
extern const boost::uuids::uuid kDns;
18+
extern const boost::uuids::uuid kUrl;
19+
extern const boost::uuids::uuid kOid;
20+
extern const boost::uuids::uuid kX500dn;
21+
22+
} // namespace ns
23+
24+
/// Generates UUIDv5 (name-based, SHA-1) for the given namespace and name
25+
///
26+
/// @see https://datatracker.ietf.org/doc/html/rfc9562#section-5.5
27+
boost::uuids::uuid GenerateBoostUuidV5(const boost::uuids::uuid& namespace_uuid, std::string_view name);
28+
29+
} // namespace utils::generators
30+
31+
USERVER_NAMESPACE_END
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <userver/utils/boost_uuid5.hpp>
2+
3+
#include <boost/uuid/name_generator_sha1.hpp>
4+
5+
USERVER_NAMESPACE_BEGIN
6+
7+
namespace utils::generators {
8+
9+
namespace ns {
10+
11+
// RFC 4122 predefined name-based UUID namespaces (same bytes as boost::uuids::ns::*).
12+
const boost::uuids::uuid kDns{
13+
{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
14+
};
15+
const boost::uuids::uuid kUrl{
16+
{0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
17+
};
18+
const boost::uuids::uuid kOid{
19+
{0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
20+
};
21+
const boost::uuids::uuid kX500dn{
22+
{0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
23+
};
24+
25+
} // namespace ns
26+
27+
boost::uuids::uuid GenerateBoostUuidV5(const boost::uuids::uuid& namespace_uuid, std::string_view name) {
28+
return boost::uuids::name_generator_sha1{namespace_uuid}(name.data(), name.size());
29+
}
30+
31+
} // namespace utils::generators
32+
33+
USERVER_NAMESPACE_END
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <userver/utils/boost_uuid4.hpp>
2+
#include <userver/utils/boost_uuid5.hpp>
3+
4+
#include <string>
5+
6+
#include <gtest/gtest.h>
7+
8+
USERVER_NAMESPACE_BEGIN
9+
10+
TEST(UUID, BoostV5UrlNamespaceMatchesRfcVector) {
11+
const std::string name = "https://example.com/product/image.png";
12+
13+
const auto once = utils::generators::GenerateBoostUuidV5(utils::generators::ns::kUrl, name);
14+
15+
EXPECT_EQ(utils::ToString(once), "ae19bb34-8126-55bf-bb41-f73c17cee8a3");
16+
EXPECT_EQ(once, utils::generators::GenerateBoostUuidV5(utils::generators::ns::kUrl, name));
17+
}
18+
19+
TEST(UUID, BoostV5UrlNamespaceEmptyNameMatchesRfcVector) {
20+
const auto once = utils::generators::GenerateBoostUuidV5(utils::generators::ns::kUrl, "");
21+
EXPECT_EQ(utils::ToString(once), "1b4db7eb-4057-5ddf-91e0-36dec72071f5");
22+
EXPECT_EQ(once, utils::generators::GenerateBoostUuidV5(utils::generators::ns::kUrl, ""));
23+
}
24+
25+
TEST(UUID, BoostV5DifferentNamesYieldDifferentUuids) {
26+
EXPECT_NE(
27+
utils::generators::GenerateBoostUuidV5(utils::generators::ns::kDns, "a"),
28+
utils::generators::GenerateBoostUuidV5(utils::generators::ns::kDns, "b")
29+
);
30+
EXPECT_NE(
31+
utils::generators::GenerateBoostUuidV5(utils::generators::ns::kUrl, "a"),
32+
utils::generators::GenerateBoostUuidV5(utils::generators::ns::kUrl, "b")
33+
);
34+
}
35+
36+
TEST(UUID, BoostV5DifferentNamespacesYieldDifferentUuids) {
37+
constexpr std::string_view kName = "same";
38+
EXPECT_NE(
39+
utils::generators::GenerateBoostUuidV5(utils::generators::ns::kDns, kName),
40+
utils::generators::GenerateBoostUuidV5(utils::generators::ns::kUrl, kName)
41+
);
42+
}
43+
44+
USERVER_NAMESPACE_END

0 commit comments

Comments
 (0)