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 <algorithm>
29#include <array>
30
31namespace ripple {
32
42//------------------------------------------------------------------------------
43
49{
50public:
51 static constexpr auto const endian = boost::endian::order::native;
52
54
56
57 void
58 operator()(void const* data, std::size_t size) noexcept;
59
60 explicit
61 operator result_type() noexcept;
62
63private:
64 char ctx_[96];
65};
66
72{
73public:
74 static constexpr auto const endian = boost::endian::order::native;
75
77
79
80 void
81 operator()(void const* data, std::size_t size) noexcept;
82
83 explicit
84 operator result_type() noexcept;
85
86private:
87 char ctx_[216];
88};
89
95{
96public:
97 static constexpr auto const endian = boost::endian::order::native;
98
100
102
103 void
104 operator()(void const* data, std::size_t size) noexcept;
105
106 explicit
107 operator result_type() noexcept;
108
109private:
110 char ctx_[112];
111};
112
113//------------------------------------------------------------------------------
114
118
119//------------------------------------------------------------------------------
120
137{
138private:
140
141public:
142 static constexpr auto const endian = boost::endian::order::native;
143
145
146 void
147 operator()(void const* data, std::size_t size) noexcept
148 {
149 h_(data, size);
150 }
151
152 explicit
153 operator result_type() noexcept
154 {
155 auto const d0 = sha256_hasher::result_type(h_);
157 rh(d0.data(), d0.size());
159 }
160};
161
162//------------------------------------------------------------------------------
163
164namespace detail {
165
171template <bool Secure>
173{
174private:
176
177public:
178 static constexpr auto const endian = boost::endian::order::big;
179
181
183 {
185 }
186
187 void
188 operator()(void const* data, std::size_t size) noexcept
189 {
190 h_(data, size);
191 }
192
193 explicit
194 operator result_type() noexcept
195 {
196 auto const digest = sha512_hasher::result_type(h_);
197 return result_type::fromVoid(digest.data());
198 }
199
200private:
201 inline void
203 {
204 }
205
206 inline void
208 {
209 secure_erase(&h_, sizeof(h_));
210 }
211};
212
213} // namespace detail
214
216
217// secure version
219
220//------------------------------------------------------------------------------
221
223template <class... Args>
225sha512Half(Args const&... args)
226{
228 using beast::hash_append;
229 hash_append(h, args...);
230 return static_cast<typename sha512_half_hasher::result_type>(h);
231}
232
239template <class... Args>
241sha512Half_s(Args const&... args)
242{
244 using beast::hash_append;
245 hash_append(h, args...);
246 return static_cast<typename sha512_half_hasher_s::result_type>(h);
247}
248
249} // namespace ripple
250
251#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.
Definition: hash_append.h:237
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
sha512_half_hasher_s::result_type sha512Half_s(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:241
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:225
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:173
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.h:188
Message digest functions used in the codebase.
Definition: digest.h:49
static constexpr auto const endian
Definition: digest.h:51
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:53
SHA-256 digest.
Definition: digest.h:95
std::array< std::uint8_t, 32 > result_type
Definition: digest.h:99
SHA-512 digest.
Definition: digest.h:72
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
Definition: digest.h:137
sha256_hasher h_
Definition: digest.h:139
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.h:147