Skip to content

Commit 516c738

Browse files
committed
Generate a slightly more compact hash string
1 parent 92e8f69 commit 516c738

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

Development/nmos/mdns.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "nmos/mdns.h"
22

33
#include <functional>
4-
#include <iomanip>
54
#include <boost/algorithm/string/erase.hpp>
65
#include <boost/algorithm/string/join.hpp>
76
#include <boost/algorithm/string/predicate.hpp>
@@ -332,11 +331,17 @@ namespace nmos
332331
return utility::us2s(nmos::fields::service_name_prefix(settings)) + "_" + service_api(service);
333332
}
334333

335-
inline std::string hash_string(const std::string& s)
334+
// generate a hash string (slightly more compact and possibly slightly more memorable than hex)
335+
inline std::string hash_string(const std::string& s, size_t len = 5)
336336
{
337-
std::ostringstream os;
338-
os << std::hex << std::setfill('0') << std::setw(8) << (std::hash<std::string>{}(s) & 0xFFFFFFFF);
339-
return os.str();
337+
auto hash = std::hash<std::string>{}(s);
338+
// vowels (and vowel-ish digits) are omitted from the set of available characters
339+
// to reduce the chances of "bad words" being formed
340+
static const char alphanums[] = "bcdfghjklmnpqrstvwxz2456789";
341+
static const size_t base = sizeof(alphanums) - 1;
342+
std::string result(len, ' ');
343+
for (auto& c : result) { c = alphanums[hash % base]; hash /= base; }
344+
return result;
340345
}
341346

342347
inline std::set<nmos::api_version> service_versions(const nmos::service_type& service, const nmos::settings& settings)

Development/nmos/test/mdns_test.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ BST_TEST_CASE(testServiceNameTruncationSuffix)
116116

117117
const size_t max_length = 63;
118118

119-
// the truncated name should end with a dash followed by an 8-character hex hash
119+
// the truncated name should end with a dash followed by a 5-character alphanumeric hash
120120
const nmos::settings settings = value_of({
121121
{ nmos::fields::host_name, U("a-host-name-that-is-itself-valid-but-already-63-characters-long.example.com") },
122122
{ nmos::experimental::fields::href_mode, 1 }
@@ -128,12 +128,11 @@ BST_TEST_CASE(testServiceNameTruncationSuffix)
128128
const auto last_dash = name.rfind('-');
129129
BST_REQUIRE(std::string::npos != last_dash);
130130

131-
// the suffix after the last dash should be exactly 8 hex characters
132131
const auto suffix = name.substr(last_dash + 1);
133-
BST_CHECK_EQUAL(8, suffix.size());
132+
BST_CHECK_EQUAL(5, suffix.size());
134133
BST_CHECK(suffix.end() == std::find_if(suffix.begin(), suffix.end(), [](char c)
135134
{
136-
return !std::isxdigit(static_cast<unsigned char>(c));
135+
return !std::isdigit(static_cast<unsigned char>(c)) && !std::islower(static_cast<unsigned char>(c));
137136
}));
138137
}
139138

0 commit comments

Comments
 (0)