diff options
author | Francis Dupont <fdupont@isc.org> | 2018-02-20 16:32:45 +0100 |
---|---|---|
committer | Francis Dupont <fdupont@isc.org> | 2018-02-20 16:32:45 +0100 |
commit | f055186394aed9ff1d58a051d77daeb50fc20aa4 (patch) | |
tree | 67fbdd42ae00ac86a2c5c2332faaa4c76b6a73a9 /src/lib/util | |
parent | [master] with-dhcp-xxxsql -> with-xxxsql (diff) | |
download | kea-f055186394aed9ff1d58a051d77daeb50fc20aa4.tar.xz kea-f055186394aed9ff1d58a051d77daeb50fc20aa4.zip |
[5502] Added FNV-1a 64 bit hash and replaced MD5 in CQL host dats source
Diffstat (limited to 'src/lib/util')
-rw-r--r-- | src/lib/util/Makefile.am | 2 | ||||
-rw-r--r-- | src/lib/util/hash.h | 57 | ||||
-rw-r--r-- | src/lib/util/tests/Makefile.am | 1 | ||||
-rw-r--r-- | src/lib/util/tests/hash_unittest.cc | 34 |
4 files changed, 94 insertions, 0 deletions
diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am index 7c0bac0096..f2c684f3ca 100644 --- a/src/lib/util/Makefile.am +++ b/src/lib/util/Makefile.am @@ -11,6 +11,7 @@ libkea_util_la_SOURCES = boost_time_utils.h boost_time_utils.cc libkea_util_la_SOURCES += buffer.h io_utilities.h libkea_util_la_SOURCES += csv_file.h csv_file.cc libkea_util_la_SOURCES += filename.h filename.cc +libkea_util_la_SOURCES += hash.h libkea_util_la_SOURCES += labeled_value.h labeled_value.cc libkea_util_la_SOURCES += memory_segment.h libkea_util_la_SOURCES += memory_segment_local.h memory_segment_local.cc @@ -50,6 +51,7 @@ libkea_util_include_HEADERS = \ buffer.h \ csv_file.h \ filename.h \ + hash.h \ io_utilities.h \ labeled_value.h \ memory_segment.h \ diff --git a/src/lib/util/hash.h b/src/lib/util/hash.h new file mode 100644 index 0000000000..45d6ba5998 --- /dev/null +++ b/src/lib/util/hash.h @@ -0,0 +1,57 @@ +// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef HASH_H +#define HASH_H + +#include <cstddef> +#include <cstdint> +#include <string> + +namespace isc { +namespace util { + +/// @brief Hash implementation based on Fowler-Noll-Vo hash function +/// +struct Hash64 { + /// @brief Compute the hash + /// + /// FNV-1a hash function + /// + /// @param data data to hash + /// @param length length of data + /// @return the hash value + static uint64_t hash(const uint8_t* data, size_t length) { + uint64_t hash = FNV_offset_basis; + for (size_t i = 0; i < length; ++i) { + hash = hash ^ data[i]; + hash = hash * FNV_prime; + } + return (hash); + } + + /// @brief Compute the hash + /// + /// FNV-1a hash function + /// + /// @param str not empty string to hash + /// @return the hash value + static uint64_t hash(const std::string& str) { + return (hash(reinterpret_cast<const uint8_t*>(str.c_str()), + str.size())); + } + + /// @brief Offset basis + static const uint64_t FNV_offset_basis = 14695981039346656037ull; + + /// @brief Prime + static const uint64_t FNV_prime = 1099511628211ull; +}; + +} // end of namespace isc::util +} // end of namespace isc + +#endif diff --git a/src/lib/util/tests/Makefile.am b/src/lib/util/tests/Makefile.am index 301d94f2bb..c6b7173bfa 100644 --- a/src/lib/util/tests/Makefile.am +++ b/src/lib/util/tests/Makefile.am @@ -36,6 +36,7 @@ run_unittests_SOURCES += csv_file_unittest.cc run_unittests_SOURCES += fd_share_tests.cc run_unittests_SOURCES += fd_tests.cc run_unittests_SOURCES += filename_unittest.cc +run_unittests_SOURCES += hash_unittest.cc run_unittests_SOURCES += hex_unittest.cc run_unittests_SOURCES += io_utilities_unittest.cc run_unittests_SOURCES += labeled_value_unittest.cc diff --git a/src/lib/util/tests/hash_unittest.cc b/src/lib/util/tests/hash_unittest.cc new file mode 100644 index 0000000000..f789e51a8a --- /dev/null +++ b/src/lib/util/tests/hash_unittest.cc @@ -0,0 +1,34 @@ +// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include <config.h> + +#include <util/hash.h> + +#include <gtest/gtest.h> + +#include <cstring> +#include <vector> + +using namespace isc::util; +using namespace std; + +namespace { + +TEST(HashTest, empty) { + EXPECT_EQ(14695981039346656037ull, Hash64::hash(0, 0)); +} + +TEST(HashTest, foobar) { + EXPECT_EQ(9625390261332436968ull, Hash64::hash(string("foobar"))); +} + +TEST(HashTest, chongo) { + EXPECT_EQ(5080352029159061781ull, + Hash64::hash(string("chongo was here!\n"))); +} + +} |