forked from FISCO-BCOS/FISCO-BCOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.h
128 lines (109 loc) · 3.6 KB
/
Hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
This file is part of FISCO-BCOS.
FISCO-BCOS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FISCO-BCOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FISCO-BCOS. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Hash.h
* @author Alex Leverington <nessence@gmail.com> Asherli
* @date 2018
*
* The FixedHash fixed-size "hash" container type.
*/
#pragma once
#include <libdevcore/FixedHash.h>
#include <libdevcore/vector_ref.h>
#include <string>
namespace dev
{
// SHA-3 convenience routines.
/// Calculate SHA3-256 hash of the given input and load it into the given output.
/// @returns false if o_output.size() != 32.
bool sha3(bytesConstRef _input, bytesRef o_output);
// sha2 - sha256 replace Hash.h begin
h256 sha256(bytesConstRef _input) noexcept;
// sha2 - sha256 replace Hash.h end
h160 ripemd160(bytesConstRef _input);
/// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash.
inline h256 sha3(bytesConstRef _input)
{
h256 ret;
sha3(_input, ret.ref());
return ret;
}
inline SecureFixedHash<32> sha3Secure(bytesConstRef _input)
{
SecureFixedHash<32> ret;
sha3(_input, ret.writable().ref());
return ret;
}
/// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash.
inline h256 sha3(bytes const& _input)
{
return sha3(bytesConstRef(&_input));
}
inline SecureFixedHash<32> sha3Secure(bytes const& _input)
{
return sha3Secure(bytesConstRef(&_input));
}
/// Calculate SHA3-256 hash of the given input (presented as a binary-filled string), returning as a
/// 256-bit hash.
inline h256 sha3(std::string const& _input)
{
return sha3(bytesConstRef(_input));
}
inline SecureFixedHash<32> sha3Secure(std::string const& _input)
{
return sha3Secure(bytesConstRef(_input));
}
/// Calculate SHA3-256 hash of the given input (presented as a FixedHash), returns a 256-bit hash.
template <unsigned N>
inline h256 sha3(FixedHash<N> const& _input)
{
return sha3(_input.ref());
}
template <unsigned N>
inline SecureFixedHash<32> sha3Secure(FixedHash<N> const& _input)
{
return sha3Secure(_input.ref());
}
/// Fully secure variants are equivalent for sha3 and sha3Secure.
inline SecureFixedHash<32> sha3(bytesSec const& _input)
{
return sha3Secure(_input.ref());
}
inline SecureFixedHash<32> sha3Secure(bytesSec const& _input)
{
return sha3Secure(_input.ref());
}
template <unsigned N>
inline SecureFixedHash<32> sha3(SecureFixedHash<N> const& _input)
{
return sha3Secure(_input.ref());
}
template <unsigned N>
inline SecureFixedHash<32> sha3Secure(SecureFixedHash<N> const& _input)
{
return sha3Secure(_input.ref());
}
/// Calculate SHA3-256 hash of the given input, possibly interpreting it as nibbles, and return the
/// hash as a string filled with binary data.
inline std::string sha3(std::string const& _input, bool _isNibbles)
{
return asString((_isNibbles ? sha3(fromHex(_input)) : sha3(bytesConstRef(&_input))).asBytes());
}
/// Calculate SHA3-256 MAC
inline void sha3mac(bytesConstRef _secret, bytesConstRef _plain, bytesRef _output)
{
sha3(_secret.toBytes() + _plain.toBytes()).ref().populate(_output);
}
extern h256 EmptySHA3;
extern h256 EmptyListSHA3;
} // namespace dev