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
36namespace std
37{
38 template <>
39 struct hash<std::vector<uint8_t>>
40 {
41 size_t operator()(const std::vector<uint8_t>& v) const
42 {
43 // For cryptographically secure hashing, use SipHash directly with a
44 // secret key. For std::hash, we use this fixed key
45 static constexpr ccf::siphash::SipKey k{
46 0x7720796f726c694b, 0x2165726568207361};
47 return ccf::siphash::siphash<2, 4>(v, k);
48 }
49 };
50
51 template <typename T>
52 struct hash<std::vector<T>>
53 {
54 size_t operator()(const std::vector<T>& v) const
55 {
57 }
58 };
59
60 template <typename T, size_t N>
61 struct hash<std::array<T, N>>
62 {
63 size_t operator()(const std::array<T, N>& v) const
64 {
66 }
67 };
68
69 template <typename A, typename B>
70 struct hash<std::pair<A, B>>
71 {
72 size_t operator()(const std::pair<A, B>& v) const
73 {
74 size_t n = 0x444e414c544f4353;
75
76 std::hash<A> h_a{};
77 ccf::ds::hashutils::hash_combine(n, v.first, h_a);
78
79 std::hash<B> h_b{};
80 ccf::ds::hashutils::hash_combine(n, v.second, h_b);
81
82 return n;
83 }
84 };
85
86}
87
88namespace ccf::ds
89{
91 namespace
92 {
93 template <typename T>
94 struct fnv_parameters
95 {};
96
97 template <>
98 struct fnv_parameters<uint32_t>
99 {
100 static constexpr uint32_t offset_basis = 0x811c9dc5;
101 static constexpr uint32_t prime = 16777619;
102 };
103
104 template <>
105 struct fnv_parameters<uint64_t>
106 {
107 static constexpr uint64_t offset_basis = 0xcbf29ce484222325;
108 static constexpr uint64_t prime = 1099511628211;
109 };
110 }
111
112 template <typename T>
113 static constexpr T fnv_1a(const std::string_view& sv)
114 {
115 using params = fnv_parameters<T>;
116
117 T hash = params::offset_basis;
118
119 for (const auto& c : sv)
120 {
121 hash ^= c;
122 hash *= params::prime;
123 }
124
125 return hash;
126 }
127}
constexpr element prime
Definition sharing.cpp:22
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:13
STL namespace.
size_t operator()(const std::array< T, N > &v) const
Definition hash.h:63
size_t operator()(const std::pair< A, B > &v) const
Definition hash.h:72
size_t operator()(const std::vector< T > &v) const
Definition hash.h:54
size_t operator()(const std::vector< uint8_t > &v) const
Definition hash.h:41