From 2cf09147b5e7aa755fa3f5b68ab95818c77e89d7 Mon Sep 17 00:00:00 2001 From: Nadav Har'El Date: Thu, 3 Mar 2016 00:44:36 +0200 Subject: [PATCH] Repair: don't use freeze() to calculate mutation checksums Use the existing "feed_hash" mechanism to find a checksum of the content of a mutation, instead of serializing the mutation (with freeze()) and then finding the checksum of that string. The serialized form is more prone to future changes, and not really guaranteed to provide equal hashes for mutations which are considered "equal". Fixes #971 Signed-off-by: Nadav Har'El Message-Id: <1456958676-27121-1-git-send-email-nyh@scylladb.com> --- repair/repair.cc | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/repair/repair.cc b/repair/repair.cc index 63e95d28f6..469dab2fff 100644 --- a/repair/repair.cc +++ b/repair/repair.cc @@ -247,18 +247,26 @@ static void check_in_shutdown() { repair_tracker.check_in_shutdown(); } +class sha256_hasher { + CryptoPP::SHA256 hash{}; +public: + void update(const char* ptr, size_t length) { + static_assert(sizeof(char) == sizeof(byte), "Assuming lengths will be the same"); + hash.Update(reinterpret_cast(ptr), length * sizeof(byte)); + } + + void finalize(std::array& digest) { + static_assert(CryptoPP::SHA256::DIGESTSIZE == std::tuple_size>::value * sizeof(digest[0]), + "digest size"); + hash.Final(reinterpret_cast(digest.data())); + } +}; + partition_checksum::partition_checksum(const mutation& m) { - auto frozen = freeze(m); - auto bytes = frozen.representation(); - CryptoPP::SHA256 hash; - static_assert(CryptoPP::SHA256::DIGESTSIZE == std::tuple_size::value * sizeof(_digest[0]), - "digest size"); - static_assert(sizeof(char) == sizeof(decltype(*bytes.data())), - "Assumed that chars are bytes"); - hash.CalculateDigest(reinterpret_cast(_digest.data()), - reinterpret_cast(bytes.data()), - bytes.size()); + sha256_hasher h; + feed_hash(h, m); + h.finalize(_digest); } static inline unaligned& qword(std::array& b, int n) {