diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2026-04-11 18:12:51 +0100 |
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2026-04-11 18:12:51 +0100 |
| commit | 3ce6cf305572709332d7329674ec45c987a093ad (patch) | |
| tree | 75a018f6dcf929182961bddca7601181fac415e4 /src/ingestor.cpp | |
| parent | 72bfa9dd305258789b0d2e80f8af13962e5aac42 (diff) | |
| download | webstat-3ce6cf305572709332d7329674ec45c987a093ad.tar.bz2 webstat-3ce6cf305572709332d7329674ec45c987a093ad.tar.xz webstat-3ce6cf305572709332d7329674ec45c987a093ad.zip | |
Introduce MD5 from libmd, use it for hashing queuedLines for park path
Diffstat (limited to 'src/ingestor.cpp')
| -rw-r--r-- | src/ingestor.cpp | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp index 0659f1f..5ae2487 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -24,6 +24,32 @@ namespace DB { namespace WebStat { namespace { + using ByteArrayView = std::span<const uint8_t>; + + auto + bytesToHexRange(const ByteArrayView bytes) + { + constexpr auto HEXN = 16ZU; + return bytes | std::views::transform([](auto byte) { + return std::array {byte / HEXN, byte % HEXN}; + }) | std::views::join + | std::views::transform([](auto nibble) { + return "0123456789abcdef"[nibble]; + }); + } + + EntityHash + makeHash(const std::string_view value) + { + MD5_CTX ctx {}; + MD5Init(&ctx); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - correct for md5ing raw bytes + MD5Update(&ctx, reinterpret_cast<const uint8_t *>(value.data()), value.length()); + EntityHash hash {}; + MD5Final(hash.data(), &ctx); + return hash; + } + Crc32Value crc32(const std::string_view value) { @@ -281,8 +307,8 @@ namespace WebStat { if (queuedLines.empty()) { return std::unexpected(0); } - const std::filesystem::path path { - settings.fallbackDir / std::format("parked-{}.short", crc32(queuedLines.front()))}; + const std::filesystem::path path {settings.fallbackDir + / std::format("parked-{:s}.short", bytesToHexRange(makeHash(queuedLines.front())))}; if (auto parked = FilePtr(fopen(path.c_str(), "w"))) { fprintf(parked.get(), "%zu\n", queuedLines.size()); for (const auto & line : queuedLines) { |
