rippled
Loading...
Searching...
No Matches
hardened_hash.h
1#ifndef XRPL_BASICS_HARDENED_HASH_H_INCLUDED
2#define XRPL_BASICS_HARDENED_HASH_H_INCLUDED
3
4#include <xrpl/beast/hash/hash_append.h>
5#include <xrpl/beast/hash/xxhasher.h>
6
7#include <cstdint>
8#include <mutex>
9#include <random>
10#include <utility>
11
12namespace ripple {
13
14namespace detail {
15
17
18template <bool = true>
21{
22 struct state_t
23 {
24 std::mutex mutex;
28
29 state_t() : gen(rng())
30 {
31 }
32 // state_t(state_t const&) = delete;
33 // state_t& operator=(state_t const&) = delete;
34 };
35 static state_t state;
36 std::lock_guard lock(state.mutex);
37 return {state.dist(state.gen), state.dist(state.gen)};
38}
39
40} // namespace detail
41
72template <class HashAlgorithm = beast::xxhasher>
74{
75private:
77
78public:
79 using result_type = typename HashAlgorithm::result_type;
80
81 hardened_hash() : m_seeds(detail::make_seed_pair<>())
82 {
83 }
84
85 template <class T>
87 operator()(T const& t) const noexcept
88 {
89 HashAlgorithm h(m_seeds.first, m_seeds.second);
90 hash_append(h, t);
91 return static_cast<result_type>(h);
92 }
93};
94
95} // namespace ripple
96
97#endif
Seed functor once per construction.
typename HashAlgorithm::result_type result_type
detail::seed_pair m_seeds
result_type operator()(T const &t) const noexcept
seed_pair make_seed_pair() noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:180