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