rippled
Loading...
Searching...
No Matches
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 <xrpl/beast/hash/hash_append.h>
24#include <xrpl/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
35namespace ripple {
36
37namespace detail {
38
40
41template <bool = true>
44{
45 struct state_t
46 {
47 std::mutex mutex;
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
95template <class HashAlgorithm = beast::xxhasher>
97{
98private:
100
101public:
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
Seed functor once per construction.
Definition: hardened_hash.h:97
typename HashAlgorithm::result_type result_type
detail::seed_pair m_seeds
Definition: hardened_hash.h:99
result_type operator()(T const &t) const noexcept
seed_pair make_seed_pair() noexcept
Definition: hardened_hash.h:43
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:199