rippled
Loading...
Searching...
No Matches
digest.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 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_PROTOCOL_DIGEST_H_INCLUDED
21#define RIPPLE_PROTOCOL_DIGEST_H_INCLUDED
22
23#include <xrpl/basics/base_uint.h>
24#include <xrpl/crypto/secure_erase.h>
25
26#include <boost/endian/conversion.hpp>
27
28#include <array>
29
30namespace ripple {
31
41//------------------------------------------------------------------------------
42
48{
49public:
50 static constexpr auto const endian = boost::endian::order::native;
51
53
55
56 void
57 operator()(void const* data, std::size_t size) noexcept;
58
59 explicit
60 operator result_type() noexcept;
61
62private:
63 char ctx_[96];
64};
65
71{
72public:
73 static constexpr auto const endian = boost::endian::order::native;
74
76
78
79 void
80 operator()(void const* data, std::size_t size) noexcept;
81
82 explicit
83 operator result_type() noexcept;
84
85private:
86 char ctx_[216];
87};
88
94{
95public:
96 static constexpr auto const endian = boost::endian::order::native;
97
99
101
102 void
103 operator()(void const* data, std::size_t size) noexcept;
104
105 explicit
106 operator result_type() noexcept;
107
108private:
109 char ctx_[112];
110};
111
112//------------------------------------------------------------------------------
113
117
118//------------------------------------------------------------------------------
119
136{
137private:
139
140public:
141 static constexpr auto const endian = boost::endian::order::native;
142
144
145 void
146 operator()(void const* data, std::size_t size) noexcept
147 {
148 h_(data, size);
149 }
150
151 explicit
152 operator result_type() noexcept
153 {
154 auto const d0 = sha256_hasher::result_type(h_);
156 rh(d0.data(), d0.size());
158 }
159};
160
161//------------------------------------------------------------------------------
162
163namespace detail {
164
170template <bool Secure>
172{
173private:
175
176public:
177 static constexpr auto const endian = boost::endian::order::big;
178
180
185
186 void
187 operator()(void const* data, std::size_t size) noexcept
188 {
189 h_(data, size);
190 }
191
192 explicit
193 operator result_type() noexcept
194 {
195 auto const digest = sha512_hasher::result_type(h_);
196 return result_type::fromVoid(digest.data());
197 }
198
199private:
200 inline void
202 {
203 }
204
205 inline void
207 {
208 secure_erase(&h_, sizeof(h_));
209 }
210};
211
212} // namespace detail
213
215
216// secure version
218
219//------------------------------------------------------------------------------
220
222template <class... Args>
224sha512Half(Args const&... args)
225{
227 using beast::hash_append;
228 hash_append(h, args...);
229 return static_cast<typename sha512_half_hasher::result_type>(h);
230}
231
238template <class... Args>
240sha512Half_s(Args const&... args)
241{
243 using beast::hash_append;
244 hash_append(h, args...);
245 return static_cast<typename sha512_half_hasher_s::result_type>(h);
246}
247
248} // namespace ripple
249
250#endif
std::enable_if_t< is_contiguously_hashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
sha512_half_hasher_s::result_type sha512Half_s(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:240
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:199
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition STExchange.h:172
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:224
void secure_erase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
Returns the SHA512-Half digest of a message.
Definition digest.h:172
void operator()(void const *data, std::size_t size) noexcept
Definition digest.h:187
Message digest functions used in the codebase.
Definition digest.h:48
static constexpr auto const endian
Definition digest.h:50
void operator()(void const *data, std::size_t size) noexcept
Definition digest.cpp:40
std::array< std::uint8_t, 20 > result_type
Definition digest.h:52
std::array< std::uint8_t, 32 > result_type
Definition digest.h:98
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
Definition digest.h:136
sha256_hasher h_
Definition digest.h:138
void operator()(void const *data, std::size_t size) noexcept
Definition digest.h:146