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#include <boost/endian/conversion.hpp>
26#include <algorithm>
27#include <array>
28
29namespace ripple {
30
40//------------------------------------------------------------------------------
41
47{
48public:
49 static constexpr auto const endian = boost::endian::order::native;
50
52
54
55 void
56 operator()(void const* data, std::size_t size) noexcept;
57
58 explicit
59 operator result_type() noexcept;
60
61private:
62 char ctx_[96];
63};
64
70{
71public:
72 static constexpr auto const endian = boost::endian::order::native;
73
75
77
78 void
79 operator()(void const* data, std::size_t size) noexcept;
80
81 explicit
82 operator result_type() noexcept;
83
84private:
85 char ctx_[216];
86};
87
93{
94public:
95 static constexpr auto const endian = boost::endian::order::native;
96
98
100
101 void
102 operator()(void const* data, std::size_t size) noexcept;
103
104 explicit
105 operator result_type() noexcept;
106
107private:
108 char ctx_[112];
109};
110
111//------------------------------------------------------------------------------
112
116
117//------------------------------------------------------------------------------
118
135{
136private:
138
139public:
140 static constexpr auto const endian = boost::endian::order::native;
141
143
144 void
145 operator()(void const* data, std::size_t size) noexcept
146 {
147 h_(data, size);
148 }
149
150 explicit
151 operator result_type() noexcept
152 {
153 auto const d0 = sha256_hasher::result_type(h_);
155 rh(d0.data(), d0.size());
157 }
158};
159
160//------------------------------------------------------------------------------
161
162namespace detail {
163
169template <bool Secure>
171{
172private:
174
175public:
176 static constexpr auto const endian = boost::endian::order::big;
177
179
181 {
183 }
184
185 void
186 operator()(void const* data, std::size_t size) noexcept
187 {
188 h_(data, size);
189 }
190
191 explicit
192 operator result_type() noexcept
193 {
194 auto const digest = sha512_hasher::result_type(h_);
195 return result_type::fromVoid(digest.data());
196 }
197
198private:
199 inline void
201 {
202 }
203
204 inline void
206 {
207 secure_erase(&h_, sizeof(h_));
208 }
209};
210
211} // namespace detail
212
214
215// secure version
217
218//------------------------------------------------------------------------------
219
221template <class... Args>
223sha512Half(Args const&... args)
224{
226 using beast::hash_append;
227 hash_append(h, args...);
228 return static_cast<typename sha512_half_hasher::result_type>(h);
229}
230
237template <class... Args>
239sha512Half_s(Args const&... args)
240{
242 using beast::hash_append;
243 hash_append(h, args...);
244 return static_cast<typename sha512_half_hasher_s::result_type>(h);
245}
246
247} // namespace ripple
248
249#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:236
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:239
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:198
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition: STExchange.h:171
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:223
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:171
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.h:186
Message digest functions used in the codebase.
Definition: digest.h:47
static constexpr auto const endian
Definition: digest.h:49
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.cpp:38
std::array< std::uint8_t, 20 > result_type
Definition: digest.h:51
SHA-256 digest.
Definition: digest.h:93
std::array< std::uint8_t, 32 > result_type
Definition: digest.h:97
SHA-512 digest.
Definition: digest.h:70
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
Definition: digest.h:135
sha256_hasher h_
Definition: digest.h:137
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.h:145