CCF
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/ds/siphash.h"
6
7#include <array>
8#include <cstdint>
9#include <small_vector/SmallVector.h>
10#include <string_view>
11#include <vector>
12
14{
15 template <typename T>
16 inline void hash_combine(size_t& n, const T& v, std::hash<T>& h)
17 {
18 n ^= h(v) + (n << 6) + (n >> 2);
19 }
20
21 template <typename T>
22 inline size_t hash_container(const T& v)
23 {
24 size_t n = 0x444e414c544f4353;
25 std::hash<typename T::value_type> h{};
26
27 for (const auto& e : v)
28 {
29 hash_combine(n, e, h);
30 }
31
32 return n;
33 }
34}
35
36// NOLINTBEGIN(cert-dcl58-cpp)
37namespace std
38{
39 template <>
40 struct hash<std::vector<uint8_t>>
41 {
42 size_t operator()(const std::vector<uint8_t>& v) const
43 {
44 // For cryptographically secure hashing, use SipHash directly with a
45 // secret key. For std::hash, we use this fixed key
46 static constexpr ccf::siphash::SipKey k{
47 0x7720796f726c694b, 0x2165726568207361};
48 return ccf::siphash::siphash<2, 4>(v, k);
49 }
50 };
51
52 template <typename T>
53 struct hash<std::vector<T>>
54 {
55 size_t operator()(const std::vector<T>& v) const
56 {
58 }
59 };
60
61 template <typename T, size_t N>
62 struct hash<std::array<T, N>>
63 {
64 size_t operator()(const std::array<T, N>& v) const
65 {
67 }
68 };
69
70 template <typename A, typename B>
71 struct hash<std::pair<A, B>>
72 {
73 size_t operator()(const std::pair<A, B>& v) const
74 {
75 size_t n = 0x444e414c544f4353;
76
77 std::hash<A> h_a{};
78 ccf::ds::hashutils::hash_combine(n, v.first, h_a);
79
80 std::hash<B> h_b{};
81 ccf::ds::hashutils::hash_combine(n, v.second, h_b);
82
83 return n;
84 }
85 };
86
87}
88// NOLINTEND(cert-dcl58-cpp)
89
90namespace ccf::ds
91{
93 namespace fnv
94 {
95 template <typename T>
97 {};
98
99 template <>
100 struct fnv_parameters<uint32_t>
101 {
102 static constexpr uint32_t offset_basis = 0x811c9dc5;
103 static constexpr uint32_t prime = 16777619;
104 };
105
106 template <>
107 struct fnv_parameters<uint64_t>
108 {
109 static constexpr uint64_t offset_basis = 0xcbf29ce484222325;
110 static constexpr uint64_t prime = 1099511628211;
111 };
112 }
113
114 template <typename T>
115 static constexpr T fnv_1a(const std::string_view& sv)
116 {
117 using params = fnv::fnv_parameters<T>;
118
119 T hash = params::offset_basis;
120
121 for (const auto& c : sv)
122 {
123 hash ^= c;
124 hash *= params::prime;
125 }
126
127 return hash;
128 }
129}
Definition hash.h:14
void hash_combine(size_t &n, const T &v, std::hash< T > &h)
Definition hash.h:16
size_t hash_container(const T &v)
Definition hash.h:22
Definition contiguous_set.h:12
uint64_t[2] SipKey
Definition siphash.h:15
STL namespace.
Definition hash.h:97
size_t operator()(const std::array< T, N > &v) const
Definition hash.h:64
size_t operator()(const std::pair< A, B > &v) const
Definition hash.h:73
size_t operator()(const std::vector< T > &v) const
Definition hash.h:55
size_t operator()(const std::vector< uint8_t > &v) const
Definition hash.h:42