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 <mutex>
28#include <random>
29#include <utility>
30
31namespace ripple {
32
33namespace detail {
34
36
37template <bool = true>
40{
41 struct state_t
42 {
43 std::mutex mutex;
47
48 state_t() : gen(rng())
49 {
50 }
51 // state_t(state_t const&) = delete;
52 // state_t& operator=(state_t const&) = delete;
53 };
54 static state_t state;
55 std::lock_guard lock(state.mutex);
56 return {state.dist(state.gen), state.dist(state.gen)};
57}
58
59} // namespace detail
60
91template <class HashAlgorithm = beast::xxhasher>
93{
94private:
96
97public:
98 using result_type = typename HashAlgorithm::result_type;
99
100 hardened_hash() : m_seeds(detail::make_seed_pair<>())
101 {
102 }
103
104 template <class T>
106 operator()(T const& t) const noexcept
107 {
108 HashAlgorithm h(m_seeds.first, m_seeds.second);
109 hash_append(h, t);
110 return static_cast<result_type>(h);
111 }
112};
113
114} // namespace ripple
115
116#endif
Seed functor once per construction.
Definition: hardened_hash.h:93
typename HashAlgorithm::result_type result_type
Definition: hardened_hash.h:98
detail::seed_pair m_seeds
Definition: hardened_hash.h:95
result_type operator()(T const &t) const noexcept
seed_pair make_seed_pair() noexcept
Definition: hardened_hash.h:39
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:199