From f4fefec343f42e1c5856965e22663f7d770f1719 Mon Sep 17 00:00:00 2001 From: Benny Halevy Date: Mon, 17 Apr 2023 12:29:20 +0300 Subject: [PATCH] utils: hashing: add simple_xx_hasher And a respective unit test. Signed-off-by: Benny Halevy --- test/boost/hashers_test.cc | 15 +++++++++++++++ utils/hashers.cc | 3 +++ utils/simple_hashers.hh | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 utils/simple_hashers.hh diff --git a/test/boost/hashers_test.cc b/test/boost/hashers_test.cc index 169e641130..b8a38f839a 100644 --- a/test/boost/hashers_test.cc +++ b/test/boost/hashers_test.cc @@ -13,6 +13,7 @@ #include #include "utils/hashers.hh" #include "utils/xx_hasher.hh" +#include "utils/simple_hashers.hh" #include "gc_clock.hh" #include "test/lib/simple_schema.hh" #include "reader_concurrency_semaphore.hh" @@ -102,3 +103,17 @@ SEASTAR_THREAD_TEST_CASE(mutation_fragment_sanity_check) { check_hash(f, 0x5092daca1b27ea26ull); } } + +BOOST_AUTO_TEST_CASE(basic_xx_hasher_sanity_check) { + simple_xx_hasher hasher1; + hasher1.update(reinterpret_cast(std::data(text_part1)), std::size(text_part1)); + hasher1.update(reinterpret_cast(std::data(text_part2)), std::size(text_part2)); + auto hash1 = hasher1.finalize(); + + bytes_view_hasher hasher2; + hasher2.update(reinterpret_cast(std::data(text_part1)), std::size(text_part1)); + hasher2.update(reinterpret_cast(std::data(text_part2)), std::size(text_part2)); + auto hash2 = hasher2.finalize(); + + BOOST_CHECK_EQUAL(hash1, hash2); +} diff --git a/utils/hashers.cc b/utils/hashers.cc index 1961efa29f..d89a8372d4 100644 --- a/utils/hashers.cc +++ b/utils/hashers.cc @@ -8,6 +8,7 @@ #include "utils/hashers.hh" #include "utils/xx_hasher.hh" +#include "utils/simple_hashers.hh" #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 #include @@ -19,6 +20,8 @@ static_assert(HasherReturningBytes); static_assert(HasherReturningBytes); static_assert(HasherReturningBytes); +static_assert(SimpleHasher); + template struct hasher_traits; template <> struct hasher_traits { using impl_type = CryptoPP::Weak::MD5; }; template <> struct hasher_traits { using impl_type = CryptoPP::SHA256; }; diff --git a/utils/simple_hashers.hh b/utils/simple_hashers.hh new file mode 100644 index 0000000000..06aff002d3 --- /dev/null +++ b/utils/simple_hashers.hh @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023-present ScyllaDB + */ + +/* + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#pragma once + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Warray-bounds" +#include +#pragma GCC diagnostic pop + +#include "utils/hashing.hh" + +template +concept SimpleHasher = HasherReturning; + +struct simple_xx_hasher : public hasher { + XXH64_state_t _state; + simple_xx_hasher(uint64_t seed = 0) noexcept { + XXH64_reset(&_state, seed); + } + void update(const char* ptr, size_t length) noexcept override { + XXH64_update(&_state, ptr, length); + } + size_t finalize() { + return static_cast(XXH64_digest(&_state)); + } +};