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 <random>
30 #include <type_traits>
31 #include <unordered_map>
32 #include <unordered_set>
33 #include <utility>
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  {
54  }
55  // state_t(state_t const&) = delete;
56  // state_t& operator=(state_t const&) = delete;
57  };
58  static state_t state;
59  std::lock_guard lock(state.mutex);
60  return {state.dist(state.gen), state.dist(state.gen)};
61 }
62 
63 } // namespace detail
64 
95 template <class HashAlgorithm = beast::xxhasher>
97 {
98 private:
100 
101 public:
102  using result_type = typename HashAlgorithm::result_type;
103 
104  hardened_hash() : m_seeds(detail::make_seed_pair<>())
105  {
106  }
107 
108  template <class T>
110  operator()(T const& t) const noexcept
111  {
112  HashAlgorithm h(m_seeds.first, m_seeds.second);
113  hash_append(h, t);
114  return static_cast<result_type>(h);
115  }
116 };
117 
118 } // namespace ripple
119 
120 #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:110
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:102
std::random_device
ripple::hardened_hash
Seed functor once per construction.
Definition: hardened_hash.h:96
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:99
ripple::hardened_hash::hardened_hash
hardened_hash()
Definition: hardened_hash.h:104
mutex
ripple::hash_append
void hash_append(Hasher &h, ValidatorBlobInfo const &blobInfo)
Definition: ValidatorList.h:897
unordered_map
type_traits