blob: 45d6ba5998b6922f7122631298e02696b0c56168 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
|