Ethereum  PoC-8
The C++ Implementation of Ethereum
SHA3.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
24 #pragma once
25 
26 #include "FixedHash.h"
27 #include "vector_ref.h"
28 
29 #include <ethash/keccak.hpp>
30 
31 #include <string>
32 
33 namespace dev
34 {
35 
36 // SHA-3 convenience routines.
37 
40 bool sha3(bytesConstRef _input, bytesRef o_output) noexcept;
41 
43 inline h256 sha3(bytesConstRef _input) noexcept
44 {
45  h256 ret;
46  sha3(_input, ret.ref());
47  return ret;
48 }
49 
51 {
53  sha3(_input, ret.writable().ref());
54  return ret;
55 }
56 
58 inline h256 sha3(bytes const& _input) noexcept
59 {
60  return sha3(bytesConstRef(&_input));
61 }
62 
63 inline SecureFixedHash<32> sha3Secure(bytes const& _input) noexcept
64 {
65  return sha3Secure(bytesConstRef(&_input));
66 }
67 
69 inline h256 sha3(std::string const& _input) noexcept
70 {
71  return sha3(bytesConstRef(_input));
72 }
73 
74 inline SecureFixedHash<32> sha3Secure(std::string const& _input) noexcept
75 {
76  return sha3Secure(bytesConstRef(_input));
77 }
78 
80 inline h256 sha3(h256 const& _input) noexcept
81 {
82  ethash::hash256 hash = ethash::keccak256_32(_input.data());
83  return h256{hash.bytes, h256::ConstructFromPointer};
84 }
85 
87 template <unsigned N>
88 inline h256 sha3(FixedHash<N> const& _input) noexcept
89 {
90  return sha3(_input.ref());
91 }
92 
93 template <unsigned N>
94 inline SecureFixedHash<32> sha3Secure(FixedHash<N> const& _input) noexcept
95 {
96  return sha3Secure(_input.ref());
97 }
98 
100 inline SecureFixedHash<32> sha3(bytesSec const& _input) noexcept
101 {
102  return sha3Secure(_input.ref());
103 }
104 
105 inline SecureFixedHash<32> sha3Secure(bytesSec const& _input) noexcept
106 {
107  return sha3Secure(_input.ref());
108 }
109 
110 template <unsigned N>
111 inline SecureFixedHash<32> sha3(SecureFixedHash<N> const& _input) noexcept
112 {
113  return sha3Secure(_input.ref());
114 }
115 
116 template <unsigned N>
117 inline SecureFixedHash<32> sha3Secure(SecureFixedHash<N> const& _input) noexcept
118 {
119  return sha3Secure(_input.ref());
120 }
121 
123 inline std::string sha3(std::string const& _input, bool _isNibbles)
124 {
125  return asString((_isNibbles ? sha3(fromHex(_input)) : sha3(bytesConstRef(&_input))).asBytes());
126 }
127 
129 inline void sha3mac(bytesConstRef _secret, bytesConstRef _plain, bytesRef _output)
130 {
131  sha3(_secret.toBytes() + _plain.toBytes()).ref().populate(_output);
132 }
133 
134 extern h256 const EmptySHA3;
135 
136 extern h256 const EmptyListSHA3;
137 
138 }
dev::vector_ref< byte const >
dev::SecureFixedHash< 32 >
dev::sha3
bool sha3(bytesConstRef _input, bytesRef o_output) noexcept
Definition: SHA3.cpp:28
FixedHash.h
dev::FixedHash::ref
bytesRef ref()
Definition: FixedHash.h:132
dev::EmptySHA3
h256 const EmptySHA3
Definition: SHA3.cpp:25
dev::h256
FixedHash< 32 > h256
Definition: FixedHash.h:356
dev::FixedHash
Definition: FixedHash.h:47
dev::sha3mac
void sha3mac(bytesConstRef _secret, bytesConstRef _plain, bytesRef _output)
Calculate SHA3-256 MAC.
Definition: SHA3.h:129
dev::FixedHash< 32 >::ConstructFromPointer
@ ConstructFromPointer
Definition: FixedHash.h:56
dev::hash256
h256 hash256(BytesMap const &_s)
Definition: TrieHash.cpp:107
dev::secure_vector
Definition: Common.h:78
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::bytesConstRef
vector_ref< byte const > bytesConstRef
Definition: Common.h:74
dev::bytesRef
vector_ref< byte > bytesRef
Definition: Common.h:73
vector_ref.h
dev::sha3Secure
SecureFixedHash< 32 > sha3Secure(bytesConstRef _input) noexcept
Definition: SHA3.h:50
dev::asBytes
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
Definition: CommonData.h:106
dev::vector_ref::toBytes
std::vector< unsigned char > toBytes() const
Definition: vector_ref.h:43
dev::asString
std::string asString(bytes const &_b)
Definition: CommonData.h:93
dev
Definition: Address.cpp:21
dev::EmptyListSHA3
h256 const EmptyListSHA3
Definition: SHA3.cpp:26
dev::fromHex
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:81
dev::SecureFixedHash::writable
FixedHash< T > & writable()
Definition: FixedHash.h:264