rippled
hardened_hash.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2014 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_BASICS_HARDENED_HASH_H_INCLUDED
21 #define RIPPLE_BASICS_HARDENED_HASH_H_INCLUDED
22 
23 #include <ripple/beast/hash/hash_append.h>
24 #include <ripple/beast/hash/xxhasher.h>
25 
26 #include <cstdint>
27 #include <functional>
28 #include <mutex>
29 #include <utility>
30 #include <random>
31 #include <type_traits>
32 #include <unordered_map>
33 #include <unordered_set>
34 
35 namespace ripple {
36 
37 namespace detail {
38 
40 
41 template <bool = true>
43 make_seed_pair() noexcept
44 {
45  struct state_t
46  {
47  std::mutex mutex;
49  std::mt19937_64 gen;
51 
52  state_t() : gen(rng()) {}
53  // state_t(state_t const&) = delete;
54  // state_t& operator=(state_t const&) = delete;
55  };
56  static state_t state;
57  std::lock_guard lock(state.mutex);
58  return {state.dist(state.gen), state.dist(state.gen)};
59 }
60 
61 }
62 
93 template <class HashAlgorithm = beast::xxhasher>
95 {
96 private:
98 
99 public:
100  using result_type = typename HashAlgorithm::result_type;
101 
103  : m_seeds (detail::make_seed_pair<>())
104  {}
105 
106  template <class T>
108  operator()(T const& t) const noexcept
109  {
110  HashAlgorithm h(m_seeds.first, m_seeds.second);
111  hash_append(h, t);
112  return static_cast<result_type>(h);
113  }
114 };
115 
116 } // ripple
117 
118 #endif
std::uniform_int_distribution
utility
functional
unordered_set
std::pair< std::uint64_t, std::uint64_t >
random
std::lock_guard
STL class.
ripple::hardened_hash::operator()
result_type operator()(T const &t) const noexcept
Definition: hardened_hash.h:108
ripple::detail::make_seed_pair
seed_pair make_seed_pair() noexcept
Definition: hardened_hash.h:43
std::mt19937_64
ripple::hardened_hash::result_type
typename HashAlgorithm::result_type result_type
Definition: hardened_hash.h:100
std::random_device
ripple::hardened_hash
Seed functor once per construction.
Definition: hardened_hash.h:94
cstdint
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::hardened_hash::m_seeds
detail::seed_pair m_seeds
Definition: hardened_hash.h:97
ripple::hardened_hash::hardened_hash
hardened_hash()
Definition: hardened_hash.h:102
ripple::hash_append
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:151
mutex
unordered_map
type_traits