rippled
digest.cpp
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 #include <ripple/protocol/digest.h>
21 #include <type_traits>
22 #include <openssl/ripemd.h>
23 #include <openssl/sha.h>
24 
25 namespace ripple {
26 
28 {
29  static_assert(sizeof(decltype(
31  sizeof(RIPEMD160_CTX), "");
32  auto const ctx = reinterpret_cast<
33  RIPEMD160_CTX*>(ctx_);
34  RIPEMD160_Init(ctx);
35 }
36 
37 void
39  std::size_t size) noexcept
40 {
41  auto const ctx = reinterpret_cast<
42  RIPEMD160_CTX*>(ctx_);
43  RIPEMD160_Update(ctx, data, size);
44 }
45 
46 openssl_ripemd160_hasher::operator result_type() noexcept
47 {
48  auto const ctx = reinterpret_cast<
49  RIPEMD160_CTX*>(ctx_);
51  RIPEMD160_Final(digest.data(), ctx);
52  return digest;
53 }
54 
55 //------------------------------------------------------------------------------
56 
58 {
59  static_assert(sizeof(decltype(
61  sizeof(SHA512_CTX), "");
62  auto const ctx = reinterpret_cast<
63  SHA512_CTX*>(ctx_);
64  SHA512_Init(ctx);
65 }
66 
67 void
69  std::size_t size) noexcept
70 {
71  auto const ctx = reinterpret_cast<
72  SHA512_CTX*>(ctx_);
73  SHA512_Update(ctx, data, size);
74 }
75 
76 openssl_sha512_hasher::operator result_type() noexcept
77 {
78  auto const ctx = reinterpret_cast<
79  SHA512_CTX*>(ctx_);
81  SHA512_Final(digest.data(), ctx);
82  return digest;
83 }
84 
85 //------------------------------------------------------------------------------
86 
88 {
89  static_assert(sizeof(decltype(
91  sizeof(SHA256_CTX), "");
92  auto const ctx = reinterpret_cast<
93  SHA256_CTX*>(ctx_);
94  SHA256_Init(ctx);
95 }
96 
97 void
99  std::size_t size) noexcept
100 {
101  auto const ctx = reinterpret_cast<
102  SHA256_CTX*>(ctx_);
103  SHA256_Update(ctx, data, size);
104 }
105 
106 openssl_sha256_hasher::operator result_type() noexcept
107 {
108  auto const ctx = reinterpret_cast<
109  SHA256_CTX*>(ctx_);
111  SHA256_Final(digest.data(), ctx);
112  return digest;
113 }
114 
115 } // ripple
ripple::openssl_sha512_hasher::openssl_sha512_hasher
openssl_sha512_hasher()
Definition: digest.cpp:57
ripple::openssl_sha512_hasher::operator()
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.cpp:68
ripple::openssl_sha512_hasher::ctx_
char ctx_[216]
Definition: digest.h:92
ripple::openssl_ripemd160_hasher::openssl_ripemd160_hasher
openssl_ripemd160_hasher()
Definition: digest.cpp:27
ripple::openssl_ripemd160_hasher::operator()
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.cpp:38
ripple::openssl_sha256_hasher::ctx_
char ctx_[112]
Definition: digest.h:118
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:44
std::array
STL class.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::openssl_sha256_hasher::openssl_sha256_hasher
openssl_sha256_hasher()
Definition: digest.cpp:87
std::size_t
ripple::openssl_ripemd160_hasher::ctx_
char ctx_[96]
Definition: digest.h:66
type_traits
ripple::openssl_sha256_hasher::operator()
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.cpp:98